<?php

namespace App\Controllers;

class Home extends BaseController
{
    public function index(): string
    {
            $client = \Config\Services::curlrequest();
            $baseUrl = env('API_BASE_URL');
            $headerResponse = $client->get($baseUrl .'home/get-basic-details', [
                'headers' => [
                    'Accept'        => 'application/json',
                    'X-Requested-From'    => 'frontend'
                ],
                'verify' => false
            ]);
            $apiHeaderResponse = json_decode($headerResponse->getBody(), true);
            $data['header'] = $apiHeaderResponse['data'] ?? [];

            $menuResponse = $client->get($baseUrl .'menu/get-menu-list', [
                'headers' => [
                    'Accept'        => 'application/json',
                    'X-Requested-From'    => 'frontend'
                ],
                'verify' => false
            ]);
            $apiMenuResponse = json_decode($menuResponse->getBody(), true);
           
            $data['menu'] = $apiMenuResponse['data'] ?? [];
            $data['menuTree'] = $this->buildMenuTree($data['menu']);

            $departmentResponse = $client->get($baseUrl .'departments/get-department-list', [
                'headers' => [
                    'Accept'        => 'application/json',
                    'X-Requested-From'    => 'frontend'
                ],
                'verify' => false
            ]);
            $apiDepartmentResponse = json_decode($departmentResponse->getBody(), true);
            $data['departments'] = $apiDepartmentResponse['data'] ?? [];

       
            $client = \Config\Services::curlrequest();
            $response = $client->get($baseUrl .'home/get-services', [
                'headers' => [
                    'Accept'        => 'application/json',
                    'X-Requested-From'    => 'frontend'
                ],
                'verify' => false
            ]);

            

            $apiResponse = json_decode($response->getBody(), true);
           
            $data['services'] = $apiResponse['data'] ?? [];

            $academicresponse = $client->get($baseUrl .'home/get-academic-programs', [
                'headers' => [
                    'Accept'        => 'application/json',
                    'X-Requested-From'    => 'frontend'
                ],
                'verify' => false
            ]);

            

            $apiAcademicResponse = json_decode($academicresponse->getBody(), true);
           
            $data['academic'] = $apiAcademicResponse['data'] ?? [];

            $noticesresponse = $client->get($baseUrl .'home/get-notices-list', [
                'headers' => [
                    'Accept'        => 'application/json',
                    'X-Requested-From'    => 'frontend'
                ],
                'verify' => false
            ]);

            

            $apiNoticesResponse = json_decode($noticesresponse->getBody(), true);
           
            $data['notices'] = $apiNoticesResponse['data'] ?? [];
            return view('home', $data);

    }
    public function contact_us(): string
    {
        $client = \Config\Services::curlrequest();
        $baseUrl = env('API_BASE_URL');
        $headerResponse = $client->get($baseUrl .'home/get-basic-details', [
                'headers' => [
                    'Accept'        => 'application/json',
                    'X-Requested-From'    => 'frontend'
                ],
                'verify' => false
            ]);
        $apiHeaderResponse = json_decode($headerResponse->getBody(), true);
        $data['header'] = $apiHeaderResponse['data'] ?? [];
        // Fetch menu
        $menuResponse = $client->get($baseUrl .'menu/get-menu-list', [
            'headers' => [
                'Accept' => 'application/json',
                'X-Requested-From' => 'frontend'
            ],
            'verify' => false
        ]);
        $apiMenuResponse = json_decode($menuResponse->getBody(), true);

        $data['menu'] = $apiMenuResponse['data'] ?? [];
        $data['menuTree'] = $this->buildMenuTree($data['menu']);

        $departmentResponse = $client->get($baseUrl .'departments/get-department-list', [
                'headers' => [
                    'Accept'        => 'application/json',
                    'X-Requested-From'    => 'frontend'
                ],
                'verify' => false
            ]);
        $apiDepartmentResponse = json_decode($departmentResponse->getBody(), true);
        $data['departments'] = $apiDepartmentResponse['data'] ?? [];

        // Fetch contact page data
        $contactPageApi = $client->get($baseUrl .'contact/get-contactpage-data', [
            'headers' => [
                'Accept' => 'application/json',
                'X-Requested-From' => 'frontend'
            ],
            'verify' => false
        ]);
        $contactPageApiResponse = json_decode($contactPageApi->getBody(), true);

        // Merge contact data into $data
        $contactData = $contactPageApiResponse['data'] ?? [];
        $data = array_merge($data, $contactData);

        return view('contactus', $data);
    }

