<?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;
use Maestro\Core\Modules\Offers\Services\OfferTotalsService;

/** @property Offer $resource */
final class OfferResource extends JsonResource
{
    /**
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        $loaded = $this->relationLoaded('sections');
        $totals = OfferTotalsService::summarize($this->resource);

        return [
            'id' => $this->id,
            'offer_number' => $this->offer_number,
            'subject' => $this->subject,
            'customer_id' => $this->customer_id,
            'contact_id' => $this->contact_id,
            'intro_html' => $this->intro_html,
            'valid_until' => optional($this->valid_until)?->format('Y-m-d'),
            'discount_type' => $this->discount_type?->value ?? (string) $this->discount_type,
            'discount_value' => (float) $this->discount_value,
            'vat_total' => (float) $this->vat_total,
            'status' => $this->status?->value ?? (string) $this->status,
            'public_uuid' => $this->public_uuid,
            'sent_at' => optional($this->sent_at)?->toIso8601String(),
            'viewed_at' => optional($this->viewed_at)?->toIso8601String(),
            'signed_at' => optional($this->signed_at)?->toIso8601String(),
            'declined_at' => optional($this->declined_at)?->toIso8601String(),
            'decline_reason' => $this->decline_reason,
            'signer_name' => $this->signer_name,
            'is_template' => (bool) $this->is_template,
            'template_name' => $this->template_name,
            'hour_project_id' => $this->hour_project_id,

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

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

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

                return [
                    'id' => $this->contactPerson->id,
                    'first_name' => $this->contactPerson->first_name,
                    'last_name' => $this->contactPerson->last_name,
                    'full_name' => trim($this->contactPerson->first_name.' '.$this->contactPerson->last_name),
                    'email' => $this->contactPerson->email,
                ];
            }),

            'sections' => OfferSectionResource::collection($loaded ? $this->sections : []),
            'totals' => $totals,

            'created_at' => optional($this->created_at)?->toIso8601String(),
            'updated_at' => optional($this->updated_at)?->toIso8601String(),
        ];
    }
}
