<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Storage;

class EventResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [
            'id'               => $this->id,
            'name'             => $this->name,
            'slug'             => $this->slug,
            'destination'      => $this->destination,
            'country'          => $this->country,
            'description'      => $this->description,
            'type'             => $this->type,
            'start_date'       => $this->start_date->toDateString(),
            'end_date'         => $this->end_date->toDateString(),
            'duration_days'    => $this->duration_days,
            'price'            => $this->price,
            'original_price'   => $this->original_price,
            'discount_percent' => $this->discount_percent,
            'max_spots'        => $this->max_spots,
            'spots_remaining'  => $this->spots_remaining,
            'status'           => $this->status,
            'hero_image_url'   => $this->hero_image_path
                                    ? Storage::url($this->hero_image_path)
                                    : null,
            'gallery_urls'     => collect($this->gallery_images ?? [])
                                    ->map(fn ($p) => Storage::url($p))
                                    ->values(),
            'highlights'       => $this->highlights ?? [],
            'rating'           => $this->rating,
            'review_count'     => $this->review_count,
            'is_featured'      => $this->is_featured,
            'volunteer_enabled'=> $this->volunteer_enabled,

            // Present only when the authenticated user's wishlist is eager-loaded
            'is_wishlisted'    => $this->when(
                                    isset($this->is_wishlisted),
                                    fn () => (bool) $this->is_wishlisted
                                  ),
        ];
    }
}
