<?php
session_start();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $_SESSION['username'] = '';
    $_SESSION['database'] = '';
    $_SESSION['userid'] = '';
    $_SESSION['countryCode'] = '';

    $username = $_POST['username'] ?? '';
    $password = $_POST['password'] ?? '';

    $response = [
        "success" => false,
        "message" => ""
    ];

    if (empty(trim($username)) && empty(trim($password))) {
        $response['message'] = "Enter Credentials.";
        echo json_encode($response);
        exit;
    }

    if (empty(trim($username))) {
        $response['message'] = "Username cannot be empty.";
        echo json_encode($response);
        exit;
    }

    if (empty(trim($password))) {
        $response['message'] = "Password cannot be empty.";
        echo json_encode($response);
        exit;
    }

    try {
        require_once 'connection.php';

        $stmt = $pdo->prepare("SELECT UNAME, PASSWORD, DATABASENAME, USERID, PROFILE_PHOTO, COUNTRYCODE, ROLE 
                               FROM USERS WHERE USERNAME = :username");
        $stmt->bindParam(':username', $username);
        $stmt->execute();

        $row = $stmt->fetch(PDO::FETCH_ASSOC);

        if ($row) {
            if (password_verify($password, $row['PASSWORD'])) {
                $_SESSION['username'] = $username;
                $_SESSION['database'] = $row['DATABASENAME'];
                $_SESSION['userid'] = $row['USERID'];
                $_SESSION['uname'] = $row['UNAME'];
                $_SESSION['profile_photo'] = $row['PROFILE_PHOTO'];
                $_SESSION['countryCode'] = $row['COUNTRYCODE'];
                $_SESSION['role'] = $row['ROLE'];

                $response['success'] = true;
                $response['message'] = "Login Successful!";
            } else {
                $response['message'] = "Incorrect Password!";
            }
        } else {
            $response['message'] = "No user found.";
        }
    } catch (PDOException $e) {
        $response['message'] = "Database error: " . $e->getMessage();
    }

    echo json_encode($response);
    exit; // prevent HTML from loading
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login</title>
    <link rel="stylesheet" href="main.css">
    <link rel="shortcut icon" href="data/favicon.ico" type="image/x-icon">
    <link rel="stylesheet"
          href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css"
          crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>

<body class="loginhtml">
    <h2>SIGN IN/UP</h2>

    <div class="container" id="container">
        <!-- Sign-up form -->
        <div class="form-container sign-up-container">
            <form id="signUpForm">
                <h1>Create Account</h1>
                <div class="social-container">
                    <a href="#" class="social"><i class="fab fa-facebook-f"></i></a>
                    <a href="#" class="social"><i class="fab fa-google-plus-g"></i></a>
                    <a href="#" class="social"><i class="fab fa-linkedin-in"></i></a>
                </div>
                <span>or use your email for registration</span>
                <input required name="username" id="signUpUsername" type="text" placeholder="Username" />
                <input required name="password" id="signUpPass" type="password" placeholder="Password" />
                <input required id="confirmPass" type="password" placeholder="Confirm Password" />
                <p class="error" id="error"></p>
                <div class="show-password">
                    <input type="checkbox" onclick="showPass()">
                    <span>Show Password</span>
                </div>
                <br>
                <button type="button" id="signUpButton" onclick="signUp()">Sign Up</button>
            </form>
        </div>

        <!-- Sign-in form -->
        <div class="form-container sign-in-container">
            <form id="loginForm">
                <h1>Sign in</h1>
                <div class="social-container">
                    <a href="#" class="social"><i class="fab fa-facebook-f"></i></a>
                    <a href="#" class="social"><i class="fab fa-google-plus-g"></i></a>
                    <a href="#" class="social"><i class="fab fa-linkedin-in"></i></a>
                </div>
                <span>or use your account</span>
                <input required name="username" id="username" type="text" placeholder="Username" />
                <input required id="pass" name="password" type="password" placeholder="Password" />
                <p class="s_error" id="s_error"></p>
                <div class="show-password">
                    <input type="checkbox" onclick="showLoginPass()">
                    <span>Show Password</span>
                </div>
                <a href="forgot_password.php">Forgot your password?</a>
                <br>
                <button type="button" onclick="login()" id="loginButton">Sign In</button>
            </form>
        </div>

        <!-- Overlay -->
        <div class="overlay-container">
            <div class="overlay">
                <div class="overlay-panel overlay-left">
                    <h1>Welcome Back!</h1>
                    <p>To keep connected with us please login with your personal info</p>
                    <button class="ghost" id="signIn">Sign In</button>
                </div>
                <div class="overlay-panel overlay-right">
                    <h1>Hello, Friend!</h1>
                    <p>Enter your personal details and start journey with us</p>
                    <button class="ghost" id="signUp">Sign Up</button>
                </div>
            </div>
        </div>
    </div>

    <script src="login.js"></script>
</body>
</html>