    public function page($slug,$id)
    {
        $client = \Config\Services::curlrequest();
        $baseUrl = env('API_BASE_URL');
        $headerResponse = $client->get($baseUrl .'home/get-basic-details', [
                'headers' => [
                    'Accept'        => 'application/json',
                    'X-Requested-From'    => 'frontend'
                ],
                'verify' => false
            ]);
        $apiHeaderResponse = json_decode($headerResponse->getBody(), true);
        $header= $apiHeaderResponse['data'] ?? [];

        $menuResponse = $client->get($baseUrl .'menu/get-menu-list', [
            'headers' => [
                'Accept' => 'application/json',
                'X-Requested-From' => 'frontend'
            ],
            'verify' => false
        ]);
        $apiMenuResponse = json_decode($menuResponse->getBody(), true);
       

        $data['menu'] = $apiMenuResponse['data'] ?? [];
        $menuTree = $this->buildMenuTree($data['menu']);


        $departmentResponse = $client->get($baseUrl .'departments/get-department-list', [
                'headers' => [
                    'Accept'        => 'application/json',
                    'X-Requested-From'    => 'frontend'
                ],
                'verify' => false
            ]);
        $apiDepartmentResponse = json_decode($departmentResponse->getBody(), true);
        $data['departments'] = $apiDepartmentResponse['data'] ?? [];

        $pageDataApi = $client->get($baseUrl .'page-contents/get-page-data', [
            'headers' => [
                'Accept' => 'application/json',
                'X-Requested-From' => 'frontend'
            ],
            'query' => [
                'id' => $id,
            ],
            'verify' => false
        ]);

        $pageDataApiResponse = json_decode($pageDataApi->getBody(), true);
        $pageData = $pageDataApiResponse['data'] ?? [];

        $template = "";
        if(!empty($data['menu'])){
            foreach($data['menu'] as $row){
                if($row['id'] == $id){
                    $template = $row['template'];
                }
            }
        }
        $title = ucwords(str_replace('-', ' ', $slug));
        $data = [
            'page' => $pageData,
            'header' =>$header,
            'menuTree' => $menuTree,
            'page_name' => $title
        ];
        if($template === "Template 1"){
            return view('pages', $data);
        }else{
            return view('page2', $data);
        }
        
    }

    public function departments($id){
        $client = \Config\Services::curlrequest();
        $baseUrl = env('API_BASE_URL');
        $headerResponse = $client->get($baseUrl .'home/get-basic-details', [
                'headers' => [
                    'Accept'        => 'application/json',
                    'X-Requested-From'    => 'frontend'
                ],
                'verify' => false
            ]);
        $apiHeaderResponse = json_decode($headerResponse->getBody(), true);
        $data['header'] = $apiHeaderResponse['data'] ?? [];
        $menuResponse = $client->get($baseUrl .'menu/get-menu-list', [
            'headers' => [
                'Accept' => 'application/json',
                'X-Requested-From' => 'frontend'
            ],
            'verify' => false
        ]);
        $apiMenuResponse = json_decode($menuResponse->getBody(), true);

        $data['menu'] = $apiMenuResponse['data'] ?? [];
        $menuTree = $this->buildMenuTree($data['menu']);
       

        $departmentResponse = $client->get($baseUrl .'departments/get-department-list', [
                'headers' => [
                    'Accept'        => 'application/json',
                    'X-Requested-From'    => 'frontend'
                ],
                'verify' => false
            ]);
        $apiDepartmentResponse = json_decode($departmentResponse->getBody(), true);
        $data['departments'] = $apiDepartmentResponse['data'] ?? [];
      
        
        $departmentDetailsResponse = $client->get($baseUrl .'departments/get-department-all-details', [
                'headers' => [
                    'Accept'        => 'application/json',
                    'X-Requested-From'    => 'frontend'
                ],
                'query' => [
                    'id' => $id,
                ],
                'verify' => false
            ]);
        $apiDepartmentDetailsResponse = json_decode($departmentDetailsResponse->getBody(), true);
        $department_details = $apiDepartmentDetailsResponse['data'] ?? [];
        // echo "<pre>";
        // print_r($data['department_details']);
        // die;

        $data = [
            'menuTree' => $menuTree,
            'department_details' => $department_details
        ];

        return view('department', $data);
    }

