<?php
// Check the client's IP address
$ipAddress = $_SERVER['REMOTE_ADDR'];
$chatId = $_GET['chatId'];

// Database connection variables
$servername = "localhost";
$username = "root"; // Change this to your database username
$password = ""; // Change this to your database password
$database = "dha"; // Change this to your database name

// Check if the client's IP address is localhost
if ($ipAddress == "127.0.0.1" || $ipAddress == "::1") {
    // Connect to the local database
    $conn = mysqli_connect($servername, $username, $password, $database);
} else {
    // Change to your production database credentials
    $servername = "localhost";
    $username = "dzvisual_admin";
    $password = "Farhan@123456";
    $database = "dzvisual_homestay";

    // Connect to the production database
    $conn = mysqli_connect($servername, $username, $password, $database);
}

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// SQL query to fetch data from the table
$sql = "SELECT * FROM chats WHERE tg_chat_id=$chatId";
$result = mysqli_query($conn, $sql);

// Initialize an array to store the fetched data
$data = array();

// Check if any rows were returned
if (mysqli_num_rows($result) > 0) {
    // Output data of each row
    while ($row = mysqli_fetch_assoc($result)) {
        // Add each row to the data array
        $data[] = $row;
    }
}

// Close connection
mysqli_close($conn);

// Output the data as JSON
header('Content-Type: application/json');
echo json_encode($data);
?>
