<?php
session_start();
require_once '../config.php';
require_once '../app/classes/AdminAuth.php';

$adminAuth = new AdminAuth($conn);

// Check admin authentication
if (!$adminAuth->isAuthenticated()) {
    header("Location: admin_login.php");
    exit();
}

// Get the page parameter
$page = $_GET['page'] ?? 'dashboard';

// Handle delete agent action
if (isset($_GET['action']) && $_GET['action'] === 'delete' && isset($_GET['id']) && is_numeric($_GET['id'])) {
    $agentId = (int) $_GET['id'];
    
    // Delete the agent
    $stmt = mysqli_prepare($conn, "DELETE FROM agents WHERE id = ?");
    mysqli_stmt_bind_param($stmt, "i", $agentId);
    mysqli_stmt_execute($stmt);
    
    // Check if any row was deleted
    if (mysqli_stmt_affected_rows($stmt) > 0) {
        // Log the deletion
        error_log("Agent deleted: ID " . $agentId . " by admin " . $_SESSION['admin_username']);
        
        // Redirect back to dashboard with success message
        header("Location: index.php?page=dashboard&deleted=1");
        exit();
    } else {
        // Redirect with error message
        header("Location: index.php?page=dashboard&error=agent_not_found");
        exit();
    }
    
    mysqli_stmt_close($stmt);
}

// Define allowed pages
$allowedPages = [
    'dashboard' => 'Admin Dashboard',
    'orders' => 'Orders Management',
    'orders_management' => 'Orders Management (Detailed)',
    'all_orders' => 'All Orders',
    'analytics' => 'Sales Analytics',
    'sales_analytics' => 'Sales Analytics (Detailed)',
    'monitor' => 'Status Monitor',
    'order_status_monitor' => 'Order Status Monitor',
    'balance_history' => 'Balance History',
    'balance_history_admin' => 'Balance History (Admin)',
    'deposits' => 'Agent Deposits',
    'pricing' => 'Agent Pricing Rules', 
    'payment_config' => 'Payment Configuration',
    'minimum_topup_config'=> 'Minimum Topup',
    'paystack_charge_config' => 'Paystack Charge Configuration',
    'external_api_pricing' => 'External API Pricing',
    'external_api_profit' => 'External API Profit Tracking',
    'subagent_topups' => 'Sub-Agent Top-ups',
    'api_config' => 'API Configuration',
    'api_keys' => 'API Keys Management',
    'notifications' => 'Notification Management',
    'notification_management' => 'Notification Management (Detailed)',
    'banner_notifications' => 'Banner Notifications',
    'low_balance_alert_config' => 'Low Balance Alert',
    'delete_old_orders' => 'Delete Old Orders',
    'user_activity' => 'User Login Activity',
    'account_management' => 'Account Management',
    'password_management' => 'Password Management',
    'profile' => 'Admin Profile',
    'api_error_logs' => 'API Error Logs',
    'data_integration' => 'Data Integration Configuration'
];

// Validate page parameter
if (!array_key_exists($page, $allowedPages)) {
    $page = 'dashboard';
}

// Set page title and description
$page_title = $allowedPages[$page];
$page_description = 'Manage and monitor your D-Bundles Stores platform';

// Include the header
include 'includes/header.php';

// Include the appropriate page content
switch ($page) {
    case 'dashboard':
        include 'pages/admin_dashboard.php';
        break;
    case 'orders':
    case 'orders_management':
        include 'pages/orders_management.php';
        break;
    case 'all_orders':
        include 'pages/all_orders.php';
        break;
    case 'analytics':
    case 'sales_analytics':
        include 'pages/sales_analytics.php';
        break;
    case 'balance_history':
    case 'balance_history_admin':
        include 'pages/balance_history_admin.php';
        break;
    case 'deposits':
        // You can create a deposits page or redirect to existing functionality
        include 'pages/records.php';
        break;
    case 'pricing':
        // You can create a pricing page or redirect to existing functionality
        include 'pages/agent_pricing.php'; // Updated to agent_pricing.php
        break;
    case 'external_api_pricing':
        include 'pages/external_api_pricing.php';
        break;
    case 'external_api_profit':
        include 'pages/external_api_profit.php';
        break;
    case 'subagent_topups':
        include 'pages/subagent_topups.php';
        break;
    case 'payment_config':
        include 'pages/payment_config.php'; // Updated to payment_config.php
        break;
    case 'minimum_topup_config':
        include 'pages/minimum_topup_config.php';
        break;
    case 'paystack_charge_config':
        include 'pages/paystack_charge_config.php';
        break;
    case 'api_config':
        include 'pages/api_config.php';
        break;
    case 'api_keys':
        include 'pages/api_keys_management.php';
        break;
    case 'notifications':
    case 'notification_management':
        include 'pages/notification_management.php';
        break;
    case 'banner_notifications':
        include 'pages/banner_notifications.php';
        break;
    case 'low_balance_alert_config':
        include 'pages/low_balance_alert_config.php';
        break;
    case 'delete_old_orders':
        include 'pages/delete_old_orders.php';
        break;
    case 'user_activity':
        include 'pages/user_login_activity.php';
        break;
    case 'account_management':
        include 'pages/account_management.php';
        break;
    case 'password_management':
        include 'pages/password_management.php';
        break;
    case 'profile':
        include 'pages/admin_profile.php';
        break;
    case 'api_error_logs':
        include 'pages/api_error_logs.php';
        break;
    case 'data_integration':
        include 'pages/data_integration_config.php';
        break;
    default:
        include 'pages/admin_dashboard.php';
        break;
}

// Include the footer
include 'includes/footer.php';
?>
