<?php

declare(strict_types=1);

namespace Maestro\Core\Modules\Offers\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Maestro\Core\Modules\Offers\Models\Offer;

/** @property Offer $resource */
final class OfferListResource extends JsonResource
{
    /**
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'offer_number' => $this->offer_number,
            'subject' => $this->subject,
            'status' => $this->status?->value ?? (string) $this->status,
            'valid_until' => optional($this->valid_until)?->format('Y-m-d'),
            'vat_total' => (float) $this->vat_total,
            'is_template' => (bool) $this->is_template,
            'template_name' => $this->template_name,
            'updated_at' => optional($this->updated_at)?->toIso8601String(),

            'customer' => $this->whenLoaded('customer', function () {
                if (! $this->customer) {
                    return null;
                }

                return [
                    'id' => $this->customer->id,
                    'name' => $this->customer->name,
                ];
            }),
        ];
    }
}
