<?php
require_once '../config.php';
require_once '../../includes/Auth.php';
require_once '../../includes/Database.php';

$auth = new Auth();
$auth->requireRole('admin');

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

$input = json_decode(file_get_contents('php://input'), true);
$shop_id = $input['shop_id'] ?? null;
$is_approved = isset($input['is_approved']) ? (bool)$input['is_approved'] : false;

if (!$shop_id) {
    jsonResponse(['success' => false, 'message' => 'Shop ID is required'], 400);
}

try {
    $db = new Database();
    $db->query(
        "UPDATE shops SET is_approved = ? WHERE id = ?",
        [$is_approved, $shop_id]
    );

    $message = $is_approved ? 'Shop approved successfully' : 'Shop approval revoked';
    jsonResponse(['success' => true, 'message' => $message]);

} catch (Exception $e) {
    jsonResponse(['success' => false, 'message' => $e->getMessage()], 500);
}
?>