<?php

namespace App\Http\Resources;

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

class ProductResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'slug' => $this->slug,
            'description' => $this->description,
            'short_description' => $this->short_description,
            'price' => (float) $this->price,
            'compare_price' => $this->compare_price ? (float) $this->compare_price : null,
            'stock_quantity' => $this->stock_quantity,
            'track_stock' => $this->track_stock,
            'has_variants' => ($this->variants_count ?? ($this->relationLoaded('variants') ? $this->variants->count() : 0)) > 0,
            'allow_preorder' => $this->allow_preorder,
            'preorder_note' => $this->preorder_note,
            'preorder_days' => $this->preorder_days,
            'status' => $this->status,
            'is_featured' => $this->is_featured,
            'is_bestseller' => $this->is_bestseller,
            'is_in_stock' => $this->is_in_stock,
            'weight_grams' => $this->weight_grams,
            'loyalty_points_earn' => $this->loyalty_points_earn,
            'meta_title' => $this->meta_title,
            'meta_description' => $this->meta_description,
            'categories' => CategoryResource::collection($this->whenLoaded('categories')),
            'variants' => $this->whenLoaded('variants', fn() => $this->variants->map(fn($v) => [
                'id' => $v->id,
                'name' => $v->name,
                'sku' => $v->sku,
                'price' => (float) $v->price,
                'stock_quantity' => $v->stock_quantity,
                'attributes' => $v->attributes,
                'is_in_stock' => $v->is_in_stock,
            ])),
            'images' => $this->whenLoaded('images', fn() => $this->images->map(fn($img) => [
                'id' => $img->id,
                'url' => $img->url,
                'alt_text' => $img->alt_text ?? $this->name,
                'is_primary' => $img->is_primary,
                'sort_order' => $img->sort_order,
            ])),
            'primary_image' => $this->whenLoaded('primaryImage', fn() => $this->primaryImage ? [
                'url' => $this->primaryImage->url,
                'alt_text' => $this->primaryImage->alt_text ?? $this->name,
            ] : null),
            'lab_report' => $this->whenLoaded('activeLabReport', fn() => $this->activeLabReport ? [
                'id' => $this->activeLabReport->id,
                'batch_number' => $this->activeLabReport->batch_number,
                'test_date' => $this->activeLabReport->test_date->format('Y-m-d'),
                'lab_name' => $this->activeLabReport->lab_name,
                'test_type' => $this->activeLabReport->test_type,
                'purity_percentage' => (float) $this->activeLabReport->purity_percentage,
                'pdf_url' => $this->activeLabReport->pdf_url,
                'certificate_number' => $this->activeLabReport->certificate_number,
            ] : null),
            'created_at' => $this->created_at->toISOString(),
        ];
    }
}
