<?php
declare(strict_types=1);

namespace App\Controller;

/**
 * Home Controller
 *
 * @method \App\Model\Entity\Home[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
 */
class HomeController extends AppController
{
    public function initialize(): void
    {
        parent::initialize();

        $this->Banners = $this->fetchTable('Banners');
        $this->PageContents = $this->fetchTable('PageContents');
        // $this->Contacts = $this->fetchTable('Contacts');
        // $this->Galleries = $this->fetchTable('Galleries');
    }

    /**
     * Index method
     *
     * @return \Cake\Http\Response|null|void Renders view
     */
    public function index(...$path)
    {
        /** 
         * Homepage for the one pager site
         * Lets load the contents for the different sections
         * 
         * Contents are stored under different menus, in our order to load them correctly
         * Selects the different menus under the section and construct a an array with
         * with the contents saved with the menu as key
         */

        // Load the banners. Banners are normally loaded at the top of the page.
        // So lets add it to the home section.
        // Append to any section if you want to show as well, could be carousel, etc....
        $banners = $this->Banners->getHomeBanners();

        // Load only enabled and show on home menus
        $homeMenus = $this->PageContents->Menus->getEnabledMenus();

        /**
         * Initializes an empty array for storing homepage sections.
         *Filter by only contents that have been marked to show on the hompage
         * @var array $homepageSections Array to hold various sections of the homepage.
         */
        $homepageSections = [];

        // Load only enabled and show on home menus
        foreach ($homeMenus as $menu) {
            // Fetch the contents for the current menu
            $contents = $this->PageContents->Contents->getHomeContents($menu);

            if (!empty($contents)) {
                foreach ($contents as $content) {
                    $slug = $content->slug ?? $menu->slug;

                    // Ensure that the key exists and is an array
                    if (!isset($homepageSections[$slug])) {
                        $homepageSections[$slug] = [
                            'section' => $menu,
                            'content' => []
                        ];
                    }

                    // Append the current content to the 'list' array
                    $homepageSections[$slug]['content'][] = $content;
                }

            }

            // Fetch additional menu-specific content
            $menuContents = $this->PageContents->Menus->getMenusContents($menu);

            if (!empty($menuContents)) {
                $homepageSections[$menu->slug] = [
                    'subsection' => $menu->slug,
                    'list' => $menuContents
                ];
            }
        }

        $this->set((compact('banners', 'homeMenus', 'homepageSections')));
    }
}
