<?php

namespace App\Http\Controllers\Front;

use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\Post;
use App\Models\PostCategory;
use Artesaos\SEOTools\Facades\SEOTools;
use Illuminate\Http\Request;
use JsonLd\Context;

class FrontListController extends Controller
{
    public function search(Request $request)
    {
        $page_type = 'search';

        $query = $request->get('query', '');

        $breadcrumbs = collect([
            ['name' => 'Home', 'url' => route('front.home')],
            ['name' => 'Search', 'url' => null],
            ['name' => $query, 'url' => url()->current()],
        ]);

        $title = 'Latest News about '.ucwords($query).' in FutureWalker';

        SEOTools::metatags();
        SEOTools::twitter();
        SEOTools::opengraph();
        SEOTools::jsonLd();
        SEOTools::setTitle($title, false);

        // Use full-text search capabilities of your database
        // For example, using MySQL's full-text search with MATCH...AGAINST
        $posts = Post::with('category')
            ->where('status', 'publish')
            ->whereRaw("to_tsvector('english', title || ' ' || bites) @@ to_tsquery('english', ?)", [$query])
            ->orderBy('published_at', 'desc')
            ->cursorPaginate(10);

        // breadcrumb json ld
        $listItems = [];

        foreach ($breadcrumbs as $index => $breadcrumb) {
            $listItems[] = [
                'name' => $breadcrumb['name'],
                'url' => $breadcrumb['url'],
            ];
        }

        $breadcrumb_context = Context::create('breadcrumb_list', [
            'itemListElement' => $listItems,
        ]);

        return view('front.post_list', compact('posts', 'breadcrumbs', 'breadcrumb_context', 'title', 'page_type'));
    }

    public function index(Request $request)
    {
        $page_type = 'default';

        $breadcrumbs = collect([
            ['name' => 'Home', 'url' => route('front.home')],
            ['name' => 'Latest News', 'url' => null],  // or you can set a route for Latest News if there's a specific one
        ]);

        $title = 'Latest News from FutureWalker';

        SEOTools::metatags();
        SEOTools::twitter();
        SEOTools::opengraph();
        SEOTools::jsonLd();
        SEOTools::setTitle($title, false);

        $posts = Post::with('category')->where('status', 'publish')->orderBy('published_at', 'desc')->cursorPaginate(10) ?? collect();

        // breadcrumb json ld
        $listItems = [];

        foreach ($breadcrumbs as $index => $breadcrumb) {
            $listItems[] = [
                'name' => $breadcrumb['name'],
                'url' => $breadcrumb['url'],
            ];
        }

        //dd($posts);

        $breadcrumb_context = Context::create('breadcrumb_list', [
            'itemListElement' => $listItems,
        ]);

        return view('front.post_list', compact('posts', 'breadcrumbs', 'breadcrumb_context', 'page_type'));
    }

    public function category(Request $request, $category_slug)
    {
        $page_type = 'default';

        // Fetch the category by slug
        $category = Category::where('slug', $category_slug)->first();

        // Check if the category exists
        if (! $category) {
            abort(404, 'Category not found');
        }

        // Breadcrumb logic
        $breadcrumbs = collect([['name' => 'Home', 'url' => route('front.home')]]);
        foreach ($category->ancestors as $ancestor) {
            $breadcrumbs->push(['name' => $ancestor->name, 'url' => route('front.category', $ancestor->slug)]);
        }
        $breadcrumbs->push(['name' => $category->name, 'url' => route('front.category', $category->slug)]);

        // Get the IDs of the category and its descendants
        $categoryIds = $category->descendants->pluck('id')->push($category->id);

        // Get the posts associated with these category IDs
        $postIds = PostCategory::whereIn('category_id', $categoryIds)->pluck('post_id');
        $posts = Post::whereIn('id', $postIds)->where('status', 'publish')->orderBy('published_at', 'desc')->cursorPaginate(10);

        $title = $category->name.' News from FutureWalker';

        SEOTools::metatags();
        SEOTools::twitter();
        SEOTools::opengraph();
        SEOTools::setTitle($title, false);
        SEOTools::jsonLd();

        // breadcrumb json ld
        $listItems = [];

        foreach ($breadcrumbs as $index => $breadcrumb) {
            $listItems[] = [
                'name' => $breadcrumb['name'],
                'url' => $breadcrumb['url'],
            ];
        }

        $breadcrumb_context = Context::create('breadcrumb_list', [
            'itemListElement' => $listItems,
        ]);

        return view('front.post_list', compact('category', 'posts', 'breadcrumbs', 'breadcrumb_context', 'page_type'));
    }
}
