<?php

namespace App\Http\Livewire\Welcome;

use App\Models\Category;
use App\Models\Locations\City;
use App\Models\Locations\Country;
use App\Models\Locations\Division;
use App\Models\Locations\Location;
use App\Models\Meeting;
use App\Models\Post;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\App;
use Livewire\Component;

class EventCardFull extends Component
{
    public $author;
    public $post = [];
    public $posts;
    public $media;
    public $postNr;

    public function mount($postNr = 0, Request $request)
    {
        $this->postNr = $postNr;

        // Get upcoming events from all locations for welcome page
        $post = Post::with([
                'postable' => function ($query) {
                    $query->select(['id', 'name']);
                },
                'category' => function ($query) {
                    $query->where('type', Meeting::class);
                },
                'translations' => function ($query) {
                    $query->where('locale', App::getLocale());
                },
                'meeting',
                'media',
            ])
            ->whereHas('category', function ($query) {
                $query->where('type', Meeting::class);
            })
            ->whereHas('translations', function ($query) {
                $query
                    ->where('locale', App::getLocale())
                    ->whereDate('from', '<=', now())
                    ->where(function ($query) {
                        $query->whereDate('till', '>', now())->orWhereNull('till');
                    })
                    ->orderBy('updated_at', 'desc');
            })
            ->get()->sortBy(function ($query) {
                if (isset($query->meeting->from)) {
                    return $query->meeting->from;
                };
            })->values();       // Use values() method to reset the collection keys after sortBy

        $lastNr = $post->count() - 1;

        if ($postNr > $lastNr || $post->isEmpty()) {
            $post = null;
            $this->post = null;
        } else {
            $post = $post[$postNr];
        }

        if ($post && isset($post->translations) && $post->translations->isNotEmpty()) {
            $translation = $post->translations->first();
            $this->post = $translation;
            $this->post['start'] = Carbon::parse($translation->updated_at)->isoFormat('LL');
            $this->post['slug'] = $translation->slug;

            $category = Category::find($post->category_id);
            $categoryTranslation = $category ? $category->translations->where('locale', App::getLocale())->first() : null;
            $this->post['category'] = $categoryTranslation ? $categoryTranslation->name : '';

            $this->post['author'] = $post->postable ? $post->postable->name : '';
            $this->post['id'] = $post->id;
            $this->post['model'] = get_class($post);
            $this->post['like_count'] = $post->like_count ?? 0;

            if ($post->meeting) {
                $this->post['address'] = ($post->meeting->address) ? $post->meeting->address : '';
                $this->post['from'] = ($post->meeting->from) ? $post->meeting->from : '';
                $this->post['venue'] = ($post->meeting->venue) ? $post->meeting->venue : '';
                $this->post['city'] = $post->meeting->location?->first()?->city?->translations?->first()?->name ?? '';
                $this->post['price'] = ($post->meeting->price) ? tbFormat($post->meeting->price) : '';
            }

            if ($post->media && $post->media->isNotEmpty()) {
                $this->media = Post::find($post->id)->getFirstMedia('posts');
            }
        }
    }

    public function render()
    {
        return view('livewire.welcome.event-card-full');
    }
}
