<?php

namespace App\Http\Requests;

use App\Concerns\EmployeeDocumentValidationRules;
use App\Concerns\ValidatesStaffDesignation;
use App\Models\TemporaryEmployee;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class StoreTemporaryEmployeeRequest extends FormRequest
{
    use EmployeeDocumentValidationRules, ValidatesStaffDesignation;

    public function authorize(): bool
    {
        return (bool) $this->user()?->can('temporary-employees.create');
    }

    protected function prepareForValidation(): void
    {
        $this->merge([
            'is_active' => $this->has('is_active')
                ? $this->boolean('is_active')
                : true,
        ]);
    }

    /**
     * @return array<string, array<int, mixed>>
     */
    public function rules(): array
    {
        return [
            'designation_id' => $this->designationIdRules(),
            'full_name' => ['required', 'string', 'max:255'],
            'father_name' => ['required', 'string', 'max:255'],
            'national_card_number' => [
                'required',
                'string',
                'max:50',
                Rule::unique(TemporaryEmployee::class, 'national_card_number'),
            ],
            'address' => ['required', 'string', 'max:1000'],
            'contact_number' => ['required', 'string', 'max:30'],
            'is_active' => ['required', 'boolean'],
            'image' => $this->employeeImageRules(required: false),
            'health_card' => $this->employeeDocumentRules(required: false),
        ];
    }

    /**
     * @return array<string, string>
     */
    public function attributes(): array
    {
        return [
            'contact_number' => 'contact number',
            'national_card_number' => 'national card number',
            'health_card' => 'health card',
        ];
    }
}
