<?php

namespace App\Http\Livewire\Welcome;

use App\Models\Category;
use App\Models\News;
use App\Models\Post;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\App;
use Livewire\Component;

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

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

        // Get news posts from all locations for welcome page
        $post = Post::with([
                'postable' => function ($query) {
                    $query->select(['id', 'name']);
                },
                'category' => function ($query) {
                    $query->where('type', News::class)->with('categoryable.translations');
                },
                'translations' => function ($query) {
                    $query->where('locale', App::getLocale());
                },
                'author',
                'media',
            ])
            ->whereHas('category', function ($query) {
                $query->where('type', News::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()->sortByDesc(function ($query) {
                if (isset($query->translations)) {
                    return $query->translations->first()->updated_at;
                };
            })->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(strtotime($translation->updated_at))->isoFormat('LL');
            $this->post['slug'] = $translation->slug;
            $this->post['from'] = $translation->from;

            $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->author ? $post->author->name : timebank_config('posts.site-content-writer');
            $this->post['id'] = $post->id;
            $this->post['model'] = get_class($post);
            $this->post['like_count'] = $post->like_count ?? 0;

            // Prepend location name to excerpt if available
            $excerpt = $translation->excerpt;
            if ($post->category && $post->category->categoryable && $post->category->categoryable->translations) {
                $locationTranslation = $post->category->categoryable->translations->where('locale', App::getLocale())->first();
                if ($locationTranslation && $locationTranslation->name) {
                    $locationName = strtoupper($locationTranslation->name);
                    $excerpt = $locationName . ' - ' . $excerpt;
                }
            }
            $this->post['excerpt'] = $excerpt;

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

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