<?php

namespace App\Http\Controllers;

use App\Models\Category;
use App\Models\Product;
use Illuminate\View\View;

class LandingController extends Controller
{
    /** Panel element IDs must stay in sync with `initFeaturedCollectionTabs` in public/js/site.js */
    private const FEATURE_TAB_PANEL_IDS = [
        'best-sellers' => 'block-template--19209892790459__featured-collections-collection_ExLVpG',
        'immune-support' => 'block-template--19209892790459__featured-collections-collection-1',
        'brain-health' => 'block-template--19209892790459__featured-collections-collection_YrNHhR',
        'sinus-support' => 'block-template--19209892790459__featured-collections-collection_nFpNWH',
        'gut-health' => 'block-template--19209892790459__featured-collections-collection_pYLypH',
        'bundles' => 'block-template--19209892790459__featured-collections-collection_phnTxW',
    ];

    public function index(): View
    {
        $featuredTabs = [];

        foreach (array_keys(self::FEATURE_TAB_PANEL_IDS) as $slug) {
            $category = Category::query()->where('slug', $slug)->first();
            if (! $category) {
                continue;
            }

            $products = Product::query()
                ->where('category_id', $category->id)
                ->where('is_active', true)
                ->orderBy('sort_order')
                ->orderBy('title')
                ->limit(12)
                ->get();

            $featuredTabs[] = [
                'category' => $category,
                'products' => $products,
                'panel_id' => self::FEATURE_TAB_PANEL_IDS[$slug],
            ];
        }

        return view('landing', [
            'featuredTabs' => $featuredTabs,
        ]);
    }
}
