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

// Table auto-create if not exists
$create_table_sql = "
CREATE TABLE IF NOT EXISTS appinfo (
  id INT PRIMARY KEY,
  home_notice TEXT,
  version_code VARCHAR(20),
  update_notice TEXT,
  updated_on VARCHAR(30),
  update_link TEXT,
  application_status VARCHAR(10),
  closed_reason TEXT,
  autopay_status VARCHAR(10),
  bkash_number VARCHAR(20),
  nagad_number VARCHAR(20),
  rocket_number VARCHAR(20),
  bkash_notice TEXT,
  nagad_notice TEXT,
  rocket_notice TEXT,
  min_deposit VARCHAR(20),
  min_withdraw VARCHAR(20),
  min_refer_bonus VARCHAR(20),
  min_first_refer_dp VARCHAR(20),
  admin_support_link TEXT,
  add_money_link TEXT,
  collect_room_link TEXT,
  join_match_link TEXT,
  telegram_link TEXT,
  share_app_link TEXT,
  dialog_notice TEXT
)";
$conn->query($create_table_sql);

// Insert initial row if not exists
$check_sql = "SELECT id FROM appinfo WHERE id = 1";
$result = $conn->query($check_sql);
if ($result->num_rows == 0) {
    $conn->query("INSERT INTO appinfo (id) VALUES (1)");
}

// Define types
$types = [
    "basic" => ["version_code", "update_notice", "updated_on", "update_link"],
    "status" => ["application_status", "closed_reason", "autopay_status"],
    "number" => ["bkash_number", "nagad_number", "rocket_number"],
    "notice" => ["bkash_notice", "nagad_notice", "rocket_notice"],
    "limits" => ["min_deposit", "min_withdraw"],
    "limits_promo" => ["min_refer_bonus", "min_first_refer_dp"],
    "link" => ["admin_support_link", "add_money_link", "collect_room_link", "join_match_link", "telegram_link", "share_app_link"],
    "dialog_notice" => ["home_notice", "dialog_notice"]
];

$type = $_POST['type'] ?? null;

$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 || !isset($types[$type])) {
    echo json_encode(["status" => "error", "message" => "Invalid or missing 'type'."]);
    exit;
}

$fields = $types[$type];
$updates = [];

foreach ($fields as $field) {
    if (isset($_POST[$field])) {
        $val = $conn->real_escape_string($_POST[$field]);
        $updates[] = "$field = '$val'";
    }
}

if (count($updates) === 0) {
    echo json_encode(["status" => "error", "message" => "No valid fields provided."]);
    exit;
}

$sql = "UPDATE appinfo SET " . implode(", ", $updates) . " WHERE id = 1";
if ($conn->query($sql)) {
    echo json_encode(["status" => "success", "message" => ucfirst(str_replace("_", " ", $type)) . " updated successfully."]);
} else {
    echo json_encode(["status" => "error", "message" => "Update failed."]);
}
?>