<?php
session_start();
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header('Access-Control-Allow-Headers: X-Requested-With');
header('Content-Type: application/json; charset=utf-8');

if (isset($_POST['user_id']) && isset($_POST['auth'])) {

    // Include the database configuration file
    include 'config.php';

    // Input sanitization
    $user_id = trim($_POST['user_id']);
    $auth = trim($_POST['auth']);

    // Use prepared statements to prevent SQL injection
    $stmt = $con->prepare("SELECT * FROM users WHERE user_id = ? AND auth = ?");
    $stmt->bind_param("ss", $user_id, $auth);

    if ($stmt->execute()) {
        $result = $stmt->get_result();

        if ($result->num_rows > 0) {
            $rows = $result->fetch_assoc();

            // Extract user data
            $user_id = $rows['user_id'];
            $phone = $rows['phone'];
            $email = $rows['email'];
           // $balance = $rows['balance'];
            $firstname = $rows['firstname'];
            $midname = $rows['midname'];
            $lastname = $rows['lastname'];
            $address = $rows['address'];
            $city = $rows['city'];
            $state = $rows['state'];
            $country = $rows['country'];
            $country_code = $rows['country_code'];
            $whatsapp = $rows['whatsapp'];
            $id_doc = $rows['id_doc'];
            $passport = $rows['passport'];
            $manager = $rows['manager'];
            $manager_phone = $rows['manager_phone'];
            $manager_webmail = $rows['manager_webmail'];
            $stat = $rows['status'];
            $account_type = $rows['account_type']; // Ensure this field exists in your database



            $ref_code = $rows['ref_code'];
            $reffered_by	 = $rows['reffered_by'];
            $admission_commision = $rows['admission_commision'];
            $visa_commision = $rows['visa_commision']; 
            
            $sms_not = $rows['sms_not'];
            $email_not	 = $rows['email_not'];
            $whatsapp_not = $rows['whatsapp_not'];
            
            $sms_not2 = $rows['sms_not2']; 
            $email_not2 = $rows['email_not2']; 
            $whatsapp_not2 = $rows['whatsapp_not2']; 

            
            
            // Prefix user ID based on account type
        /*    if ($account_type === "Student") {
                $user_id = "SN" . $user_id;
            } elseif ($account_type === "Parent") {
                $user_id = "PR" . $user_id;
            } elseif ($account_type === "Agent") {
                $user_id = "AG" . $user_id;
            } */

            // Success response
            $output = [
                'success' => true,
                'user_id' => $user_id,
                'phone' => $phone,
                'email' => $email,
                'firstname' => $firstname,
                'lastname' => $lastname,
                'midname' => $midname,
                'address' => $address,
                'city' => $city,
                'state' => $state,
                'country_code' => $country_code,
                'country' => $country,
                'whatsapp' => $whatsapp,
                'id_doc' => $id_doc,
                'passport' => $passport,
                'manager' => $manager,
                'manager_phone' => $manager_phone, 
                'manager_email' => $manager_webmail, 
                'status' => $stat, 
                'account_type' => $account_type,  
                
                'ref_code' => $ref_code,
                'reffered_by' => $reffered_by,
                'admission_commision' => $admission_commision, 
                'visa_commision' => $visa_commision, 
               
                'sms_not' => $sms_not, 
                'email_not' => $email_not,  
                'whatsapp_not' => $whatsapp_not,  
                'sms_not2' => $sms_not2,  
                'email_not2' => $email_not2,  
                'whatsapp_not2' => $whatsapp_not2
                
            ];
            echo json_encode($output);
        } else {
            // User not found
            echo json_encode(['success' => false, 'message' => 'User not found']);
        }
    } else {
        // Query execution error
        error_log('Database error: ' . $stmt->error);
        echo json_encode(['success' => false, 'message' => 'Internal server error']);
    }

    // Close the statement and connection
    $stmt->close();
    $con->close();
} else {
    // Invalid request
    echo json_encode(['success' => false, 'message' => 'Invalid request']);
}
?>
