<?php

namespace App\Http\Requests\PublicSite;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

/**
 * Public employer manpower requisition (§144A).
 */
class SubmitRequisitionRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            // Step 1 — company
            'company_name' => ['required', 'string', 'max:255'],
            'country' => ['required', 'string', 'size:2'],
            'city' => ['nullable', 'string', 'max:100'],
            'industry' => ['required', 'string', 'max:120'],
            'website' => ['nullable', 'url', 'max:255'],
            'registration_number' => ['nullable', 'string', 'max:100'],
            'license_number' => ['nullable', 'string', 'max:100'],
            'company_address' => ['required', 'string', 'max:1000'],

            // Step 2 — contact person
            'contact_name' => ['required', 'string', 'max:255'],
            'contact_designation' => ['required', 'string', 'max:120'],
            'contact_email' => ['required', 'email', 'max:255'],
            'contact_phone' => ['required', 'string', 'max:32'],
            'contact_whatsapp' => ['nullable', 'string', 'max:32'],
            'preferred_contact_method' => ['nullable', Rule::in(['email', 'phone', 'whatsapp'])],

            // Step 6 — timeline
            'required_joining_date' => ['nullable', 'date'],
            'application_deadline' => ['nullable', 'date'],
            'preferred_interview_date' => ['nullable', 'date'],
            'expected_deployment_date' => ['nullable', 'date'],
            'urgency' => ['nullable', Rule::in(['standard', 'urgent', 'critical'])],

            // Step 7 — selection preference
            'interview_required' => ['nullable', 'boolean'],
            'interview_method' => ['nullable', Rule::in(['online_video', 'in_person', 'employer_delegation', 'agency_screening', 'other'])],
            'employer_visiting' => ['nullable', 'boolean'],
            'cv_shortlisting_required' => ['nullable', 'boolean'],
            'trade_test_required' => ['nullable', 'boolean'],

            'additional_notes' => ['nullable', 'string', 'max:5000'],

            // Steps 3–5 & 8 — at least one position, each with its own package.
            'positions' => ['required', 'array', 'min:1', 'max:20'],
            'positions.*.job_title' => ['required', 'string', 'max:255'],
            'positions.*.job_category_id' => ['nullable', 'uuid', Rule::exists('job_categories', 'uuid')],
            'positions.*.quantity' => ['required', 'integer', 'min:1', 'max:100000'],
            'positions.*.gender_preference' => ['nullable', Rule::in(['male', 'female', 'any'])],
            'positions.*.minimum_age' => ['nullable', 'integer', 'between:16,80'],
            'positions.*.maximum_age' => ['nullable', 'integer', 'between:16,80', 'gte:positions.*.minimum_age'],
            'positions.*.minimum_experience_years' => ['nullable', 'integer', 'between:0,60'],
            'positions.*.qualification' => ['nullable', 'string', 'max:2000'],
            'positions.*.required_skills' => ['nullable', 'string', 'max:2000'],
            'positions.*.trade_test' => ['nullable', Rule::in(['no', 'agency_arranged', 'employer_arranged', 'undecided'])],

            'positions.*.salary_amount' => ['required', 'integer', 'min:0'],
            'positions.*.salary_currency' => ['required', 'string', 'size:3'],
            'positions.*.salary_period' => ['nullable', Rule::in(['hourly', 'daily', 'weekly', 'monthly'])],
            'positions.*.working_hours_per_day' => ['nullable', 'integer', 'between:1,24'],
            'positions.*.working_days_per_week' => ['nullable', 'integer', 'between:1,7'],
            'positions.*.overtime_available' => ['nullable', 'boolean'],
            'positions.*.overtime_details' => ['nullable', 'string', 'max:1000'],
            'positions.*.contract_duration_months' => ['nullable', 'integer', 'between:1,600'],
            'positions.*.probation_months' => ['nullable', 'integer', 'between:0,24'],
            'positions.*.weekly_holiday' => ['nullable', 'string', 'max:40'],
            'positions.*.annual_leave_days' => ['nullable', 'integer', 'between:0,365'],

            'positions.*.accommodation_provided' => ['nullable', 'boolean'],
            'positions.*.food_provided' => ['nullable', 'boolean'],
            'positions.*.food_allowance' => ['nullable', 'boolean'],
            'positions.*.transport_provided' => ['nullable', 'boolean'],
            'positions.*.medical_provided' => ['nullable', 'boolean'],
            'positions.*.insurance_provided' => ['nullable', 'boolean'],
            'positions.*.airfare_provided' => ['nullable', 'boolean'],
            'positions.*.visa_cost_covered' => ['nullable', 'boolean'],
            'positions.*.work_permit_cost_covered' => ['nullable', 'boolean'],
            'positions.*.uniform_provided' => ['nullable', 'boolean'],
            'positions.*.other_benefits' => ['nullable', 'string', 'max:2000'],
            'positions.*.required_documents' => ['nullable', 'array', 'max:30'],
            'positions.*.required_documents.*' => ['string', 'max:120'],

            // Honeypot: a real browser leaves this empty.
            'website_url' => ['nullable', 'size:0'],
            'consent' => ['accepted'],
        ];
    }

    public function messages(): array
    {
        return [
            'positions.required' => 'Add at least one position you need workers for.',
            'consent.accepted' => 'Please confirm the details you have given are accurate.',
        ];
    }

    /** @return array<string, mixed> */
    public function requisitionAttributes(): array
    {
        return $this->safe()->except(['positions', 'consent', 'website_url']);
    }

    /** @return array<int, array<string, mixed>> */
    public function positions(): array
    {
        return $this->safe()->only(['positions'])['positions'] ?? [];
    }
}
