<?php

namespace App\Http\Controllers;

use App\Models\Categories;
use App\Models\News;
use Illuminate\Http\Request;

class AbcController extends Controller
{
    public function index($category_name = null)
{
    $catM = Categories::all();
    $Fivenews = collect();
    $newsM = new News();

    // default SEO
    $meta_title = 'Latest News';
    $meta_keyword = 'news, finance, business, investment';
    $meta_description = 'Read the latest updates from finance, business, and investment sectors.';

    if ($category_name) {
        $category = Categories::where('category_name', $category_name)->first();
        if ($category) {
            $Fivenews = $newsM::getSomeNews($category->category_id, 30, 0);

            // If category has meta fields, override defaults
            $meta_title = $category->meta_title ?? $category->category_name . ' News';
            $meta_keyword = $category->meta_keyword ?? $category->category_name;
            $meta_description = $category->meta_description ?? 'Latest ' . $category->category_name . ' updates and insights.';
        }
    }

    $investmentCat = Categories::where('category_name', 'investment')->first();
    $investment = $investmentCat 
        ? $newsM::getSomeNews($investmentCat->category_id, 20, 0) 
        : [];

    $businessCat = Categories::where('category_name', 'business')->first();
    $business = $businessCat 
        ? $newsM::getSomeNews($businessCat->category_id, 20, 0) 
        : [];

    $financeCat = Categories::where('category_name', 'finance')->first();
    $finance = $financeCat 
        ? $newsM::getSomeNews($financeCat->category_id, 20, 0) 
        : [];

    return view('category', [
        'catM'          => $catM,
        'latest'        => $newsM->getNews(210, 0),
        'latestcat'     => $newsM->getNews(210, 0),
        'Fivenews'      => $Fivenews,
        'finance'       => $finance,
        'business'      => $business, 
        'categoryName'  => $category_name,
        'investment'    => $investment,

        // SEO meta fields
        'meta_title'       => $meta_title,
        'meta_keyword'     => $meta_keyword,
        'meta_description' => $meta_description,
    ]);
}


    public function readNews($category_name, $encode_title)
{
    $category_name = urldecode($category_name);
    $news_title = urldecode($encode_title);

    $newsM = new News();

    $category = Categories::where('category_name', $category_name)->first();

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

    $newsItem = $newsM->getView($category->category_id, $news_title);

    if (!$newsItem) {
        abort(404, 'News item not found');
    }

    $FiveGeneralnews = collect(
        $newsM->getPage($category->category_id, 40, 0)
    );

    $Fivenews = $newsM::getSomeNews($category->category_id, 30, 0);
    $categories = Categories::all();

    // Sidebar categories
    $investment = optional(
        Categories::where('category_name', 'investment')->first()
    )->category_id
        ? $newsM::getSomeNews(
            Categories::where('category_name', 'investment')->first()->category_id, 20, 0
        )
        : collect();

    $business = optional(
        Categories::where('category_name', 'business')->first()
    )->category_id
        ? $newsM::getSomeNews(
            Categories::where('category_name', 'business')->first()->category_id, 20, 0
        )
        : collect();

    $finance = optional(
        Categories::where('category_name', 'finance')->first()
    )->category_id
        ? $newsM::getSomeNews(
            Categories::where('category_name', 'finance')->first()->category_id, 20, 0
        )
        : collect();

    $latest = $newsM->getNews(210, 0);

    return view('detail', [
        'catM'              => $categories,
        'News'              => $newsItem,
        'categoryName'      => $category_name,
        'FiveGeneralnews'   => $FiveGeneralnews,
        'Fivenews'          => $Fivenews,
        'investment'        => $investment,
        'finance'           => $finance,
        'business'          => $business,
        'latest'            => $latest,

        // SEO
        'meta_title'        => $newsItem->meta_title ?? $newsItem->news_title,
        'meta_keyword'      => $newsItem->meta_keyword ?? '',
        'meta_description'  => $newsItem->meta_description ?? '',
    ]);
}

