<?php
/**
 * Update agent status (suspend/activate)
 * Only store owners can update their agents' status
 */

session_start();
require_once '../config.php';
require_once '../app/classes/StoreManager.php';

header('Content-Type: application/json');

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    echo json_encode(['success' => false, 'message' => 'Method not allowed']);
    exit;
}

// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
    echo json_encode(['success' => false, 'message' => 'Authentication required']);
    exit;
}

$userId = $_SESSION['user_id'];
$agentId = intval($_POST['agent_id'] ?? 0);
$status = trim($_POST['status'] ?? '');

// Validate input
if ($agentId <= 0) {
    echo json_encode(['success' => false, 'message' => 'Invalid agent ID']);
    exit;
}

if (!in_array($status, ['active', 'suspended'])) {
    echo json_encode(['success' => false, 'message' => 'Invalid status. Must be "active" or "suspended"']);
    exit;
}

// Verify user owns a store
$storeManager = new StoreManager($conn);
$store = $storeManager->getStoreByOwnerId($userId);

if (!$store) {
    echo json_encode(['success' => false, 'message' => 'Store not found']);
    exit;
}

// Update agent status
$result = $storeManager->updateAgentStatus($store['id'], $agentId, $status);

echo json_encode($result);
?>





















