<?php

namespace App\Http\Controllers;

use App\Models\Post;
use App\Models\BlogCategory;
use App\Models\Tag;
use Illuminate\Http\Request;

class PostController extends Controller
{
    public function index()
    {
        // Get featured post (latest published post)
        $featuredPost = Post::where('status', 'published')
            ->latest('published_at')
            ->first();

        // Get other posts
        $postsQuery = Post::where('status', 'published')
            ->whereNotNull('published_at');
            
        // Exclude featured post if exists
        if ($featuredPost) {
            $postsQuery->where('id', '!=', $featuredPost->id);
        }
        
        $posts = $postsQuery->latest('published_at')->paginate(8);

        // Get categories with post count
        $categories = BlogCategory::withCount('posts')->having('posts_count', '>', 0)->get();

        // Get recent posts
        $recentPosts = Post::where('status', 'published')
            ->whereNotNull('published_at')
            ->latest('published_at')
            ->take(5)
            ->get();

        // Get popular tags
        $tags = Tag::withCount('posts')->having('posts_count', '>', 0)->orderBy('posts_count', 'desc')->take(10)->get();

        return view('blog.index', compact(
            'featuredPost',
            'posts',
            'categories',
            'recentPosts',
            'tags'
        ));
    }

    public function show($slug)
    {
        $post = Post::where('slug', $slug)
            ->where('status', 'published')
            ->whereNotNull('published_at')
            ->firstOrFail();

        // Get related posts
        $relatedPosts = Post::where('id', '!=', $post->id)
            ->where('status', 'published')
            ->whereNotNull('published_at')
            ->where(function ($query) use ($post) {
                // Related by category
                if ($post->blog_category_id) {
                    $query->where('blog_category_id', $post->blog_category_id);
                }
                
                // Related by tags
                if ($post->tags->count() > 0) {
                    $tagIds = $post->tags->pluck('id')->toArray();
                    $query->orWhereHas('tags', function ($q) use ($tagIds) {
                        $q->whereIn('tags.id', $tagIds);
                    });
                }
            })
            ->latest('published_at')
            ->take(4)
            ->get();

        return view('blog.show', compact('post', 'relatedPosts'));
    }

    public function category($slug)
    {
        $category = BlogCategory::where('slug', $slug)->firstOrFail();
        
        $posts = Post::where('blog_category_id', $category->id)
            ->where('status', 'published')
            ->whereNotNull('published_at')
            ->latest('published_at')
            ->paginate(8);

        // Get categories with post count
        $categories = BlogCategory::withCount('posts')->having('posts_count', '>', 0)->get();

        // Get recent posts
        $recentPosts = Post::where('status', 'published')
            ->whereNotNull('published_at')
            ->latest('published_at')
            ->take(5)
            ->get();

        // Get popular tags
        $tags = Tag::withCount('posts')->having('posts_count', '>', 0)->orderBy('posts_count', 'desc')->take(10)->get();

        return view('blog.category', compact(
            'category',
            'posts',
            'categories',
            'recentPosts',
            'tags'
        ));
    }

    public function tag($slug)
    {
        $tag = Tag::where('slug', $slug)->firstOrFail();
        
        $posts = $tag->posts()
            ->where('status', 'published')
            ->whereNotNull('published_at')
            ->latest('published_at')
            ->paginate(8);

        // Get categories with post count
        $categories = BlogCategory::withCount('posts')->having('posts_count', '>', 0)->get();

        // Get recent posts
        $recentPosts = Post::where('status', 'published')
            ->whereNotNull('published_at')
            ->latest('published_at')
            ->take(5)
            ->get();

        // Get popular tags
        $tags = Tag::withCount('posts')->having('posts_count', '>', 0)->orderBy('posts_count', 'desc')->take(10)->get();

        return view('blog.tag', compact(
            'tag',
            'posts',
            'categories',
            'recentPosts',
            'tags'
        ));
    }
}