<?php

namespace App\Http\Resources\Public;

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

/**
 * A job demand as the public website sees it (§162).
 *
 * This is an explicit allow-list, not a filter: agency margin, employer
 * commercial rates, internal cost and commission are simply not assembled here,
 * and the employer name only appears when the demand says it may.
 */
class PublicJobResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'slug' => $this->public_slug,
            'title' => $this->job_title,
            'occupation' => $this->occupation,
            'country' => $this->country,
            'city' => $this->city,
            'category' => $this->whenLoaded('jobCategory', fn () => $this->jobCategory ? [
                'slug' => $this->jobCategory->slug,
                'name' => $this->jobCategory->name,
            ] : null),

            // Named only where the employer has agreed to be public.
            'employer_name' => $this->show_employer_publicly
                ? $this->whenLoaded('employer', fn () => $this->employer?->company_name)
                : null,

            'vacancies' => $this->quantity_required,
            'is_featured' => (bool) $this->is_featured,
            'is_urgent' => (bool) $this->is_urgent,
            'accepting_applications' => $this->isAcceptingApplications(),

            'salary' => [
                'amount' => $this->salary_amount,
                'currency' => $this->salary_currency,
                'period' => $this->salary_period,
            ],

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

            'application_deadline' => $this->application_deadline?->toDateString(),
            'published_at' => $this->publish_at?->toIso8601String(),

            'summary' => $this->public_summary,
        ];
    }
}
