<?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\Enums\OfferStatus;
use Maestro\Core\Modules\Offers\Models\Offer;
use Maestro\Core\Modules\Offers\Services\OfferDocumentBuilder;

/** @property Offer $resource */
final class OfferPublicResource extends JsonResource
{
    /**
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        $doc = OfferDocumentBuilder::build($this->resource);

        $status = $this->resource->status instanceof OfferStatus
            ? $this->resource->status
            : OfferStatus::tryFrom((string) $this->resource->status);

        $canRespond = $status !== null
            && in_array($status, [OfferStatus::Sent, OfferStatus::Viewed], true);

        return [
            ...$doc,
            'interaction' => [
                'can_accept' => $canRespond,
                'can_decline' => $canRespond,
            ],
        ];
    }
}
