<?php

namespace App\Http\Resources\Package;

use App\Http\Resources\UserResource;
use App\Http\Resources\Contractor\ContractorResource;
use App\Http\Resources\Contract\ContractResource;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

/**
 * Package Resource
 *
 * Transform package model to API response
 */
class PackageResource extends JsonResource
{
    /**
     * Transform the resource into an array
     */
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'job_type' => $this->job_type,
            'job_type_label' => $this->job_type === 'konstruksi' ? 'Konstruksi' : 'Konsultasi',
            'sub_classification' => $this->sub_classification,
            'budget' => $this->budget,
            'budget_formatted' => $this->budget_formatted,
            'province' => $this->province,
            'regency' => $this->regency,
            'district' => $this->district,
            'village' => $this->village,
            'location' => $this->location,
            'duration_days' => $this->duration_days,
            'description' => $this->description,
            'status' => $this->status,
            'status_label' => $this->status_label,
            'status_color' => $this->status_color,
            'is_open' => $this->isOpen(),
            'matched_at' => $this->matched_at?->format('d/m/Y H:i'),
            'created_at' => $this->created_at->format('d/m/Y H:i'),
            'updated_at' => $this->updated_at->format('d/m/Y H:i'),

            // Relationships
            'ppk' => new UserResource($this->whenLoaded('ppk')),
            'selected_contractor' => new ContractorResource($this->whenLoaded('selectedContractor')),
            'contract' => new ContractResource($this->whenLoaded('contract')),
            'documents' => JobDocumentResource::collection($this->whenLoaded('documents')),

            // Counts (eager loaded with withCount)
            'interests_count' => $this->whenCounted('interests'),
            'documents_count' => $this->whenCounted('documents'),
            'progress_count' => $this->whenCounted('progress'),
        ];
    }
}