<?php

declare(strict_types=1);

namespace App\Http\Resources;

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

class OrderResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'order_id' => $this->id,
            'order_number' => $this->order_number,
            'tracking_code' => $this->order_number,
            'user_id' => $this->user_id,
            'producer_id' => $this->producer_id,
            'product_id' => $this->product_id,
            'project_id' => $this->project_id,
            'type' => $this->type,
            'quantity' => $this->quantity,
            'unit' => $this->unit,
            'unit_price' => $this->unit_price !== null ? (int) $this->unit_price : null,
            'total_price' => (int) $this->total_price,
            'payable' => (int) $this->total_price,
            'status' => $this->status,
            'order_date' => $this->order_date?->toISOString(),
            'delivery_date' => $this->delivery_date?->toISOString(),
            'delivery_address' => $this->delivery_address,
            'address' => $this->metadata['address'] ?? [
                'address' => $this->delivery_address,
            ],
            'notes' => $this->notes,
            'metadata' => $this->metadata,
            'payment_method' => $this->metadata['payment_method'] ?? null,
            'group_id' => $this->metadata['group_id'] ?? null,
            'created_at' => $this->created_at?->toISOString(),
            'product' => new ProductResource($this->whenLoaded('product')),
            'producer' => new ProducerResource($this->whenLoaded('producer')),
            'user' => new UserResource($this->whenLoaded('user')),
        ];
    }
}
