<?php

namespace App\Http\Controllers;

use App\Models\Category;
use App\Models\Post;
use App\Models\Tag;
use Illuminate\Http\Request;
use Illuminate\View\View;

class BlogController extends Controller
{
    /**
     * 博客首页
     */
    public function index(Request $request): View
    {
        $featuredPosts = Post::published()
            ->featured()
            ->with(['category', 'user', 'tags'])
            ->latest('published_at')
            ->take(3)
            ->get();

        $posts = Post::published()
            ->with(['category', 'user', 'tags'])
            ->latest('published_at')
            ->paginate(12);

        $categories = Category::active()
            ->ordered()
            ->withCount('publishedPosts')
            ->get();

        return view('blog.index', compact('featuredPosts', 'posts', 'categories'));
    }

    /**
     * 文章详情
     */
    public function show(Post $post): View
    {
        // 只显示已发布的文章
        abort_unless($post->isPublished(), 404);

        // 增加浏览量
        $post->incrementViews();

        // 加载关联
        $post->load(['category', 'user', 'tags']);

        // 获取相关文章
        $relatedPosts = Post::published()
            ->where('id', '!=', $post->id)
            ->where(function ($query) use ($post) {
                $query->where('category_id', $post->category_id)
                    ->orWhereHas('tags', function ($q) use ($post) {
                        $q->whereIn('tags.id', $post->tags->pluck('id'));
                    });
            })
            ->with(['category', 'user'])
            ->latest('published_at')
            ->take(4)
            ->get();

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

    /**
     * 分类文章列表
     */
    public function category(Category $category): View
    {
        abort_unless($category->is_active, 404);

        $posts = $category->publishedPosts()
            ->with(['user', 'tags'])
            ->latest('published_at')
            ->paginate(12);

        $categories = Category::active()
            ->ordered()
            ->withCount('publishedPosts')
            ->get();

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

    /**
     * 标签文章列表
     */
    public function tag(Tag $tag): View
    {
        $posts = $tag->publishedPosts()
            ->with(['category', 'user'])
            ->latest('published_at')
            ->paginate(12);

        $categories = Category::active()
            ->ordered()
            ->withCount('publishedPosts')
            ->get();

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

    /**
     * 搜索
     */
    public function search(Request $request): View
    {
        $query = $request->input('q', '');

        $posts = Post::published()
            ->where(function ($q) use ($query) {
                $q->where('title', 'like', "%{$query}%")
                    ->orWhere('excerpt', 'like', "%{$query}%")
                    ->orWhere('content', 'like', "%{$query}%");
            })
            ->with(['category', 'user', 'tags'])
            ->latest('published_at')
            ->paginate(12);

        $categories = Category::active()
            ->ordered()
            ->withCount('publishedPosts')
            ->get();

        return view('blog.search', compact('posts', 'query', 'categories'));
    }
}

