<?php

namespace App\Http\Livewire;

use App\Models\Post;
use Illuminate\Support\Facades\App;
use Livewire\Component;

class SearchInfoModal extends Component
{
    public $show = false;
    public $post = null;
    public $image = null;
    public $imageCaption = null;
    public $imageOwner = null;
    public $fallbackTitle;
    public $fallbackDescription;

    protected $listeners = ['openSearchInfoModal' => 'open'];

    public function mount()
    {
        $this->fallbackTitle = __('How search works');
        $this->fallbackDescription = __('The search bar helps you find people, organizations, events and posts. Posts and events are pushed to the top. People or organizations nearby your location get also a higher search ranking. ');
    }

    public function open()
    {
        $this->show = true;
        $this->loadPost();
    }

    public function loadPost()
    {
        $locale = App::getLocale();

        $this->post = Post::with([
            'category',
            'media',
            'translations' => function ($query) use ($locale) {
                $query->where('locale', $locale)
                    ->orderBy('created_at', 'desc')
                    ->limit(3);
            }
        ])
        ->whereHas('category', function ($query) {
            $query->where('type', 'SiteContents\Search\Info');
        })
        ->whereHas('translations', function ($query) use ($locale) {
            $query->where('locale', $locale)
                ->whereDate('from', '<=', now())
                ->where(function ($query) {
                    $query->whereDate('till', '>', now())->orWhereNull('till');
                })
                ->orderBy('updated_at', 'desc');
        })
        ->orderBy('created_at', 'desc')
        ->limit(3)
        ->first();

        if ($this->post && $this->post->hasMedia('posts')) {
            $this->image = $this->post->getFirstMediaUrl('posts', 'half_hero');
            $mediaItem = $this->post->getFirstMedia('posts');
            if ($mediaItem) {
                // Get owner
                $this->imageOwner = $mediaItem->getCustomProperty('owner');

                // Try to get caption for current locale
                $this->imageCaption = $mediaItem->getCustomProperty('caption-' . $locale);

                // If not found, try fallback locales
                if (!$this->imageCaption) {
                    $fallbackLocales = ['en', 'nl', 'de', 'es', 'fr'];
                    foreach ($fallbackLocales as $fallbackLocale) {
                        $this->imageCaption = $mediaItem->getCustomProperty('caption-' . $fallbackLocale);
                        if ($this->imageCaption) {
                            break;
                        }
                    }
                }
            }
        }
    }

    public function close()
    {
        $this->show = false;
        $this->image = null;
        $this->imageCaption = null;
        $this->imageOwner = null;
    }

    public function render()
    {
        return view('livewire.search-info-modal');
    }
}
