<?php  
require_once "config.php";  
$conn->set_charset("utf8mb4");

$type       = isset($_POST['Type']) ? trim($_POST['Type']) : '';  
$match_type = isset($_POST['match_type']) ? trim($_POST['match_type']) : '';  
$rules      = isset($_POST['rules']) ? trim($_POST['rules']) : '';  


$admin_username = $_POST['admin_username']; // POST থেকে username নেওয়া
    $admin_password = $_POST['admin_password'];

    // Prepared statement — username ব্যবহার করবে (email নয়)
    $admin_stmt = $conn->prepare("SELECT 1 FROM admin WHERE username = ? AND password = ?");
    $admin_stmt->bind_param("ss", $admin_username, $admin_password);
    $admin_stmt->execute();
    $admin_result = $admin_stmt->get_result();

    if ($admin_result->num_rows === 0) {
        http_response_code(403);
        echo json_encode([
            "status" => "error",
            "message" => "Access Denied"
        ], JSON_PRETTY_PRINT);
        $admin_stmt->close();
        $conn->close();
        exit();
    }
    $admin_stmt->close();




if ($type === '' || $match_type === '' || $rules === '') {  
    echo "Missing fields";  
    exit;  
}  

if (strcasecmp($type, "Add") === 0) {  
    $stmt = $conn->prepare("INSERT INTO match_types(type, rules) VALUES(?, ?) ON DUPLICATE KEY UPDATE rules=VALUES(rules)");  
    $stmt->bind_param("ss", $match_type, $rules);  

    if ($stmt->execute()) {  
        echo "Rulse Added Successfully";  
    } else {  
        echo "Insert failed";  
    }  
    $stmt->close();  
}  
elseif (strcasecmp($type, "Update") === 0) {  
    $stmt = $conn->prepare("UPDATE match_types SET rules=? WHERE type=?");  
    $stmt->bind_param("ss", $rules, $match_type);  

    if ($stmt->execute() && $stmt->affected_rows > 0) {  
        echo "Match Type Updated: " . $match_type;  
    } else {  
        echo "Update failed or not found";  
    }  
    $stmt->close();  
}  
else {  
    echo "Invalid Type param";  
}