<?php
namespace App\Models;
use CodeIgniter\Model;
use App\Traits\ModelTrait;

class Cms extends Model
{
    use ModelTrait;

	protected $table            = 'epos_cms';
    protected $primaryKey       = 'id';
    protected $useTimestamps    = true;
    protected $allowedFields    = ['type', 'start_date', 'end_date', 'branches', 'price_list', 'organization_id', 'internal_name', 'active',
        'editor', 'page_pos', 'ribbon', 'deleted', 'template_id', 'data', 'dwell_time', 'promotion_id', 'media_web', 'media_mobile'];

    public static function getMaxPagePosition() {
        $model = new Cms();
        $date = date('Y-m-d H:i:s');
        $query = $model
            ->where('active', 1)
            ->where('start_date<=', $date)
            ->where('end_date>=', $date)
            ->selectMax('page_pos');
        $result = $query->first();
        return !empty($result) ? $result['page_pos'] : 0;
    }

    public static function getDetailTable($type) {
        switch($type) {
        }
        return '';
    }

    public static function getDetailModel($type) {
        switch($type) {
        }
        return null;
    }

    public function getBrochureAndItems_For_APIAppMedia()
    {
        // First, get all brochure records
        $data = $this->where('type', 'brochure')
                    //  ->where('active', 1)
                    //  ->where('deleted !=', 1)
                     ->findAll();
        
        // Second, on each brochure record, get cms_items from epos_cms_items with given cms_id
        $cmsItemModel = new \App\Models\CmsItem();
        
        foreach ($data as &$brochure) {
            $cms_id = $brochure['id'];
            $items = $cmsItemModel->where('cms_id', $cms_id)
                                //   ->where('deleted !=', 1)
                                  ->findAll();
            $brochure['items'] = $items;
        }
        
        
        return [
            'brochures' => $data
        ];
    }

    public function getBottomBannerAndItems_For_APIAppMedia()
    {
        // First, get all bottom_banner records
        $data = $this->where('type', 'bottom_banner')
                    //  ->where('active', 1)
                    //  ->where('deleted !=', 1)
                     ->findAll();
        
        // Second, on each bottom_banner record, get cms_items from epos_cms_items with given cms_id
        $cmsItemModel = new \App\Models\CmsItem();
        
        foreach ($data as &$banner) {
            $cms_id = $banner['id'];
            $items = $cmsItemModel->where('cms_id', $cms_id)
                                //   ->where('deleted !=', 1)
                                  ->findAll();
            $banner['items'] = $items;
        }
        
        
        return [
            'bottom_banners' => $data
        ];
    }

    public function getHomeBannerAndItems_For_APIAppMedia()
    {
        // First, get all home_banner records
        $data = $this->where('type', 'home_banner')
                    //  ->where('active', 1)
                    //  ->where('deleted !=', 1)
                     ->findAll();
        
        // Second, on each home_banner record, get cms_items from epos_cms_items with given cms_id
        $cmsItemModel = new \App\Models\CmsItem();
        
        foreach ($data as &$banner) {
            $cms_id = $banner['id'];
            $items = $cmsItemModel->where('cms_id', $cms_id)
                                //   ->where('deleted !=', 1)
                                  ->findAll();
            $banner['items'] = $items;
        }
        
        
        return [
            'home_banners' => $data
        ];
    }

    public function getCategoryBannerAndItems_For_APIAppMedia()
    {
        // First, get all category_banner records
        $data = $this->where('type', 'category_banner')
                    //  ->where('active', 1)
                    //  ->where('deleted !=', 1)
                     ->findAll();
        
        // Second, on each category_banner record, get cms_items from epos_cms_items with given cms_id
        $cmsItemModel = new \App\Models\CmsItem();
        
        foreach ($data as &$banner) {
            $cms_id = $banner['id'];
            $items = $cmsItemModel->where('cms_id', $cms_id)
                                //   ->where('deleted !=', 1)
                                  ->findAll();
            $banner['items'] = $items;
        }
        
        
        return [
            'category_banners' => $data
        ];
    }

    public function getBrandAndItems_For_APIAppMedia()
    {
        // First, get all brand records
        $data = $this->where('type', 'brand')
                    //  ->where('active', 1)
                    //  ->where('deleted !=', 1)
                     ->findAll();
        
        // Second, on each brand record, get cms_items from epos_cms_items with given cms_id
        $cmsItemModel = new \App\Models\CmsItem();
        
        foreach ($data as &$brand) {
            $cms_id = $brand['id'];
            $items = $cmsItemModel->where('cms_id', $cms_id)
                                //   ->where('deleted !=', 1)
                                  ->findAll();
            $brand['items'] = $items;
        }
        
        
        return [
            'brands' => $data
        ];
    }

