<?php

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

return new class extends Component
{
    public $slug;

    public function mount($slug)
    {
        $this->slug = $slug;
    }

    public function render()
    {
        $post = Post::where('slug', $this->slug)
            ->where('status', 'published')
            ->with('category')
            ->firstOrFail();

        // Increment view count (Livewire specific: only on initial render or as needed)
        // For simple view counting, we can do it here.
        $post->increment('view_count');

        $relatedPosts = Post::where('category_id', $post->category_id)
            ->where('id', '!=', $post->id)
            ->where('status', 'published')
            ->latest()
            ->take(3)
            ->get();

        $trendingPosts = Post::where('status', 'published')
            ->orderBy('view_count', 'desc')
            ->take(5)
            ->get();

        $metaDesc = $post->meta_description
            ?: Str::limit(strip_tags($post->content), 160);

        $ogImage = $post->featured_image
            ? (Str::startsWith($post->featured_image, 'http') ? $post->featured_image : asset('storage/' . $post->featured_image))
            : null;

        return $this->view([
            'post'          => $post,
            'relatedPosts'  => $relatedPosts,
            'trendingPosts' => $trendingPosts,
        ])->layout('layouts::frontend', [
            'pageTitle'       => $post->meta_title ?: ($post->title . ' | Bikash Barta'),
            'metaDescription' => $metaDesc,
            'ogImage'         => $ogImage,
        ]);
    }

    protected function view($data = [])
    {
        return app('view')->file('D:\02-my freelance\00-DR_Padhi\DR_PADHI\storage\framework/views/livewire/views/449c4f27.blade.php', $data);
    }
};
