<?php
/**
 * Force activate the API module by directly updating the database
 * Run this from the Perfex CRM root directory
 */

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

echo "Force activating API module...\n\n";

try {
    // Include the main index file to load the framework
    require_once '../index.php';

    $CI =& get_instance();

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

    if (!$is_active) {
        echo "Attempting to activate API module...\n";

        // Try to activate the module
        $result = $CI->app_modules->activate('api');

        if ($result) {
            echo "✅ SUCCESS: API module activated successfully!\n";
        } else {
            echo "❌ FAILED: Could not activate module through standard method\n";

            // Try direct database approach
            echo "Trying direct database activation...\n";

            // Check if modules table exists and what the structure is
            $CI->db->select('*');
            $CI->db->from('modules');
            $query = $CI->db->get();

            if ($query->num_rows() > 0) {
                echo "Found modules table. Checking for API module entry...\n";

                // Check if API module exists in database
                $CI->db->where('module_name', 'api');
                $existing = $CI->db->get('modules');

                if ($existing->num_rows() == 0) {
                    // Insert API module into database
                    $module_data = [
                        'module_name' => 'api',
                        'installed_version' => '2.1.0',
                        'active' => 1,
                        'created_at' => date('Y-m-d H:i:s')
                    ];

                    $CI->db->insert('modules', $module_data);
                    echo "✅ Inserted API module into database\n";
                } else {
                    // Update existing entry to active
                    $CI->db->where('module_name', 'api');
                    $CI->db->update('modules', ['active' => 1]);
                    echo "✅ Updated API module to active in database\n";
                }
            } else {
                echo "❌ No modules table found. This might be a different CRM version.\n";
            }
        }
    } else {
        echo "✅ API module is already active\n";
    }

    // Verify activation
    echo "\nVerifying activation...\n";
    $is_active_after = $CI->app_modules->is_active('api');
    echo "API module active after activation: " . ($is_active_after ? 'YES' : 'NO') . "\n";

    if ($is_active_after) {
        echo "\n🎉 SUCCESS: API module is now active and ready to use!\n";
        echo "You can now test API endpoints with your JWT token.\n";
    } else {
        echo "\n❌ FAILED: API module could not be activated.\n";
        echo "You may need to activate it manually through the admin panel.\n";
    }

} catch (Exception $e) {
    echo "❌ ERROR: " . $e->getMessage() . "\n";
    echo "This might indicate a database connection issue or missing dependencies.\n";
}
?>