<?php

namespace App\Master\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;
use Mtc\MercuryDataModels\Tenant;

class CreateLiveSiteAccessRequest extends FormRequest
{
    public function authorize(): bool
    {
        return Auth::user()?->hasRole('mtc') ?? false;
    }

    public function rules(): array
    {
        return [
            'tenant_id' => [
                'required',
                'string',
                Rule::exists(Tenant::class, 'id')->where(function ($query) {
                    $query->whereNotNull('live_at');
                }),
            ],
            'reason' => ['required', 'string', 'max:1000'],
        ];
    }

    public function messages(): array
    {
        return [
            'tenant_id.exists' => 'The selected site is not a live site or does not exist.',
        ];
    }
}