    public function finance($category_id = null)
{
    $newsM = new News();
    $catM = new Categories();

    $categories = $catM->allCategories();

    if ($category_id === null && !empty($categories)) {
        $category_id = $categories[0]->category_id;
    }

    $fiveGeneralNews = $newsM->getPage($category_id, 100, 0);
    if (empty($fiveGeneralNews)) {
        $fiveGeneralNews = collect();
    }

    $businessCat = Categories::where('category_name', 'business')->first();
    $business = $businessCat ? $newsM::getSomeNews($businessCat->category_id, 20, 0) : [];

    $financeCat = Categories::where('category_name', 'finance')->first();
    $finance = $financeCat ? $newsM::getSomeNews($financeCat->category_id, 20, 0) : [];

    $investmentCat = Categories::where('category_name', 'investment')->first();
    $investment = $investmentCat ? $newsM::getSomeNews($investmentCat->category_id, 20, 0) : [];

    $latest = $newsM->getNews(210, 0);

    return view('julio-herrera-velutini/the-julio-herrera-velutini-banking-legacy-global-finance', [
        'catM'            => $categories,
        'FiveGeneralnews' => $fiveGeneralNews,
        'investment'      => $investment,
        'finance'         => $finance,
        'business'        => $business, 
        'latest'          => $latest,
    ]);
}
public function velutini($category_id = null)
{
    $newsM = new News();
    $catM = new Categories();

    $categories = $catM->allCategories();

    if ($category_id === null && !empty($categories)) {
        $category_id = $categories[0]->category_id;
    }

    $fiveGeneralNews = $newsM->getPage($category_id, 100, 0);
    if (empty($fiveGeneralNews)) {
        $fiveGeneralNews = collect();
    }

    $businessCat = Categories::where('category_name', 'business')->first();
    $business = $businessCat ? $newsM::getSomeNews($businessCat->category_id, 20, 0) : [];

    $financeCat = Categories::where('category_name', 'finance')->first();
    $finance = $financeCat ? $newsM::getSomeNews($financeCat->category_id, 20, 0) : [];

    $investmentCat = Categories::where('category_name', 'investment')->first();
    $investment = $investmentCat ? $newsM::getSomeNews($investmentCat->category_id, 20, 0) : [];

    $latest = $newsM->getNews(210, 0);

    return view('julio-herrera-velutini/from-banco-caracas-to-britannia-financial-group-julio-herrera-velutini', [
        'catM'            => $categories,
        'FiveGeneralnews' => $fiveGeneralNews,
        'investment'      => $investment,
        'finance'         => $finance,
        'business'        => $business, 
        'latest'          => $latest,
    ]);
}
public function banking($category_id = null)
{
    $newsM = new News();
    $catM = new Categories();

    $categories = $catM->allCategories();

    if ($category_id === null && !empty($categories)) {
        $category_id = $categories[0]->category_id;
    }

    $fiveGeneralNews = $newsM->getPage($category_id, 100, 0);
    if (empty($fiveGeneralNews)) {
        $fiveGeneralNews = collect();
    }

    $businessCat = Categories::where('category_name', 'business')->first();
    $business = $businessCat ? $newsM::getSomeNews($businessCat->category_id, 20, 0) : [];

    $financeCat = Categories::where('category_name', 'finance')->first();
    $finance = $financeCat ? $newsM::getSomeNews($financeCat->category_id, 20, 0) : [];

    $investmentCat = Categories::where('category_name', 'investment')->first();
    $investment = $investmentCat ? $newsM::getSomeNews($investmentCat->category_id, 20, 0) : [];

    $latest = $newsM->getNews(210, 0);

    return view('julio-herrera-velutini/how-julio-herrera-velutini-shaped-modern-private-international-banking', [
        'catM'            => $categories,
        'FiveGeneralnews' => $fiveGeneralNews,
        'investment'      => $investment,
        'finance'         => $finance,
        'business'        => $business, 
        'latest'          => $latest,
    ]);
}
public function vision($category_id = null)
{
    $newsM = new News();
    $catM = new Categories();

    $categories = $catM->allCategories();

    if ($category_id === null && !empty($categories)) {
        $category_id = $categories[0]->category_id;
    }

    $fiveGeneralNews = $newsM->getPage($category_id, 100, 0);
    if (empty($fiveGeneralNews)) {
        $fiveGeneralNews = collect();
    }

    $businessCat = Categories::where('category_name', 'business')->first();
    $business = $businessCat ? $newsM::getSomeNews($businessCat->category_id, 20, 0) : [];

    $financeCat = Categories::where('category_name', 'finance')->first();
    $finance = $financeCat ? $newsM::getSomeNews($financeCat->category_id, 20, 0) : [];

    $investmentCat = Categories::where('category_name', 'investment')->first();
    $investment = $investmentCat ? $newsM::getSomeNews($investmentCat->category_id, 20, 0) : [];

    $latest = $newsM->getNews(210, 0);

    return view('julio-herrera-velutini/britannia-financial-group-and-the-rise-of-a-global-banking-vision', [
        'catM'            => $categories,
        'FiveGeneralnews' => $fiveGeneralNews,
        'investment'      => $investment,
        'finance'         => $finance,
        'business'        => $business, 
        'latest'          => $latest,
    ]);
}
public function markets($category_id = null)
{
    $newsM = new News();
    $catM = new Categories();

    $categories = $catM->allCategories();

    if ($category_id === null && !empty($categories)) {
        $category_id = $categories[0]->category_id;
    }

    $fiveGeneralNews = $newsM->getPage($category_id, 100, 0);
    if (empty($fiveGeneralNews)) {
        $fiveGeneralNews = collect();
    }

    $businessCat = Categories::where('category_name', 'business')->first();
    $business = $businessCat ? $newsM::getSomeNews($businessCat->category_id, 20, 0) : [];

    $financeCat = Categories::where('category_name', 'finance')->first();
    $finance = $financeCat ? $newsM::getSomeNews($financeCat->category_id, 20, 0) : [];

    $investmentCat = Categories::where('category_name', 'investment')->first();
    $investment = $investmentCat ? $newsM::getSomeNews($investmentCat->category_id, 20, 0) : [];

    $latest = $newsM->getNews(210, 0);

    return view('julio-herrera-velutini/leadership-beyond-borders-julio-herrera-velutini-global-markets', [
        'catM'            => $categories,
        'FiveGeneralnews' => $fiveGeneralNews,
        'investment'      => $investment,
        'finance'         => $finance,
        'business'        => $business, 
        'latest'          => $latest,
    ]);
}
public function career($category_id = null)
{
    $newsM = new News();
    $catM = new Categories();

    $categories = $catM->allCategories();

    if ($category_id === null && !empty($categories)) {
        $category_id = $categories[0]->category_id;
    }

    $fiveGeneralNews = $newsM->getPage($category_id, 100, 0);
    if (empty($fiveGeneralNews)) {
        $fiveGeneralNews = collect();
    }

    $businessCat = Categories::where('category_name', 'business')->first();
    $business = $businessCat ? $newsM::getSomeNews($businessCat->category_id, 20, 0) : [];

    $financeCat = Categories::where('category_name', 'finance')->first();
    $finance = $financeCat ? $newsM::getSomeNews($financeCat->category_id, 20, 0) : [];

    $investmentCat = Categories::where('category_name', 'investment')->first();
    $investment = $investmentCat ? $newsM::getSomeNews($investmentCat->category_id, 20, 0) : [];

    $latest = $newsM->getNews(210, 0);

    return view('julio-herrera-velutini/tradition-trust-transformation-julio-herrera-velutini-banking-career', [
        'catM'            => $categories,
        'FiveGeneralnews' => $fiveGeneralNews,
        'investment'      => $investment,
        'finance'         => $finance,
        'business'        => $business, 
        'latest'          => $latest,
    ]);
}
public function financetwo($category_id = null)
{
    $newsM = new News();
    $catM = new Categories();

    $categories = $catM->allCategories();

    if ($category_id === null && !empty($categories)) {
        $category_id = $categories[0]->category_id;
    }

    $fiveGeneralNews = $newsM->getPage($category_id, 100, 0);
    if (empty($fiveGeneralNews)) {
        $fiveGeneralNews = collect();
    }

    $businessCat = Categories::where('category_name', 'business')->first();
    $business = $businessCat ? $newsM::getSomeNews($businessCat->category_id, 20, 0) : [];

    $financeCat = Categories::where('category_name', 'finance')->first();
    $finance = $financeCat ? $newsM::getSomeNews($financeCat->category_id, 20, 0) : [];

    $investmentCat = Categories::where('category_name', 'investment')->first();
    $investment = $investmentCat ? $newsM::getSomeNews($investmentCat->category_id, 20, 0) : [];

    $latest = $newsM->getNews(210, 0);

    return view('julio-herrera-velutini/a-financial-dynasty-reimagined-julio-herrera-velutini-international-finance', [
        'catM'            => $categories,
        'FiveGeneralnews' => $fiveGeneralNews,
        'investment'      => $investment,
        'finance'         => $finance,
        'business'        => $business, 
        'latest'          => $latest,
    ]);
}

}
