<?php

namespace App\Http\Requests\Recruitment;

use App\Domain\Identity\Models\User;
use App\Domain\Organization\Models\Branch;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class StoreJobDemandRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            'employer_id' => ['required', 'uuid', Rule::exists('employers', 'uuid')->whereNull('deleted_at')],
            'branch_id' => ['nullable', 'uuid', Rule::exists('branches', 'uuid')->whereNull('deleted_at')],
            'owner_id' => ['nullable', 'uuid', Rule::exists('users', 'uuid')->whereNull('deleted_at')],

            'job_title' => ['required', 'string', 'max:255'],
            'country' => ['nullable', 'string', 'size:2'],
            'city' => ['nullable', 'string', 'max:100'],
            'industry' => ['nullable', 'string', 'max:120'],
            'occupation' => ['nullable', 'string', 'max:120'],

            'quantity_required' => ['required', 'integer', 'min:1', 'max:100000'],
            'gender_requirement' => ['nullable', Rule::in(['male', 'female', 'any'])],
            'minimum_age' => ['nullable', 'integer', 'between:16,80'],
            'maximum_age' => ['nullable', 'integer', 'between:16,80', 'gte:minimum_age'],

            'salary_amount' => ['nullable', 'integer', 'min:0'],
            'salary_currency' => ['nullable', 'string', 'size:3', 'required_with:salary_amount'],
            'salary_period' => ['nullable', Rule::in(['hourly', 'daily', 'weekly', 'monthly', 'yearly'])],
            'overtime_details' => ['nullable', 'string', 'max:1000'],
            'working_hours_per_day' => ['nullable', 'integer', 'between:1,24'],
            'working_days_per_week' => ['nullable', 'integer', 'between:1,7'],
            'weekly_holiday' => ['nullable', 'string', 'max:40'],

            'accommodation_provided' => ['nullable', 'boolean'],
            'food_provided' => ['nullable', 'boolean'],
            'transport_provided' => ['nullable', 'boolean'],
            'medical_provided' => ['nullable', 'boolean'],
            'insurance_provided' => ['nullable', 'boolean'],
            'airfare_terms' => ['nullable', 'string', 'max:60'],
            'benefits_notes' => ['nullable', 'string', 'max:2000'],

            'contract_duration_months' => ['nullable', 'integer', 'between:1,600'],
            'probation_months' => ['nullable', 'integer', 'between:0,24'],
            'qualification' => ['nullable', 'string', 'max:2000'],
            'required_experience_years' => ['nullable', 'integer', 'between:0,60'],
            'language_requirements' => ['nullable', 'string', 'max:1000'],
            'skills' => ['nullable', 'string', 'max:2000'],
            'employment_terms' => ['nullable', 'string', 'max:5000'],

            'recruitment_fee_amount' => ['nullable', 'integer', 'min:0'],
            'processing_cost_amount' => ['nullable', 'integer', 'min:0'],
            'fee_currency' => ['nullable', 'string', 'size:3'],

            'application_deadline' => ['nullable', 'date'],
            'interview_date' => ['nullable', 'date'],
            'deployment_target_date' => ['nullable', 'date'],

            'notes' => ['nullable', 'string', 'max:5000'],
        ];
    }

    /** @return array<string, mixed> Without the employer, which is resolved separately. */
    public function modelAttributes(): array
    {
        $data = $this->validated();
        unset($data['employer_id']);

        $data['branch_id'] = $this->resolveKey(Branch::class, $data['branch_id'] ?? null);
        $data['owner_id'] = $this->resolveKey(User::class, $data['owner_id'] ?? null);

        return $data;
    }

    private function resolveKey(string $model, ?string $uuid): ?int
    {
        return $uuid === null ? null : $model::where('uuid', $uuid)->value('id');
    }
}
