<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\News;


use App\Models\Categories;

class Home extends Controller
{
 public function index()
{
    $newsM = new News();
    $catModel = new Categories();

    // All categories (menu etc.)
    $categories = $catModel->allCategories();

    // Category wise news (all categories)
    $categoryNews = [];
    foreach ($categories as $category) {
        $categoryNews[strtolower($category->category_name)] =
            $newsM->getindex($category->category_id, 20, 0);
    }

    // Homepage categories
    $homeCategories = [
        'business',
        'world',
        'finance',
        'technology',
        'politics',
        'lifestyle',
        'opinion',
        'investigation',
    ];

    $homeCatNews = [];

    foreach ($homeCategories as $catName) {
        $cat = Categories::where('category_name', $catName)->first();

        $homeCatNews[$catName] = $cat
            ? News::getSomeNews($cat->category_id, 20, 0)
            : [];
    }

    $data = [
        'items'         => News::all(),
        'catM'          => $categories,
        'catNews'       => $categoryNews,

        // homepage sections
        'business'      => $homeCatNews['business'],
        'world'         => $homeCatNews['world'],
        'finance'       => $homeCatNews['finance'],
        'technology'    => $homeCatNews['technology'],
        'politics'      => $homeCatNews['politics'],
        'lifestyle'     => $homeCatNews['lifestyle'],
        'opinion'       => $homeCatNews['opinion'],
        'investigation' => $homeCatNews['investigation'],

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

    return view('index', $data);
}





}