<?php

include_once __DIR__ . '/secrets.php';

include 'db_connect.php';

$email         = $_POST['email'] ?? '';
$password           = $_POST['password'] ?? '';
$key           = $_POST['key'] ?? '';

$security_key = decrypData($key);

if ($security_key === false) {
    echo 'Invalid encryption data';
    exit;
}

if ($security_key == SECURITY_KEY_PLAIN && strlen( $email ) > 0 ) {
    
    $con = dbConnect();

    // Step 1: ফোন এবং পিন মিলছে কিনা চেক
    $stmt = $con->prepare("SELECT userid FROM signup WHERE email = ? AND password = ?");
    $stmt->bind_param("ss", $email, $password);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($row = $result->fetch_assoc()) {
        echo json_encode([
                "status" => "VALID LOGIN",
                "userid"     => $row['userid']
        ]);
            
    } else {
        // পিন বা ফোন ভুল
        echo json_encode([
            "status"  => "INVALID",
            "message" => "ফোন নাম্বার বা পিন ভুল!"
        ]);
    }

    $stmt->close();
    $con->close();
}


?>
