<?php

namespace App\Http\Controllers;

use App\Models\Blog;
use App\Models\Category;
use App\Models\Package;
use App\Models\User;
use Illuminate\Http\Request;

class FrontendController extends Controller
{
    public function home()
    {
        $featured = Blog::published()->with('author', 'category')->latest('published_at')->take(3)->get();
        $latest   = Blog::published()->with('author', 'category')->latest('published_at')->skip(3)->take(6)->get();
        $categories = Category::whereNull('parent_id')->where('is_active', true)->withCount('blogs')->take(8)->get();
        $packages = Package::where('is_active', true)->orderBy('sort_order')->take(3)->get();

        return view('frontend.home', compact('featured', 'latest', 'categories', 'packages'));
    }

    public function blogs(Request $request)
    {
        $query = Blog::published()->with('author', 'category');

        if ($search = $request->get('q')) {
            $query->where(function ($q) use ($search) {
                $q->where('title', 'like', "%$search%")
                  ->orWhere('content', 'like', "%$search%");
            });
        }

        $blogs = $query->latest('published_at')->paginate(9)->withQueryString();
        $categories = Category::tree();

        return view('frontend.blogs', compact('blogs', 'categories', 'search'));
    }

    public function show(string $slug)
    {
        $blog = Blog::where('slug', $slug)->published()->with('author', 'category')->firstOrFail();
        $blog->increment('views');

        $related = Blog::published()
            ->where('id', '!=', $blog->id)
            ->where('category_id', $blog->category_id)
            ->latest('published_at')
            ->take(3)
            ->get();

        return view('frontend.blog-show', compact('blog', 'related'));
    }

    public function category(string $slug)
    {
        $category = Category::where('slug', $slug)->firstOrFail();

        // collect this category + all descendant ids
        $ids = $this->collectDescendantIds($category);
        $ids[] = $category->id;

        $blogs = Blog::published()
            ->whereIn('category_id', $ids)
            ->with('author', 'category')
            ->latest('published_at')
            ->paginate(9);

        return view('frontend.category', compact('category', 'blogs'));
    }

    protected function collectDescendantIds(Category $cat): array
    {
        $ids = [];
        foreach ($cat->children as $child) {
            $ids[] = $child->id;
            $ids = array_merge($ids, $this->collectDescendantIds($child));
        }
        return $ids;
    }

    public function author(string $username)
    {
        $author = User::where('username', $username)->where('role', 'reseller')->firstOrFail();
        $blogs = Blog::published()->where('user_id', $author->id)->latest('published_at')->paginate(9);

        return view('frontend.author', compact('author', 'blogs'));
    }

    public function packages()
    {
        $packages = Package::where('is_active', true)->orderBy('sort_order')->get();
        return view('frontend.packages', compact('packages'));
    }

    public function about()
    {
        return view('frontend.about');
    }

    public function contact()
    {
        return view('frontend.contact');
    }
}
