<?php

namespace App\Modules\HR\Recruitment\Requests;

use App\Modules\HR\Recruitment\Models\RecruitmentOfferLetter;
use App\Modules\HR\Recruitment\Support\OfferLetterEmploymentType;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class StoreOfferLetterRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user()?->can('create', RecruitmentOfferLetter::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')
                ),
            ],
            'candidate_id' => [
                'required', 'integer',
                Rule::exists('recruitment_candidates', 'id')->where(
                    fn ($q) => $q->where('tenant_id', $tenantId)->whereNull('deleted_at')
                ),
            ],
            'vacancy_id' => [
                'required', 'integer',
                Rule::exists('recruitment_vacancies', 'id')->where(
                    fn ($q) => $q->where('tenant_id', $tenantId)->whereNull('deleted_at')
                ),
            ],
            'department_id' => [
                'nullable', 'integer',
                Rule::exists('departments', 'id')->where(fn ($q) => $q->where('tenant_id', $tenantId)),
            ],
            'position_id' => [
                'nullable', 'integer',
                Rule::exists('positions', 'id')->where(fn ($q) => $q->where('tenant_id', $tenantId)),
            ],
            'employment_type' => ['required', 'string', Rule::in(OfferLetterEmploymentType::all())],
            'employment_status_id' => [
                'nullable', 'integer',
                Rule::exists('employment_statuses', 'id')->where(fn ($q) => $q->where('tenant_id', $tenantId)),
            ],
            'start_date' => ['required', 'date'],
            'probation_period_days' => ['nullable', 'integer', 'min:0', 'max:365'],
            'salary' => ['required', 'numeric', 'min:0', 'max:9999999999999.99'],
            'currency' => ['required', 'string', 'size:3'],
            'allowances' => ['nullable', 'string', 'max:5000'],
            'benefits' => ['nullable', 'string', 'max:5000'],
            'working_hours' => ['nullable', 'string', 'max:120'],
            'notes' => ['nullable', 'string', 'max:5000'],
            'expires_at' => ['nullable', 'date', 'after:now'],
            'acceptance_deadline_at' => ['nullable', 'date', 'after:now'],
            'attachments' => ['nullable', 'array', 'max:5'],
            'attachments.*' => ['file', 'max:10240'],
        ];
    }
}
