<?php
namespace App\Controllers;

use App\Controllers\BaseController;
use App\Models\Branch;
use App\Models\Cms;

class APIAppMediaController extends BaseController
{
    /**
     * Helper method to read and parse XML file
     */
    private function getXmlFileData($filename)
    {
        try {
            $filePath = ROOTPATH . 'app_media_files' . DIRECTORY_SEPARATOR . $filename;
            
            if (!file_exists($filePath)) {
                return response()->setJSON([
                    'error' => 'File not found: ' . $filename
                ])->setStatusCode(404);
            }
            
            $fileContent = file_get_contents($filePath);
            $xml = simplexml_load_string($fileContent);
            
            if ($xml === false) {
                return response()->setJSON([
                    'error' => 'Failed to parse XML file: ' . $filename
                ])->setStatusCode(500);
            }
            
            // Convert SimpleXMLElement to array
            $json = json_encode($xml);
            $array = json_decode($json, true);
            
            return response()->setJSON([
                'success' => true,
                'data' => $array
            ]);
            
        } catch (\Exception $e) {
            return response()->setJSON([
                'error' => 'Failed to read file: ' . $e->getMessage()
            ])->setStatusCode(500);
        }
    }

    public function getDepotListV2()
    {
        // return $this->getXmlFileData('depot_list_v2.xml');
        $branchModel = new Branch();
        $data = $branchModel->getDepotListV2_For_APIAppMedia();
        return response()->setJSON($data);
    }

    public function getBrochureAndItems()
    {
        // return $this->getXmlFileData('brochure.xml');
        $cms = new Cms();
        $data = $cms->getBrochureAndItems_For_APIAppMedia();
        return response()->setJSON($data);
    }

    public function getBottomBannerAndItems()
    {
        $cms = new Cms();
        $data = $cms->getBottomBannerAndItems_For_APIAppMedia();
        return response()->setJSON($data);
    }

    public function getHomeBannerAndItems()
    {
        $cms = new Cms();
        $data = $cms->getHomeBannerAndItems_For_APIAppMedia();
        return response()->setJSON($data);
    }

    public function getCategoryBannerAndItems()
    {
        $cms = new Cms();
        $data = $cms->getCategoryBannerAndItems_For_APIAppMedia();
        return response()->setJSON($data);
    }

    public function getBrandAndItems()
    {
        $cms = new Cms();
        $data = $cms->getBrandAndItems_For_APIAppMedia();
        return response()->setJSON($data);
    }

    public function getCategoryCarouselAndItems()
    {
        $cms = new Cms();
        $data = $cms->getCategoryCarouselAndItems_For_APIAppMedia();
        return response()->setJSON($data);
    }

    public function getMultibuyAndItems()
    {
        $cms = new Cms();
        $data = $cms->getMultibuyAndItems_For_APIAppMedia();
        return response()->setJSON($data);
    }

    public function getProductsCarouselAndItems()
    {
        $cms = new Cms();
        $data = $cms->getProductsCarouselAndItems_For_APIAppMedia();
        return response()->setJSON($data);
    }

    public function getShopByCategoryAndItems()
    {
        $cms = new Cms();
        $data = $cms->getShopByCategoryAndItems_For_APIAppMedia();
        return response()->setJSON($data);
    }

    public function getSponsorAndItems()
    {
        $cms = new Cms();
        $data = $cms->getSponsorAndItems_For_APIAppMedia();
        return response()->setJSON($data);
    }

    public function getCmsAndItemsByType($cms_type = null)
    {
        // Get cms_type from route parameter or POST body
        if (empty($cms_type)) {
            $cms_type = request()->getVar('cms_type');
        }
        
        if (empty($cms_type)) {
            return response()->setJSON([
                'error' => 'cms_type parameter is required'
            ])->setStatusCode(400);
        }
        
        $cms = new Cms();
        $data = $cms->getCmsAndItemsByType_For_APIAppMedia($cms_type);
        return response()->setJSON($data);
    }



    public function getAllCmsAndItems()
    {
        $all = [];
        $branchModel = new Branch();
        $data = $branchModel->getDepotListV2_For_APIAppMedia();
        $all = array_merge($all, $data);

        $cms = new Cms();
        $data = $cms->getBrochureAndItems_For_APIAppMedia();
        $all = array_merge($all, $data);
        $data = $cms->getBottomBannerAndItems_For_APIAppMedia();
        $all = array_merge($all, $data);
        $data = $cms->getHomeBannerAndItems_For_APIAppMedia();
        $all = array_merge($all, $data);
        $data = $cms->getCategoryBannerAndItems_For_APIAppMedia();
        $all = array_merge($all, $data);
        $data = $cms->getBrandAndItems_For_APIAppMedia();
        $all = array_merge($all, $data);
        $data = $cms->getCategoryCarouselAndItems_For_APIAppMedia();
        $all = array_merge($all, $data);
        $data = $cms->getMultibuyAndItems_For_APIAppMedia();
        $all = array_merge($all, $data);
        $data = $cms->getProductsCarouselAndItems_For_APIAppMedia();
        $all = array_merge($all, $data);
        $data = $cms->getShopByCategoryAndItems_For_APIAppMedia();
        $all = array_merge($all, $data);
        $data = $cms->getSponsorAndItems_For_APIAppMedia();
        $all = array_merge($all, $data);
      
        return response()->setJSON($all);
    }
}

