<?php
class AccessControl
{


    public function checkClientStatus()
    {

        $CI = &get_instance();
        $CI->load->helper(['application_helper', 'url']);

        // Example: Client ID stored in session or constant
        $client_id = ApiBaseUrl()['clientID']; // Or fetch from DB/session/config

        // Call external API
        $ch = curl_init(ApiBaseUrl()['url'] . '/api/webgen/getclientInfoById');
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['_id' => $client_id]));

        $response = curl_exec($ch);
        curl_close($ch);

        $result = json_decode($response, true);

        // Optional: Debug
        // print_r($result); exit;
        $result['status'] = @$result['status'] ? $result['status'] : 'ACTIVE';
        // var_dump($result['status']);die();
        $class = $CI->router->fetch_class();
        $method = $CI->router->fetch_method();
        if (isset($result['status']) && $result['status'] != 'ACTIVE') {

            // var_dump($method);
            // var_dump($class);die();
            // Allow only default controller
            if (!($class == 'Home' && $method == 'terminate')) {
                redirect('Home/terminate'); // or show custom "account inactive" page
                // exit;
            }
        } else {
            if ($method == 'terminate') {
                redirect('Home');
            }
        }
    }
}
