<?php
include 'db.php';
header('Content-Type: application/json');

// Check if user is logged in (basic security check)
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Get JSON input
$input = json_decode(file_get_contents('php://input'), true);
$action = $input['action'] ?? '';

if ($action !== 'clear_license') {
    echo json_encode(['success' => false, 'message' => 'Invalid action.']);
    exit;
}

try {
    // Truncate the branch_info table to completely clear all license data
    $stmt = $conn->prepare("TRUNCATE TABLE branch_info");
    
    if ($stmt->execute()) {
        // Also clear any session data related to license
        if (isset($_SESSION['license_verified'])) {
            unset($_SESSION['license_verified']);
        }
        
        echo json_encode([
            'success' => true, 
            'message' => 'License data cleared successfully. System will require re-activation.'
        ]);
    } else {
        echo json_encode([
            'success' => false, 
            'message' => 'Failed to clear license data from database.'
        ]);
    }
    
} catch (Exception $e) {
    echo json_encode([
        'success' => false, 
        'message' => 'Database error: ' . $e->getMessage()
    ]);
}
?>
