<?php

declare(strict_types=1);

namespace App\Http\Resources;

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

class ProjectResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'code' => $this->code,
            'name' => $this->name,
            'excerpt' => $this->excerpt,
            'desc' => $this->desc,
            'status' => $this->status,
            'user_id' => $this->user_id,
            'producer_id' => $this->producer_id,
            'category_id' => $this->category_id,
            'product_id' => $this->product_id,
            'breed_id' => $this->breed_id,
            'quantity' => $this->quantity,
            'unit' => $this->unit,
            'unit_price' => $this->unit_price !== null ? (int) $this->unit_price : null,
            'start_date' => $this->start_date?->toISOString(),
            'end_date' => $this->end_date?->toISOString(),
            'deadline' => $this->deadline?->toISOString(),
            'presale_start' => $this->presale_start?->toDateString(),
            'presale_end' => $this->presale_end?->toDateString(),
            'budget' => $this->budget !== null ? (int) $this->budget : null,
            'budgets' => $this->budgets,
            'estimated_profit' => $this->estimated_profit !== null ? (int) $this->estimated_profit : null,
            'contract_text' => $this->contract_text,
            'sale_registration_enabled' => (bool) $this->sale_registration_enabled,
            'sale_deactivation_at' => $this->sale_deactivation_at?->toISOString(),
            'status_label' => \App\Models\Project::statusOptions()[(string) $this->status] ?? $this->status,
            'images' => $this->formatImages($this->images),
            'province_id' => $this->province_id,
            'county_id' => $this->county_id,
            'address' => $this->address,
            'view_count' => $this->view_count,
            'request_count' => $this->request_count,
            'project_request_id' => $this->project_request_id ?? null,
            'created_at' => $this->created_at?->toISOString(),
            'updated_at' => $this->updated_at?->toISOString(),
            'user' => new UserResource($this->whenLoaded('user')),
            'producer' => new ProducerResource($this->whenLoaded('producer')),
            'category' => new CategorySimpleResource($this->whenLoaded('category')),
            'product' => new ProductResource($this->whenLoaded('product')),
            'breed' => new BreedResource($this->whenLoaded('breed')),
            'contract_signatures' => $this->whenLoaded('contractSignatures', fn () => $this->contractSignatures->map(fn ($s) => [
                'id' => $s->id,
                'user_id' => $s->user_id,
                'role' => $s->role,
                'signed_at' => $s->signed_at?->toISOString(),
            ])->values()),
            'completion' => $this->whenLoaded('completion', fn () => $this->completion ? [
                'started_at' => $this->completion->started_at?->toISOString(),
                'producer_confirmed_at' => $this->completion->producer_confirmed_at?->toISOString(),
                'employer_confirmed_at' => $this->completion->employer_confirmed_at?->toISOString(),
                'finalized_at' => $this->completion->finalized_at?->toISOString(),
            ] : null),
        ];
    }

    protected function formatImages(?array $images): ?array
    {
        if ($images === null) {
            return null;
        }

        return array_map(
            fn ($image) => is_string($image) && ! str_starts_with($image, 'http') ? url($image) : $image,
            $images
        );
    }
}
