<?php
/**
 * Check the current status of the API module
 */

// Define necessary constants
define('BASEPATH', true);
define('APPPATH', 'application/');
define('ENVIRONMENT', 'testing');

echo "Checking API module status...\n\n";

try {
    // Try to load Perfex framework
    require_once '../index.php';

    $CI =& get_instance();

    echo "Framework loaded successfully\n";

    // Check if app_modules is available
    if (isset($CI->app_modules)) {
        echo "✅ app_modules library is available\n";

        // Check module status
        $is_active = $CI->app_modules->is_active('api');
        echo "API module active: " . ($is_active ? 'YES' : 'NO') . "\n";

        // Get list of all modules
        $all_modules = $CI->app_modules->get();
        echo "\nAll installed modules:\n";
        foreach ($all_modules as $module) {
            echo "- " . $module['name'] . " (" . $module['system_name'] . "): " . ($module['active'] ? 'ACTIVE' : 'INACTIVE') . "\n";
        }

        // Check if API module exists
        $api_module = $CI->app_modules->get('api');
        if ($api_module) {
            echo "\nAPI module details:\n";
            print_r($api_module);
        } else {
            echo "\n❌ API module not found in modules list\n";
        }

    } else {
        echo "❌ app_modules library not available\n";
    }

    // Check database connection
    if (isset($CI->db)) {
        echo "\n✅ Database connection available\n";

        // Check for modules table
        $tables = $CI->db->list_tables();
        if (in_array('modules', $tables)) {
            echo "✅ modules table exists\n";

            // Check API module in database
            $CI->db->where('module_name', 'api');
            $query = $CI->db->get('modules');
            if ($query->num_rows() > 0) {
                $module_data = $query->row_array();
                echo "API module in database: " . print_r($module_data, true) . "\n";
            } else {
                echo "❌ API module not found in database\n";
            }
        } else {
            echo "❌ modules table does not exist\n";
        }
    } else {
        echo "❌ Database connection not available\n";
    }

} catch (Exception $e) {
    echo "❌ ERROR: " . $e->getMessage() . "\n";
    echo "Stack trace:\n" . $e->getTraceAsString() . "\n";
}
?>