<?php

namespace App\Http\Resources\Tenant;

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

class EventTypeResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        $nameTranslations = is_array($this->name) ? $this->name : [];
        $fallbackName = is_string($this->name) ? $this->name : '';
        $localizedName = $nameTranslations[app()->getLocale()] ?? $nameTranslations['en'] ?? reset($nameTranslations) ?? $fallbackName;

        return [
            'id' => $this->id,
            'slug' => $this->slug,
            'name' => $localizedName,
            'name_translations' => $nameTranslations,
            'icon' => $this->icon,
            'settings' => $this->settings ?? [],
            'created_at' => $this->created_at?->toISOString(),
            'updated_at' => $this->updated_at?->toISOString(),
        ];
    }
}
