<?php

namespace App\Http\Controllers\Front;

use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\HybridTopRssPostKeywords;
use App\Models\RssPost;
use App\Models\RssPostKeyword;
use Artesaos\SEOTools\Facades\SEOTools;
use Illuminate\Http\Request;
use JsonLd\Context;

class FrontListController extends Controller
{
    public function search(Request $request)
    {
        if (is_empty($request->input('query'))) {
            return redirect()->back();
        }

        return redirect()->to(get_route_search_result($request->input('query')));
    }

    public function searchResults(Request $request, $query)
    {
        $page_type = 'search';

        $query = urldecode($query);

        $rss_post_keyword = RssPostKeyword::where('value_lowercased', strtolower($query))->first();

        if (! is_null($rss_post_keyword)) {
            $query = $rss_post_keyword->value;
        }

        $breadcrumbs = collect([
            ['name' => 'Home', 'url' => route('front.home')],
            ['name' => 'News Bites', 'url' => route('front.all')],
            ['name' => strtolower('#'.$query), 'url' => null],
        ]);

        $title = "{$query} News Bites: Updated Hourly";

        $top_rss_keywords = HybridTopRssPostKeywords::get(1, 5);

        $description = 'Stay updated with '.$query;

        foreach ($top_rss_keywords as $rss_keyword) {

            if (strtolower($rss_keyword) != strtolower($query)) {
                $description .= ', '.$rss_keyword->value;
            }

        }
        $description .= ' and over 1000 other news topics with 1-minute news bites from FutureWalker.';

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

        $rss_posts = RssPost::with('category', 'entities_keywords')
            ->where('status', 'published')
            ->whereRaw("to_tsvector('english', title || ' ' || bites || ' ' || keyword_list) @@ plainto_tsquery('english', ?)", [trim(preg_replace('/\s+/', ' ', $query))])
            ->where('published_at', '<=', now())
            ->orderBy('published_at', 'desc')
            ->cursorPaginate(60);

        // 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.rss_post_list', compact('rss_posts', 'breadcrumbs', 'breadcrumb_context', 'title', 'page_type'));
    }

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

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

        $title = '1-minute AI & Tech News Bites: Updated Hourly';
        $description = 'Stay updated on over 1000 news topics, summarized into 1-minute news bites with FutureWalker.';

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

        $rss_posts = RssPost::with('category', 'entities_keywords')->where('status', 'published')
            ->where('published_at', '<=', now())
            ->orderBy('published_at', 'desc')
            ->cursorPaginate(60) ?? collect();

        // breadcrumb json ld
        $listItems = [];

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

        //dd($rss_posts);

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

        return view('front.rss_post_list', compact('rss_posts', 'breadcrumbs', 'breadcrumb_context', 'page_type'));
    }

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

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

        if (is_null($category)) {
            abort(404);
        }

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

        $title = 'AI & Tech '."$category->name".': Updated Hourly';

        $top_rss_keywords = HybridTopRssPostKeywords::get(1, 10);

        $description = 'Stay updated with ';

        foreach ($top_rss_keywords as $rss_keyword) {
            $description .= ', '.$rss_keyword->value;
        }
        $description .= ' and 1000+ other news '.strtolower($category->name).'  with 1-minute news bites from FutureWalker.';

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

        $rss_posts = RssPost::with('category', 'entities_keywords')->where('status', 'published')
            ->where('category_id', $category->id)
            ->where('published_at', '<=', now())
            ->orderBy('published_at', 'desc')
            ->cursorPaginate(60) ?? collect();

        // breadcrumb json ld
        $listItems = [];

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

        //dd($rss_posts);

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

        return view('front.rss_post_list', compact('rss_posts', 'breadcrumbs', 'breadcrumb_context', 'page_type', 'category'));
    }
}
