<?php

namespace App\Domains\Ticket\Resources;

use App\Domains\Ticket\Enums\ValidityTimeType;
use App\Domains\Ticket\Models\ValidityTime;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

/** @mixin ValidityTime */
class ValidityTimeResource extends JsonResource
{
    /** @return array<string, mixed> */
    public function toArray(Request $request): array
    {
        $fields = match ($this->type) {
            ValidityTimeType::TimeWindow => [
                'start_time' => $this->start_time,
                'end_time' => $this->end_time,
            ],
            ValidityTimeType::FixedWindow => [
                'fixed_starts_at' => $this->fixed_starts_at,
                'fixed_expires_at' => $this->fixed_expires_at,
            ],
        };

        return [
            'id' => $this->id,
            'type' => $this->type->value,
            'is_valid' => $this->isValid(),
            ...array_filter($fields, fn (mixed $value): bool => $value !== null),
        ];
    }
}
