<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

/** The staff-facing view of an employer requisition (§144G). */
class RequisitionResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->uuid,
            'requisition_no' => $this->requisition_no,
            'status' => $this->status->value,
            'status_label' => $this->status->label(),
            'status_reason' => $this->status_reason,
            'allowed_transitions' => array_map(
                fn ($status) => ['value' => $status->value, 'label' => $status->label()],
                $this->status->allowedTransitions(),
            ),
            'can_convert' => $this->status->canConvert(),
            'priority' => $this->priority,
            'urgency' => $this->urgency,

            'company_name' => $this->company_name,
            'country' => $this->country,
            'city' => $this->city,
            'industry' => $this->industry,
            'website' => $this->website,
            'registration_number' => $this->registration_number,
            'license_number' => $this->license_number,
            'company_address' => $this->company_address,

            'contact' => [
                'name' => $this->contact_name,
                'designation' => $this->contact_designation,
                'email' => $this->contact_email,
                'phone' => $this->contact_phone,
                'whatsapp' => $this->contact_whatsapp,
                'preferred_method' => $this->preferred_contact_method,
            ],

            'timeline' => [
                'required_joining_date' => $this->required_joining_date?->toDateString(),
                'application_deadline' => $this->application_deadline?->toDateString(),
                'preferred_interview_date' => $this->preferred_interview_date?->toDateString(),
                'expected_deployment_date' => $this->expected_deployment_date?->toDateString(),
            ],

            'selection' => [
                'interview_required' => (bool) $this->interview_required,
                'interview_method' => $this->interview_method,
                'employer_visiting' => (bool) $this->employer_visiting,
                'cv_shortlisting_required' => (bool) $this->cv_shortlisting_required,
                'trade_test_required' => (bool) $this->trade_test_required,
            ],

            'additional_notes' => $this->additional_notes,
            'total_workers' => $this->whenLoaded('positions', fn () => $this->totalWorkers()),

            'positions' => $this->whenLoaded('positions', fn () => $this->positions->map(fn ($position) => [
                'id' => $position->uuid,
                'job_title' => $position->job_title,
                'quantity' => $position->quantity,
                'gender_preference' => $position->gender_preference,
                'minimum_age' => $position->minimum_age,
                'maximum_age' => $position->maximum_age,
                'minimum_experience_years' => $position->minimum_experience_years,
                'qualification' => $position->qualification,
                'required_skills' => $position->required_skills,
                'trade_test' => $position->trade_test,
                'salary' => [
                    'amount' => $position->salary_amount,
                    'currency' => $position->salary_currency,
                    'period' => $position->salary_period,
                ],
                'working_hours_per_day' => $position->working_hours_per_day,
                'working_days_per_week' => $position->working_days_per_week,
                'contract_duration_months' => $position->contract_duration_months,
                'benefits' => [
                    'accommodation' => $position->accommodation_provided,
                    'food' => $position->food_provided,
                    'transport' => $position->transport_provided,
                    'medical' => $position->medical_provided,
                    'insurance' => $position->insurance_provided,
                    'airfare' => $position->airfare_provided,
                    'visa_cost' => $position->visa_cost_covered,
                    'other' => $position->other_benefits,
                ],
                'required_documents' => $position->required_documents ?? [],
                'is_converted' => $position->isConverted(),
                'job_demand' => $position->relationLoaded('jobDemand') && $position->jobDemand ? [
                    'id' => $position->jobDemand->uuid,
                    'demand_code' => $position->jobDemand->demand_code,
                ] : null,
            ])),

            'employer' => $this->whenLoaded('employer', fn () => $this->employer ? [
                'id' => $this->employer->uuid,
                'company_name' => $this->employer->company_name,
                'can_accept_new_demand' => $this->employer->canAcceptNewDemand(),
            ] : null),
            'assignee' => $this->whenLoaded('assignee', fn () => $this->assignee?->name),

            'submitted_at' => $this->submitted_at?->toIso8601String(),
            'reviewed_at' => $this->reviewed_at?->toIso8601String(),
            'approved_at' => $this->approved_at?->toIso8601String(),
            'converted_at' => $this->converted_at?->toIso8601String(),

            // Only loaded for readers holding document.view (see the controller).
            'documents' => DocumentResource::collection($this->whenLoaded('documents')),

            'history' => $this->whenLoaded('statusHistories', fn () => $this->statusHistories->map(fn ($entry) => [
                'from_status' => $entry->from_status?->label(),
                'to_status' => $entry->to_status->label(),
                'reason' => $entry->reason,
                'information_requested' => $entry->information_requested,
                'changed_at' => $entry->changed_at?->toIso8601String(),
                'changed_by' => $entry->relationLoaded('changedBy') ? $entry->changedBy?->name : null,
            ])),
        ];
    }
}
