<?php

namespace App\Modules\HR\Employees\Requests;

use Illuminate\Validation\Rule;

final class EmployeeFormRules
{
    /**
     * @return array<string, mixed>
     */
    public static function employeeCore(bool $salaryRequired = true): array
    {
        $salaryRules = $salaryRequired
            ? ['required', 'numeric', 'min:0', 'max:9999999999999.99']
            : ['nullable', 'numeric', 'min:0', 'max:9999999999999.99'];

        return [
            'full_name' => ['required', 'string', 'max:255'],
            'arabic_name' => ['nullable', 'string', 'max:255'],
            'fingerprint_id' => ['nullable', 'string', 'max:128'],
            'phone_1' => ['required', 'string', 'max:40'],
            'phone_2' => ['nullable', 'string', 'max:40'],
            'email' => ['nullable', 'email', 'max:255'],
            'company_email' => ['nullable', 'email', 'max:255'],
            'department_id' => ['required', 'integer', 'exists:departments,id'],
            'branch_id' => ['nullable', 'integer', Rule::exists('hr_branches', 'id')->where('tenant_id', auth()->user()?->tenant_id)],
            'section_id' => ['nullable', 'integer', Rule::exists('hr_sections', 'id')->where('tenant_id', auth()->user()?->tenant_id)],
            'team_id' => ['nullable', 'integer', Rule::exists('hr_teams', 'id')->where('tenant_id', auth()->user()?->tenant_id)],
            'position_id' => ['required', 'integer', 'exists:positions,id'],
            'job_grade_id' => ['nullable', 'integer', Rule::exists('hr_job_grades', 'id')->where('tenant_id', auth()->user()?->tenant_id)],
            'work_location_id' => ['nullable', 'integer', Rule::exists('hr_work_locations', 'id')->where('tenant_id', auth()->user()?->tenant_id)],
            'cost_center_id' => ['nullable', 'integer', Rule::exists('hr_cost_centers', 'id')->where('tenant_id', auth()->user()?->tenant_id)],
            'manager_employee_id' => ['nullable', 'integer', 'exists:employees,id'],
            'salary' => $salaryRules,
            'hire_date' => ['required', 'date'],
            'probation_end_date' => ['nullable', 'date', 'after_or_equal:hire_date'],
            'confirmation_date' => ['nullable', 'date', 'after_or_equal:hire_date'],
            'gender' => ['required', 'in:male,female,other'],
            'date_of_birth' => ['nullable', 'date', 'before:today'],
            'marital_status' => ['nullable', 'in:single,married,divorced,widowed'],
            'blood_type' => ['nullable', 'in:A+,A-,B+,B-,AB+,AB-,O+,O-'],
            'religion' => ['nullable', 'string', 'max:80'],
            'nationality' => ['nullable', 'string', 'max:100'],
            'national_id' => ['required', 'string', 'max:120'],
            'passport_number' => ['nullable', 'string', 'max:80'],
            'social_insurance_number' => ['nullable', 'string', 'max:80'],
            'tax_number' => ['nullable', 'string', 'max:80'],
            'bank_name' => ['nullable', 'string', 'max:120'],
            'bank_account_number' => ['nullable', 'string', 'max:80'],
            'iban' => ['nullable', 'string', 'max:64'],
            'swift_code' => ['nullable', 'string', 'max:32'],
            'employment_status_id' => ['required', 'integer', 'exists:employment_statuses,id'],
        ];
    }

    /**
     * @return array<string, mixed>
     */
    public static function address(): array
    {
        return [
            'address_country_id' => ['nullable', 'integer', 'exists:geo_countries,id'],
            'address_region_id' => ['nullable', 'integer', 'exists:geo_regions,id'],
            'address_city_id' => ['nullable', 'integer', 'exists:geo_cities,id'],
            'address_postal_code' => ['nullable', 'string', 'max:32'],
            'address_street' => ['nullable', 'string', 'max:255'],
            'address_building_number' => ['nullable', 'string', 'max:50'],
            'address_location_link' => ['nullable', 'string', 'max:500'],
        ];
    }

    /**
     * @return array<string, mixed>
     */
    public static function customFieldsShell(): array
    {
        return [
            'custom_fields' => ['nullable', 'array'],
        ];
    }

    public static function documents(): array
    {
        $imagePdf = ['nullable', 'file', 'max:10240', 'mimes:pdf,jpg,jpeg,png'];
        $office = ['nullable', 'file', 'max:10240', 'mimes:pdf,doc,docx'];
        $photo = ['nullable', 'file', 'max:10240', 'mimes:jpg,jpeg,png'];

        return [
            'doc_photo' => $photo,
            'doc_cv' => $office,
            'doc_national_id_front' => $imagePdf,
            'doc_national_id_back' => $imagePdf,
            'doc_national_id_expires_at' => ['nullable', 'date'],
            'doc_passport' => $imagePdf,
            'doc_passport_expires_at' => ['nullable', 'date'],
            'doc_contract' => $imagePdf,
            'doc_contract_expires_at' => ['nullable', 'date'],
            'doc_certificate' => $office,
            'doc_residency' => $imagePdf,
            'doc_residency_expires_at' => ['nullable', 'date'],
            'doc_visa' => $imagePdf,
            'doc_visa_expires_at' => ['nullable', 'date'],
            'doc_work_permit' => $imagePdf,
            'doc_work_permit_expires_at' => ['nullable', 'date'],
        ];
    }
}
