<?php

namespace App\Http\Resources;

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

class JobDemandResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->uuid,
            'demand_code' => $this->demand_code,
            'job_title' => $this->job_title,
            'country' => $this->country,
            'city' => $this->city,
            'industry' => $this->industry,
            'occupation' => $this->occupation,

            '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(),
            ),
            'terms_editable' => $this->status->allowsTermsEditing(),
            'accepts_candidates' => $this->acceptsCandidates(),
            'intake_blockers' => $this->intakeBlockers(),

            // §15 "Track quantity".
            'pipeline' => [
                'required' => $this->quantity_required,
                'shortlisted' => $this->shortlisted_count,
                'submitted' => $this->submitted_count,
                'interviewed' => $this->interviewed_count,
                'selected' => $this->selected_count,
                'visa_approved' => $this->visa_approved_count,
                'deployed' => $this->deployed_count,
                'remaining' => $this->remainingPositions(),
                'over_selected' => $this->isOverSelected(),
                'fully_deployed' => $this->isFullyDeployed(),
            ],

            'gender_requirement' => $this->gender_requirement,
            'minimum_age' => $this->minimum_age,
            'maximum_age' => $this->maximum_age,

            'salary_amount' => $this->salary_amount,
            'salary_currency' => $this->salary_currency,
            'salary_period' => $this->salary_period,
            'overtime_details' => $this->overtime_details,
            'working_hours_per_day' => $this->working_hours_per_day,
            'working_days_per_week' => $this->working_days_per_week,
            'weekly_holiday' => $this->weekly_holiday,

            'benefits' => [
                'accommodation' => (bool) $this->accommodation_provided,
                'food' => (bool) $this->food_provided,
                'transport' => (bool) $this->transport_provided,
                'medical' => (bool) $this->medical_provided,
                'insurance' => (bool) $this->insurance_provided,
                'airfare_terms' => $this->airfare_terms,
                'notes' => $this->benefits_notes,
            ],

            'contract_duration_months' => $this->contract_duration_months,
            'probation_months' => $this->probation_months,
            'qualification' => $this->qualification,
            'required_experience_years' => $this->required_experience_years,
            'language_requirements' => $this->language_requirements,
            'skills' => $this->skills,
            'employment_terms' => $this->employment_terms,

            'application_deadline' => $this->application_deadline?->toDateString(),
            'deadline_passed' => $this->hasPassedDeadline(),
            'interview_date' => $this->interview_date?->toDateString(),
            'deployment_target_date' => $this->deployment_target_date?->toDateString(),
            'approved_at' => $this->approved_at?->toIso8601String(),

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

            // Agency-side commercials are not for every reader.
            $this->mergeWhen($request->user()?->can('job_demand.update'), fn () => [
                'recruitment_fee_amount' => $this->recruitment_fee_amount,
                'processing_cost_amount' => $this->processing_cost_amount,
                'fee_currency' => $this->fee_currency,
                'notes' => $this->notes,
            ]),

            'created_at' => $this->created_at?->toIso8601String(),
            'updated_at' => $this->updated_at?->toIso8601String(),
        ];
    }
}
