<?php

namespace App\Http\Controllers\Frontend;

use App\Http\Controllers\Controller;
use App\Models\BlogPost;
use App\Models\Product;
use App\Services\SectionService;

class HomeController extends Controller
{
    protected array $data;

    public function index()
    {
        $sectionService = app(SectionService::class);
        $sections = $sectionService->allOrdered();

        $productSection = $sections->firstWhere('slug', 'products');
        $blogSection    = $sections->firstWhere('slug', 'blog');

        $productCount = $productSection['config']['count'] ?? setting('products.on_homepage', 6);
        $blogCount    = $blogSection['config']['count'] ?? setting('posts.on_homepage', 3);

        $products = Product::latest('id')->withAvg('reviews', 'rating')->paginate($productCount);
        $posts    = BlogPost::latest('id')->paginate($blogCount);

        return view('theme::pages.home', compact('sections', 'products', 'posts'));
    }
}
