<?php

namespace App\Http\Resources\Api;

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

class EmpletteResource extends JsonResource
{
    /** @return array<string, mixed> */
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'status' => $this->status,
            'estimated_total' => (float) $this->estimated_total,
            'final_total' => (float) $this->final_total,
            'currency' => $this->currency,
            // Note admin affichée côté mobile / admin.
            'admin_notes' => $this->admin_notes,
            // Note envoyée par le client à la création.
            'client_notes' => $this->client_notes,
            // Compat : ancien champ unique (on renvoie la note admin).
            'notes' => $this->admin_notes ?? $this->notes,
            'user' => $this->whenLoaded('user', fn () => new UserResource($this->user)),
            'order_id' => $this->order_id,
            'order_payment_status' => $this->whenLoaded('order', fn () => $this->order?->payment_status),
            'quote_sent_at' => $this->quote_sent_at?->toIso8601String(),
            'items' => EmpletteItemResource::collection($this->whenLoaded('items')),
            'logs' => EmpletteLogResource::collection($this->whenLoaded('logs')),
            'created_at' => $this->created_at?->toIso8601String(),
            'updated_at' => $this->updated_at?->toIso8601String(),
        ];
    }
}

