<?php

namespace App\Livewire;

use App\Models\Park;
use App\Models\Photo;
use Livewire\Component;
use Illuminate\Support\Facades\Log;

class PhotoGalleryComponent extends Component
{
    public Park $park;
    public $photos = [];
    public $featuredPhoto = null;
    public $selectedPhoto = null;
    public $isLoading = true;
    public $error = null;
    public $viewMode = 'grid'; // grid or carousel

    protected $listeners = ['photoUploaded' => 'loadPhotos'];

    public function mount(Park $park)
    {
        $this->park = $park;
        $this->loadPhotos();
    }

    public function loadPhotos()
    {
        $this->isLoading = true;
        $this->error = null;

        try {
            $this->photos = $this->park->photos()->ordered()->get();
            $this->featuredPhoto = $this->park->featuredPhoto();
            $this->isLoading = false;
        } catch (\Exception $e) {
            Log::error('Error loading photos: ' . $e->getMessage());
            $this->error = 'Failed to load photos: ' . $e->getMessage();
            $this->isLoading = false;
        }
    }

    public function selectPhoto($photoId)
    {
        $this->selectedPhoto = collect($this->photos)->firstWhere('id', $photoId);
    }

    public function closePhotoDetail()
    {
        $this->selectedPhoto = null;
    }

    public function setFeatured($photoId)
    {
        try {
            $photo = Photo::findOrFail($photoId);
            $this->park->setFeaturedPhoto($photo);
            $this->loadPhotos();
            $this->dispatch('notify', [
                'type' => 'success',
                'message' => 'Featured photo updated successfully'
            ]);
        } catch (\Exception $e) {
            Log::error('Error setting featured photo: ' . $e->getMessage());
            $this->dispatch('notify', [
                'type' => 'error',
                'message' => 'Failed to set featured photo'
            ]);
        }
    }

    public function deletePhoto($photoId)
    {
        try {
            $photo = Photo::findOrFail($photoId);
            
            // Make API request to the PhotoController
            app(\App\Http\Controllers\PhotoController::class)->destroy($photo);
            
            $this->loadPhotos();
            $this->dispatch('notify', [
                'type' => 'success',
                'message' => 'Photo deleted successfully'
            ]);
            
            if ($this->selectedPhoto && $this->selectedPhoto->id === $photoId) {
                $this->selectedPhoto = null;
            }
        } catch (\Exception $e) {
            Log::error('Error deleting photo: ' . $e->getMessage());
            $this->dispatch('notify', [
                'type' => 'error',
                'message' => 'Failed to delete photo: ' . $e->getMessage()
            ]);
        }
    }

    public function toggleViewMode()
    {
        $this->viewMode = $this->viewMode === 'grid' ? 'carousel' : 'grid';
    }

    public function render()
    {
        return view('livewire.photo-gallery-component');
    }
}