<?php
include 'config.php';

date_default_timezone_set("Asia/Dhaka");

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    // =========================
    // ADMIN CHECK
    // =========================
    $admin_username = $_POST['admin_username'] ?? '';
    $admin_password = $_POST['admin_password'] ?? '';

    $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"
        ]);
        exit();
    }

    $admin_stmt->close();

    // =========================
    // INPUT
    // =========================

    $match_title = $_POST['match_title'] ?? '';
    $total_match = (int)($_POST['total_match'] ?? 0);
    $start_datetime = $_POST['start_datetime'] ?? '';
    $interval_minute = (int)($_POST['interval_minute'] ?? 30);

    $match_time = $_POST['match_time'] ?? '';
    $total_prize = $_POST['total_prize'] ?? '';
    $per_kill = $_POST['per_kill'] ?? '';
    $entry_fee = $_POST['entry_fee'] ?? '';
    $entry_type = $_POST['entry_type'] ?? '';
    $match_type = $_POST['match_type'] ?? '';
    $version = $_POST['version'] ?? '';
    $play_map = $_POST['play_map'] ?? '';
    $registration = $_POST['registration'] ?? '';
    $total_player = $_POST['total_player'] ?? '';
    $prize_details = $_POST['prize_details'] ?? '';
    $timer = $_POST['timer'] ?? '';
    $join_type = $_POST['join_type'] ?? '';

    if (!$match_title  !$start_datetime  $total_match <= 0) {
        echo json_encode([
            "status" => "error",
            "message" => "Invalid Input Data"
        ]);
        exit();
    }

    // =========================
    // TIME PARSE (12H SUPPORT)
    // =========================
    $start_time = strtotime($start_datetime);

    if (!$start_time) {
        echo json_encode([
            "status" => "error",
            "message" => "Invalid Date Format (Use 12-hour AM/PM)"
        ]);
        exit();
    }

    // =========================
    // SAVE TO SCHEDULE (PENDING)
    // =========================

    $stmt = $conn->prepare("
        INSERT INTO match_schedule (
            match_title,
            total_match,
            start_time,
            interval_minute,
            match_time,
            total_prize,
            per_kill,
            entry_fee,
            entry_type,
            match_type,
            version,
            play_map,
            registration,
            total_player,
            prize_details,
            timer,
            join_type,
            status,
            created_count
        )
        VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,'pending',0)
    ");

    $stmt->bind_param(
        "sisssssssssssssss",
        $match_title,
        $total_match,
        $start_datetime,
        $interval_minute,
        $match_time,
        $total_prize,
        $per_kill,
        $entry_fee,
        $entry_type,
        $match_type,
        $version,
        $play_map,
        $registration,
        $total_player,
        $prize_details,
        $timer,
        $join_type
    );

    $stmt->execute();
    $stmt->close();

    // =========================
    // SUCCESS RESPONSE
    // =========================
    echo json_encode([
        "status" => "success",
        "message" => "Auto Match Requested Successfully"
    ]);
}

$conn->close();
?>