    public function getCategoryCarouselAndItems_For_APIAppMedia()
    {
        // First, get all category_carousel records
        $data = $this->where('type', 'category_carousel')
                    //  ->where('active', 1)
                    //  ->where('deleted !=', 1)
                     ->findAll();
        
        // Second, on each category_carousel record, get cms_items from epos_cms_items with given cms_id
        $cmsItemModel = new \App\Models\CmsItem();
        
        foreach ($data as &$carousel) {
            $cms_id = $carousel['id'];
            $items = $cmsItemModel->where('cms_id', $cms_id)
                                //   ->where('deleted !=', 1)
                                  ->findAll();
            $carousel['items'] = $items;
        }
        
        
        return [
            'category_carousels' => $data
        ];
    }

    public function getMultibuyAndItems_For_APIAppMedia()
    {
        // First, get all multibuy records
        $data = $this->where('type', 'multibuy')
                    //  ->where('active', 1)
                    //  ->where('deleted !=', 1)
                     ->findAll();
        
        // Second, on each multibuy record, get cms_items from epos_cms_items with given cms_id
        $cmsItemModel = new \App\Models\CmsItem();
        
        foreach ($data as &$multibuy) {
            $cms_id = $multibuy['id'];
            $items = $cmsItemModel->where('cms_id', $cms_id)
                                //   ->where('deleted !=', 1)
                                  ->findAll();
            $multibuy['items'] = $items;
        }
        
        
        return [
            'multibuys' => $data
        ];
    }

    public function getProductsCarouselAndItems_For_APIAppMedia()
    {
        // First, get all products_carousel records
        $data = $this->where('type', 'products_carousel')
                    //  ->where('active', 1)
                    //  ->where('deleted !=', 1)
                     ->findAll();
        
        // Second, on each products_carousel record, get cms_items from epos_cms_items with given cms_id
        $cmsItemModel = new \App\Models\CmsItem();
        
        foreach ($data as &$carousel) {
            $cms_id = $carousel['id'];
            $items = $cmsItemModel->where('cms_id', $cms_id)
                                //   ->where('deleted !=', 1)
                                  ->findAll();
            $carousel['items'] = $items;
        }
        
        
        return [
            'products_carousels' => $data
        ];
    }

    public function getShopByCategoryAndItems_For_APIAppMedia()
    {
        // First, get all shop_by_category records
        $data = $this->where('type', 'shop_by_category')
                    //  ->where('active', 1)
                    //  ->where('deleted !=', 1)
                     ->findAll();
        
        // Second, on each shop_by_category record, get cms_items from epos_cms_items with given cms_id
        $cmsItemModel = new \App\Models\CmsItem();
        
        foreach ($data as &$shop) {
            $cms_id = $shop['id'];
            $items = $cmsItemModel->where('cms_id', $cms_id)
                                //   ->where('deleted !=', 1)
                                  ->findAll();
            $shop['items'] = $items;
        }
        
        
        return [
            'shop_by_categories' => $data
        ];
    }

    public function getSponsorAndItems_For_APIAppMedia()
    {
        // First, get all sponsor records
        $data = $this->where('type', 'sponsor')
                    //  ->where('active', 1)
                    //  ->where('deleted !=', 1)
                     ->findAll();
        
        // Second, on each sponsor record, get cms_items from epos_cms_items with given cms_id
        $cmsItemModel = new \App\Models\CmsItem();
        
        foreach ($data as &$sponsor) {
            $cms_id = $sponsor['id'];
            $items = $cmsItemModel->where('cms_id', $cms_id)
                                //   ->where('deleted !=', 1)
                                  ->findAll();
            $sponsor['items'] = $items;
        }
        
        
        return [
            'sponsors' => $data
        ];
    }

    public function getCmsAndItemsByType_For_APIAppMedia($cms_type)
    {
        // First, get all records by cms_type
        $data = $this->where('type', $cms_type)
                    //  ->where('active', 1)
                    //  ->where('deleted !=', 1)
                     ->findAll();
        
        // Second, on each record, get cms_items from epos_cms_items with given cms_id
        $cmsItemModel = new \App\Models\CmsItem();
        
        foreach ($data as &$record) {
            $cms_id = $record['id'];
            $items = $cmsItemModel->where('cms_id', $cms_id)
                                //   ->where('deleted !=', 1)
                                  ->findAll();
            $record['items'] = $items;
        }
        
        // Convert cms_type to plural form for response key
        $pluralKey = $cms_type . 's';
        // Handle special cases
        if ($cms_type === 'category_carousel') {
            $pluralKey = 'category_carousels';
        } elseif ($cms_type === 'products_carousel') {
            $pluralKey = 'products_carousels';
        } elseif ($cms_type === 'shop_by_category') {
            $pluralKey = 'shop_by_categories';
        } elseif ($cms_type === 'home_banner' || $cms_type === 'category_banner' || $cms_type === 'bottom_banner' || $cms_type === 'mega_menu_banner') {
            $pluralKey = str_replace('_banner', '_banners', $cms_type);
        }
        
        return [
            $pluralKey => $data
        ];
    }
}