<?php

namespace App\Controllers\Home;

use App\Controllers\BaseController;

class HomeController extends BaseController
{
    public function services(){
        $session = session();
        $jwtToken = $session->get('jwt_token');
        $userId = $session->get('user_id');

        if (!$jwtToken || !$userId) {
            return redirect()->back()->with('error', 'Unauthorized access.');
        }

        try {

            $client = \Config\Services::curlrequest();
            $response = $client->get(base_url() . 'api/home/get-services', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $jwtToken,
                    'Accept'        => 'application/json',
                ],
                'verify' => false
            ]);

            

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

        } catch (\Exception $e) {
            return redirect()->back()->with('error', 'API error: ' . $e->getMessage());
        }
    }
    public function academicPrograms(){
        $session = session();
        $jwtToken = $session->get('jwt_token');
        $userId = $session->get('user_id');

        if (!$jwtToken || !$userId) {
            return redirect()->back()->with('error', 'Unauthorized access.');
        }

        try {

            $client = \Config\Services::curlrequest();
            $response = $client->get(base_url() . 'api/home/get-academic-programs', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $jwtToken,
                    'Accept'        => 'application/json',
                ],
                'verify' => false
            ]);

            

            $apiResponse = json_decode($response->getBody(), true);
           
            $data['academic'] = $apiResponse['data'] ?? [];
            return view('home/academic_programs', $data);

        } catch (\Exception $e) {
            return redirect()->back()->with('error', 'API error: ' . $e->getMessage());
        }
    }

    public function tenderNoticesList()
    {
        $session = session();

        $data['page_title'] = "Tender & Notices || " . COMPANY_SHORT_NAME;
        $data['meta_tag'] = '<meta content="' . COMPANY_FULL_NAME . ', Sketch Me Global" name="keywords">'
                        . '<meta content="Tender & Notices, ' . COMPANY_FULL_NAME . ', Sketch Me Global" name="description">';

        $jwtToken = session('jwt_token');
        $userId = session('user_id');

        if (!$jwtToken || !$userId) {
            return $this->response->setJSON([
                'status' => 401,
                'message' => 'Unauthorized',
            ]);
        }
       
        try {
            $client = \Config\Services::curlrequest();
            $response = $client->get(base_url() . 'api/home/get-notices-list', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $jwtToken,
                    'Accept'        => 'application/json',
                ],
                'query' => [
                    'row_status' => 1
                ],
                'verify' => false
            ]);

            $apiResponse = json_decode($response->getBody(), true);
              

            if (isset($apiResponse['status']) && $apiResponse['status'] == 200) {
                $data['notices'] = $apiResponse['data'] ?? [];
            } else {
                $data['notices'] = [];
                $data['error'] = $apiResponse['message'] ?? 'Unable to fetch notices list';
            }
         
        } catch (\Exception $e) {
            log_message('error', 'notices API Call Failed: ' . $e->getMessage());
            $data['notices'] = [];
            $data['error'] = 'Something went wrong while fetching notices.';
        }

        return view('home/tender_notices/list', $data);
    }
    public function addHomeNotices()
    {
        $session = session();

        $data['page_title'] = "Add Teander & Notices || " . COMPANY_SHORT_NAME;
        $data['meta_tag'] = '<meta content="' . COMPANY_FULL_NAME . ', Sketch Me Global" name="keywords">'
                        . '<meta content="eander & Notice, ' . COMPANY_FULL_NAME . ', Sketch Me Global" name="description">';

        return view('home/tender_notices/add', $data);
    }
    public function editNotices($id)
    {
        $session = session();
        $jwtToken = $session->get('jwt_token');
        $userId = $session->get('user_id');

        if (!$jwtToken || !$userId) {
            return redirect()->back()->with('error', 'Unauthorized access.');
        }

        try {
            $client = \Config\Services::curlrequest();
            $response = $client->get(base_url() . 'api/home/get-notice-by-id', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $jwtToken,
                    'Accept'        => 'application/json',
                ],
                'query' => [
                    'id' => $id
                ],
                'verify' => false
            ]);

            $apiResponse = json_decode($response->getBody(), true);

            if (isset($apiResponse['status']) && $apiResponse['status'] === true) {
                $data['notice'] = $apiResponse['data'];
                return view('home/tender_notices/edit', $data);
            } else {
                return redirect()->back()->with('error', 'Notice not found.');
            }
        } catch (\Exception $e) {
            return redirect()->back()->with('error', 'API error: ' . $e->getMessage());
        }
    }

    public function deleteNotices($id)
    {
        $session = session();
        $jwtToken = $session->get('jwt_token');
        $userId   = $session->get('user_id');

        if (!$jwtToken || !$userId) {
            return redirect()->back()->with('error', 'Unauthorized action.');
        }

        try {
            $client = \Config\Services::curlrequest();
            $response = $client->post(base_url() . 'api/home/delete-notice', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $jwtToken,
                    'Accept'        => 'application/json',
                ],
                'form_params' => [
                    'id' => $id
                ],
                'verify' => false
            ]);

            $apiResponse = json_decode($response->getBody(), true);

            if (isset($apiResponse['status']) && $apiResponse['status'] === true) {
                return redirect()->to(base_url('backdesk/home-tender-notices/index'))
                                ->with('success', $apiResponse['message'] ?? 'Notice deleted successfully.');
            } else {
                return redirect()->back()->with('error', $apiResponse['message'] ?? 'Failed to delete notice.');
            }

        } catch (\Exception $e) {
            return redirect()->back()->with('error', 'Error: ' . $e->getMessage());
        }
    }
     public function basic_details(){
        $session = session();
        $jwtToken = $session->get('jwt_token');
        $userId = $session->get('user_id');

        if (!$jwtToken || !$userId) {
            return redirect()->back()->with('error', 'Unauthorized access.');
        }

        try {

            $client = \Config\Services::curlrequest();
            $response = $client->get(base_url() . 'api/home/get-basic-details', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $jwtToken,
                    'Accept'        => 'application/json',
                ],
                'verify' => false
            ]);

            

            $apiResponse = json_decode($response->getBody(), true);
           
            $data['company'] = $apiResponse['data'] ?? [];
            return view('home/basic_details', $data);

        } catch (\Exception $e) {
            return redirect()->back()->with('error', 'API error: ' . $e->getMessage());
        }
    }

}
?>