<?php

namespace App\Http\Livewire\Welcome;

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

class ArticleCardFull extends Component
{
    public $author;
    public $post = [];
    public $posts;
    public $media;
    public $postNr;
    public bool $random = false;

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

        if ($random) {
            $this->random = true;
        }

        // Get article posts from all locations for welcome page
        $post = Post::with([
                'postable' => function ($query) {
                    $query->select(['id', 'name']);
                },
                'category' => function ($query) {
                    $query->where('type', 'App\Models\Article');
                },
                'translations' => function ($query) {
                    $query->where('locale', App::getLocale());
                },
                'author',
                'media',
            ])
            ->whereHas('category', function ($query) {
                $query->where('type', 'App\Models\Article');
            })
            ->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');
            });

        // Apply random or sorted ordering
        if ($this->random) {
            $post = $post->inRandomOrder()->get();
        } else {
            $post = $post->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['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;

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

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