<?php

declare(strict_types=1);

namespace App\Http\Resources;

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

class ProductResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        $price = (int) ($this->total_price ?? $this->price ?? 0);

        return [
            'id' => $this->id,
            'slug' => $this->slug ?? ('product-' . $this->id),
            'code' => $this->code ?? ('RV-' . str_pad((string) $this->id, 4, '0', STR_PAD_LEFT)),
            'name' => $this->name,
            'title' => $this->name,
            'excerpt' => $this->excerpt,
            'desc' => $this->desc,
            'description' => $this->desc,
            'image' => $this->image ? url($this->image) : null,
            'gallery' => $this->formatFiles($this->gallery),
            'price' => $price,
            'total_price' => $price,
            'price_per_unit' => $this->price_per_unit !== null ? (int) $this->price_per_unit : null,
            'sale_price' => null,
            'is_discounted' => false,
            'age' => $this->age,
            'gender' => $this->gender,
            'weight' => $this->weight,
            'weight_unit' => $this->weight_unit,
            'count' => $this->count,
            'unit' => $this->unit,
            'status' => $this->status,
            'is_active' => (bool) $this->is_active,
            'is_featured' => (bool) $this->is_featured,
            'view_count' => $this->view_count,
            'rating_average' => $this->rating_average !== null ? (float) $this->rating_average : null,
            'rating_count' => $this->rating_count,
            'comment_count' => $this->comment_count,
            'producer_id' => $this->producer_id,
            'category_id' => $this->category_id,
            'breed_id' => $this->breed_id,
            'category_slug' => $this->whenLoaded('category', fn () => $this->category?->slug ?? $this->category?->url),
            'breed_name' => $this->whenLoaded('breed', fn () => $this->breed?->name),
            'producer_name' => $this->whenLoaded('producer', fn () => $this->producer?->brand_name),
            'address' => $this->address,
            'created_at' => $this->created_at?->toISOString(),
            'updated_at' => $this->updated_at?->toISOString(),
            'producer' => new ProducerResource($this->whenLoaded('producer')),
            'category' => new CategoryResource($this->whenLoaded('category')),
            'breed' => new BreedResource($this->whenLoaded('breed')),
            'province' => $this->whenLoaded('province', fn () => [
                'id' => $this->province?->id,
                'name' => $this->province?->name,
            ]),
            'county' => $this->whenLoaded('county', fn () => [
                'id' => $this->county?->id,
                'name' => $this->county?->name,
            ]),
            'comments' => CommentResource::collection($this->whenLoaded('comments')),
            'ratings' => RatingResource::collection($this->whenLoaded('ratings')),
        ];
    }

    private function formatFiles(?array $files): ?array
    {
        if ($files === null) {
            return null;
        }

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