<?php

namespace App\Http\Controllers;

use App\Models\LibraryCategory;
use App\Models\LibraryContent;
use App\Models\LibraryComment;
use Illuminate\Http\Request;
use Illuminate\View\View;

class LibraryController extends Controller
{
    // ─── Library Index ───────────────────────────────────────────────────
    public function index(): View
    {
        $libraryCategories = LibraryCategory::whereNull('parent_id')
            ->with(['children.contents', 'contents'])
            ->withCount('contents')
            ->orderBy('order')
            ->get();

        $stats = [
            'total_categories' => LibraryCategory::count(),
            'total_contents'   => LibraryContent::count(),
            'total_views'      => LibraryContent::sum('views'),
        ];

        $recentContents = LibraryContent::with('category')
            ->latest()
            ->take(6)
            ->get();

        return view('library.index', compact('libraryCategories', 'stats', 'recentContents'));
    }

    // ─── Category View ────────────────────────────────────────────────────
    public function category(LibraryCategory $category): View
    {
        $category->load(['children.contents', 'parent']);

        $contents = LibraryContent::where('category_id', $category->id)
            ->latest()
            ->paginate(15);

        $libraryCategories = LibraryCategory::whereNull('parent_id')
            ->with('children')
            ->orderBy('order')
            ->get();

        return view('library.category', compact('category', 'contents', 'libraryCategories'));
    }

    // ─── Content Detail ───────────────────────────────────────────────────
    public function show(LibraryCategory $category, LibraryContent $content): View
    {
        $content->load('category');
        $content->incrementViews();

        $relatedContents = LibraryContent::where('category_id', $category->id)
            ->where('id', '!=', $content->id)
            ->latest()
            ->take(5)
            ->get();

        $libraryCategories = LibraryCategory::whereNull('parent_id')
            ->with('children')
            ->orderBy('order')
            ->get();

        // Load approved comments with replies and users
        $comments = LibraryComment::where('content_id', $content->id)
            ->whereNull('parent_id')
            ->where('is_approved', true)
            ->with(['user', 'replies' => function ($q) {
                $q->where('is_approved', true)->with('user');
            }])
            ->latest()
            ->get();

        return view('library.show', compact('category', 'content', 'relatedContents', 'libraryCategories', 'comments'));
    }

    // ─── Search ───────────────────────────────────────────────────────────
    public function search(Request $request): View
    {
        $q = $request->input('q', '');
        $contents = collect();

        if (strlen($q) >= 2) {
            $contents = LibraryContent::with('category')
                ->where('title', 'like', "%{$q}%")
                ->latest()
                ->paginate(15);
        }

        $libraryCategories = LibraryCategory::whereNull('parent_id')
            ->with('children')
            ->orderBy('order')
            ->get();

        return view('library.search', compact('q', 'contents', 'libraryCategories'));
    }
}