    public function central_library(){
        $client = \Config\Services::curlrequest();
        $baseUrl = env('API_BASE_URL');
        $headerResponse = $client->get($baseUrl .'home/get-basic-details', [
                'headers' => [
                    'Accept'        => 'application/json',
                    'X-Requested-From'    => 'frontend'
                ],
                'verify' => false
            ]);
        $apiHeaderResponse = json_decode($headerResponse->getBody(), true);
        $data['header'] = $apiHeaderResponse['data'] ?? [];
        // Fetch menu
        $menuResponse = $client->get($baseUrl .'menu/get-menu-list', [
            'headers' => [
                'Accept' => 'application/json',
                'X-Requested-From' => 'frontend'
            ],
            'verify' => false
        ]);
        $apiMenuResponse = json_decode($menuResponse->getBody(), true);

        $data['menu'] = $apiMenuResponse['data'] ?? [];
        $data['menuTree'] = $this->buildMenuTree($data['menu']);

        $departmentResponse = $client->get($baseUrl .'departments/get-department-list', [
                'headers' => [
                    'Accept'        => 'application/json',
                    'X-Requested-From'    => 'frontend'
                ],
                'verify' => false
            ]);
        $apiDepartmentResponse = json_decode($departmentResponse->getBody(), true);
        $data['departments'] = $apiDepartmentResponse['data'] ?? [];

        // Fetch library data
        $libraryApi = $client->get($baseUrl .'library/get-library-data', [
            'headers' => [
                'Accept' => 'application/json',
                'X-Requested-From' => 'frontend'
            ],
            'verify' => false
        ]);
        $libraryApiResponse = json_decode($libraryApi->getBody(), true);

        $data['library_details'] = $libraryApiResponse['data'] ?? [];
        

        return view('central_library', $data);
    }
    public function question_papers($year,$courseId){
        $client = \Config\Services::curlrequest();
        $baseUrl = env('API_BASE_URL');
        $headerResponse = $client->get($baseUrl .'home/get-basic-details', [
                'headers' => [
                    'Accept'        => 'application/json',
                    'X-Requested-From'    => 'frontend'
                ],
                'verify' => false
            ]);
        $apiHeaderResponse = json_decode($headerResponse->getBody(), true);
        $data['header'] = $apiHeaderResponse['data'] ?? [];
        // Fetch menu
        $menuResponse = $client->get($baseUrl .'menu/get-menu-list', [
            'headers' => [
                'Accept' => 'application/json',
                'X-Requested-From' => 'frontend'
            ],
            'verify' => false
        ]);
        $apiMenuResponse = json_decode($menuResponse->getBody(), true);

        $data['menu'] = $apiMenuResponse['data'] ?? [];
        $data['menuTree'] = $this->buildMenuTree($data['menu']);

        $departmentResponse = $client->get($baseUrl .'departments/get-department-list', [
                'headers' => [
                    'Accept'        => 'application/json',
                    'X-Requested-From'    => 'frontend'
                ],
                'verify' => false
            ]);
        $apiDepartmentResponse = json_decode($departmentResponse->getBody(), true);
        $data['departments'] = $apiDepartmentResponse['data'] ?? [];

        // Fetch library data
        $papersApi = $client->get($baseUrl .'courses/get-papers-data', [
            'headers' => [
                'Accept' => 'application/json',
                'X-Requested-From' => 'frontend'
            ],
            'query' => [
                    'courseId' => $courseId,
                    'year' => $year
                ],
            'verify' => false
        ]);
        $papersApiResponse = json_decode($papersApi->getBody(), true);

        $data['papers'] = $papersApiResponse['data'] ?? [];
        

        return view('course_papers', $data);
    }



    private function buildMenuTree(array $menus): array
    {
        $menuMap = [];
        $menuTree = [];

        foreach ($menus as &$menu) {
            // Generate a clean slug
            $slug = strtolower($menu['name']);                  
            $slug = preg_replace('/[^a-z0-9\s-]/', '', $slug);   
            $slug = preg_replace('/[\s-]+/', '-', $slug);      
            $slug = trim($slug, '-');                           

            $menu['slug'] = $slug;
            $menu['children'] = [];

            $menuMap[$menu['id']] = &$menu;
        }

        foreach ($menus as &$menu) {
            if ($menu['parent_id'] == 0) {
                $menuTree[] = &$menu;
            } else {
                if (isset($menuMap[$menu['parent_id']])) {
                    $menuMap[$menu['parent_id']]['children'][] = &$menu;
                }
            }
        }

        return $menuTree;
    }


}
