<?php

namespace App\Http\Resources;

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

class ProductResource extends JsonResource
{
    /**
     * Transform the resource into an array with complete dual-casing support.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        $primaryUrl = $this->primary_image_url;
        $hoverUrl = $this->hover_image_url;
        $additionalUrls = $this->additional_image_urls ?? [];

        // Build gallery image list combining primary, hover, and additional images
        $gallery = array_values(array_unique(array_merge([$primaryUrl, $hoverUrl], $additionalUrls)));

        $isSoldOut = (bool) ($this->is_sold_out || $this->stock <= 0 || $this->status === 'sold_out');

        return [
            'id' => $this->id,
            'name' => $this->name,
            'product_name' => $this->name,
            'slug' => $this->slug,
            'sku' => $this->sku,
            'product_type' => strtolower($this->product_type ?? 'normal'),
            'productType' => strtolower($this->product_type ?? 'normal'),
            'category' => $this->category->name ?? 'Jewellery',
            'category_id' => $this->category_id,
            'price' => (float) $this->price,
            'selling_price' => (float) $this->price,
            'original_price' => $this->original_price ? (float) $this->original_price : null,
            'originalPrice' => $this->original_price ? (float) $this->original_price : null,
            'mrp' => $this->original_price ? (float) $this->original_price : (float) $this->price,
            'discount' => $this->discount_percentage,
            'discount_percentage' => $this->discount_percentage,
            'stock' => (int) $this->stock,
            'stock_quantity' => (int) $this->stock,
            'status' => $this->status,
            'badge' => $this->badge,
            'is_new' => (bool) $this->is_new,
            'isNew' => (bool) $this->is_new,
            'is_best_seller' => (bool) $this->is_best_seller,
            'isBestSeller' => (bool) $this->is_best_seller,
            'is_featured' => (bool) $this->is_featured,
            'isFeatured' => (bool) $this->is_featured,
            'is_trending' => (bool) $this->is_trending,
            'is_sold_out' => $isSoldOut,
            'is_force_sold_out' => (bool) $this->is_sold_out,
            'available' => (bool) ($this->status === 'active' && $this->stock > 0 && !$this->is_sold_out),
            'short_summary' => $this->short_description,
            'short_description' => $this->short_description,
            'description' => $this->description,
            
            // Complete standard & URL image properties
            'image' => $primaryUrl,
            'primary_image' => $primaryUrl,
            'primary_image_url' => $primaryUrl,
            'hover_image' => $hoverUrl,
            'hoverImage' => $hoverUrl,
            'hover_image_url' => $hoverUrl,
            'additional_images' => $additionalUrls,
            'additional_image_urls' => $additionalUrls,
            
            'images' => [
                'primary' => $primaryUrl,
                'hover' => $hoverUrl,
                'gallery' => $gallery,
            ],
            'created_at' => $this->created_at ? $this->created_at->toIso8601String() : null,
            'updated_at' => $this->updated_at ? $this->updated_at->toIso8601String() : null,
        ];
    }
}
