<?php

namespace App\Modules\HR\Recruitment\Requests;

use App\Modules\HR\Recruitment\Models\RecruitmentInterview;
use App\Modules\HR\Recruitment\Support\InterviewMode;
use App\Modules\HR\Recruitment\Support\InterviewStatus;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class StoreInterviewRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user()?->can('create', RecruitmentInterview::class) ?? false;
    }

    /** @return array<string, mixed> */
    public function rules(): array
    {
        return $this->baseRules();
    }

    /** @return array<string, mixed> */
    protected function baseRules(): array
    {
        $tenantId = $this->user()?->tenant_id;

        return [
            'application_id' => [
                'required',
                'integer',
                Rule::exists('recruitment_applications', 'id')->where(
                    fn ($q) => $q->where('tenant_id', $tenantId)->whereNull('deleted_at')
                ),
            ],
            'scheduled_at' => ['required', 'date'],
            'mode' => ['required', 'string', Rule::in(InterviewMode::all())],
            'location' => ['nullable', 'string', 'max:255'],
            'interviewer_user_id' => [
                'nullable',
                'integer',
                Rule::exists('users', 'id')->where(fn ($q) => $q->where('tenant_id', $tenantId)),
            ],
            'notes' => ['nullable', 'string', 'max:5000'],
            'status' => ['nullable', 'string', Rule::in(InterviewStatus::all())],
        ];
    }
}
