<?php

namespace App\Domains\Event\Resources;

use App\Domains\Tenant\Models\Tenant;
use App\Domains\Ticket\Resources\ValidityTimeResource;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

/** @mixin Tenant */
class EventResource extends JsonResource
{
    /** @return array<string, mixed> */
    public function toArray(Request $request): array
    {
        $socialMedia = $this->socialMedia->keyBy('code');

        return [
            'id' => $this->id,
            'title' => $this->event_title,
            'location' => $this->event_location,
            'dates' => $this->eventDates->map(fn ($eventDate): array => [
                'id' => $eventDate->id,
                'validity_time_id' => $eventDate->validity_time_id,
                'validity_time' => ValidityTimeResource::make($eventDate->validityTime),
                'date' => $eventDate->date->format('Y-m-d'),
                'start_time' => substr($eventDate->time_start, 0, 5),
                'end_time' => substr($eventDate->time_end, 0, 5),
            ])->values(),
            'social_media' => $this->socialMedia->map(fn ($item): array => [
                'code' => $item->code,
                'url' => $item->pivot->url,
                'orden' => $item->pivot->orden,
            ])->values(),
            'contact' => [
                'whatsapp_url' => $socialMedia->get('whatsapp')?->pivot->url,
                'instagram_url' => $socialMedia->get('instagram')?->pivot->url,
                'facebook_url' => $socialMedia->get('facebook')?->pivot->url,
            ],
        ];
    }
}
