<?php
declare(strict_types=1);

namespace App\Model\Table;

use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
use App\Utility\SchemaUtility;

/**
 * Menus Model
 *
 * @property \App\Model\Table\AlbumsTable&\Cake\ORM\Association\HasMany $Albums
 * @property \App\Model\Table\ContentsTable&\Cake\ORM\Association\HasMany $Contents
 * @property \App\Model\Table\PageContentsTable&\Cake\ORM\Association\HasMany $PageContents
 * @property \App\Model\Table\PageProductsTable&\Cake\ORM\Association\HasMany $PageProducts
 *
 * @method \App\Model\Entity\Menu newEmptyEntity()
 * @method \App\Model\Entity\Menu newEntity(array $data, array $options = [])
 * @method \App\Model\Entity\Menu[] newEntities(array $data, array $options = [])
 * @method \App\Model\Entity\Menu get($primaryKey, $options = [])
 * @method \App\Model\Entity\Menu findOrCreate($search, ?callable $callback = null, $options = [])
 * @method \App\Model\Entity\Menu patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
 * @method \App\Model\Entity\Menu[] patchEntities(iterable $entities, array $data, array $options = [])
 * @method \App\Model\Entity\Menu|false save(\Cake\Datasource\EntityInterface $entity, $options = [])
 * @method \App\Model\Entity\Menu saveOrFail(\Cake\Datasource\EntityInterface $entity, $options = [])
 * @method \App\Model\Entity\Menu[]|\Cake\Datasource\ResultSetInterface|false saveMany(iterable $entities, $options = [])
 * @method \App\Model\Entity\Menu[]|\Cake\Datasource\ResultSetInterface saveManyOrFail(iterable $entities, $options = [])
 * @method \App\Model\Entity\Menu[]|\Cake\Datasource\ResultSetInterface|false deleteMany(iterable $entities, $options = [])
 * @method \App\Model\Entity\Menu[]|\Cake\Datasource\ResultSetInterface deleteManyOrFail(iterable $entities, $options = [])
 */
class MenusTable extends Table
{
    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config): void
    {
        parent::initialize($config);

        $this->setTable('menus');
        $this->setDisplayField('name');
        $this->setPrimaryKey('id');

        $this->belongsTo('ParentMenus', [
            'foreignKey' => 'parent_menu_id',
            'className' => 'Menus'
        ]);
        $this->hasMany('ChildMenus', [
            'foreignKey' => 'parent_menu_id',
            'className' => 'Menus',
            'conditions' => [
                'ChildMenus.status' => 1
            ]
        ]);
        $this->hasMany('SubChildMenus', [
            'foreignKey' => 'parent_menu_id',
            'className' => 'Menus',
            'conditions' => [
                'SubChildMenus.status' => 1
            ]
        ]);
        $this->hasMany('Albums', [
            'foreignKey' => 'menu_id',
        ]);
        $this->hasMany('Contents', [
            'foreignKey' => 'menu_id',
        ]);
        $this->hasMany('PageContents', [
            'foreignKey' => 'menu_id',
        ]);
        $this->hasMany('PageProducts', [
            'foreignKey' => 'menu_id',
        ]);
    }

    public function getMainMenus()
    {
        $query = $this->find()
            ->contain(['ChildMenus'])
            ->where(['Menus.parent_menu_id' => 0, 'Menus.status' => 1]);

        // Dynamic order clause based on schema
        if (SchemaUtility::hasColumn('Menus', 'order_value')) {
            $query->order(['order_value' => 'ASC']);
        }

        // Fallback ordering
        // if (!SchemaUtility::hasColumn('Menus', 'order_value')) {
        //     $query->order(['Menus.home_order' => 'ASC']);
        // }

        return $query->all()->toArray();
    }

    public function getFooterMenus()
    {
        $query = $this->find()
            ->contain(['ChildMenus'])
            ->where([
                'Menus.parent_menu_id' => 0,
                'Menus.status' => 1,
                'Menus.show_on_footer' => 1,
            ]);

        // Dynamic order clause based on schema
        if (SchemaUtility::hasColumn('Menus', 'order_value')) {
            $query->order(['order_value' => 'ASC']);
        }

        // Fallback ordering
        if (!SchemaUtility::hasColumn('Menus', 'order_value')) {
            $query->order(['Menus.name' => 'ASC']);
        }

        return $query->all()->toArray();
    }

    public function getEnabledMenus()
    {
        $query = $this->find('all', [
            'conditions' => [
                'status' => '1',
            ]
        ]);

        // Dynamic order clause based on schema
        if (SchemaUtility::hasColumn('Menus', 'home_order')) {
            $query->order(['home_order' => 'ASC']);
        }


        return $query->toArray();
    }

    public function getMenusContents($menu)
    {
        $query = $this->find('all', [
            'contain' => [
                'ParentMenus'
            ],
            'conditions' => [
                'Menus.parent_menu_id' => $menu->id,
                'Menus.status' => 1,
                'Menus.show_on_home' => 1
            ]
        ]);

        // Dynamic order clause based on schema
        if (SchemaUtility::hasColumn('Menus', 'home_order')) {
            $query->order(['Menus.home_order' => 'ASC']);
        }
        return $query->toArray();
    }

    public function getMenuDetail($section)
    {
        $query = $this->find('all', [
            'conditions' => [
                'slug' => $section // Retrieve 'slug' parameter from the request
            ]
        ])->first();

        return $query;
    }

    public function getMenuDetailById($id)
    {
        $query = $this->find('all', [
            'conditions' => [
                'id' => $id // Retrieve 'slug' parameter from the request
            ]
        ])->first();

        return $query;
    }

    public function getSubMenu($menuDetails)
    {
        $query = $this->find('all', [
            'conditions' => [
                'status' => '1',
                'parent_menu_id' => $menuDetails->id
            ]
        ])->toArray();

        return $query;
    }

    public function getSubSecSubMenu($menuDetails)
    {
        $query = $this->find('all', [
            'conditions' => [
                'parent_menu_id' => $menuDetails->id
            ]
        ])->select(['id']);

        return $query;
    }

    public function getCategory($menuDetails)
    {
        $query = $this->find('all', [
            'contain' => [
                'ParentMenus'
            ],
            'conditions' => [
                'Menus.parent_menu_id' => $menuDetails->id,
                'Menus.status' => 1
            ]
        ])->toArray();

        return $query;
    }

    public function getSubParentMenu($menuDetails)
    {
        $query = $this->find('all', [
            'conditions' => [
                'id' => $menuDetails->parent_menu_id
            ]
        ])->first();

        return $query;
    }

    public function getHomeContentParentMenu($id)
    {
        $query = $this->find('all', [
            'conditions' => [
                'status' => '1',
                'id' => $id
            ]
        ])->select(['slug','name'])  // Select only the 'slug' field
        ->first();

        return $query;
    }

}
