<?php
// /public_html/login.php

// 1. ENABLE ERROR REPORTING (Critical for debugging)
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

session_start();

// 2. CHECK DATABASE CONNECTION
if (file_exists('config/db_connect.php')) {
    require 'config/db_connect.php';
} elseif (file_exists('db_connect.php')) {
    require 'db_connect.php';
} else {
    die("<h1 style='color:red'>Critical Error: Database file not found.</h1>");
}

// 3. HANDLE LOGIN
$error = "";
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['login_btn'])) {
    $email = $conn->real_escape_string($_POST['email']);
    $password = $_POST['password'];

    // DEBUG: Print what we received (Remove this after fixing)
    // echo "Trying to login with Email: $email <br>";

    $sql = "SELECT id, name, password, role FROM users WHERE email = '$email'";
    $result = $conn->query($sql);

    if (!$result) {
        $error = "Database Query Failed: " . $conn->error;
    } elseif ($result->num_rows > 0) {
        $row = $result->fetch_assoc();
        
        // DEBUG: Verify Hash
        // echo "Found User. ID: " . $row['id'] . "<br>";
        
        if (password_verify($password, $row['password'])) {
            // SUCCESS
            $_SESSION['user_id'] = $row['id'];
            $_SESSION['user_name'] = $row['name'];
            $_SESSION['role'] = $row['role'];
            header("Location: dashboard/index.php"); 
            exit();
        } else {
            // FAIL: Wrong Password
            $error = "Incorrect password. (Hash check failed)";
        }
    } else {
        // FAIL: User not found
        $error = "No account found with this email.";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login | DigitalRaj</title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">

    <style>
        :root { --bg-dark: #0f172a; --card-bg: rgba(30, 41, 59, 0.7); --primary: #3b82f6; }
        body { font-family: 'Inter', sans-serif; background: var(--bg-dark); min-height: 100vh; display: flex; align-items: center; justify-content: center; }
        .login-card { width: 100%; max-width: 400px; background: var(--card-bg); backdrop-filter: blur(20px); border: 1px solid rgba(255,255,255,0.1); border-radius: 24px; padding: 40px; color: white; }
        .form-control { background: rgba(15, 23, 42, 0.6); border: 1px solid rgba(255, 255, 255, 0.1); color: white; }
        .form-control:focus { background: rgba(15, 23, 42, 0.9); border-color: var(--primary); color: white; box-shadow: none; }
        .btn-glow { width: 100%; background: var(--primary); border: none; padding: 12px; border-radius: 12px; color: white; font-weight: 600; margin-top: 20px; }
        .brand-icon { width: 50px; height: 50px; background: linear-gradient(135deg, var(--primary), #60a5fa); border-radius: 14px; margin: 0 auto 20px; display: flex; align-items: center; justify-content: center; font-size: 24px; }
    </style>
</head>
<body>

    <div class="login-card">
        <div class="brand-icon"><i class="bi bi-qr-code-scan"></i></div>
        <h2 class="text-center fw-bold mb-1">Welcome Back</h2>
        <p class="text-center text-secondary mb-4 small">Login to your dashboard</p>

        <?php if($error): ?>
            <div class="alert alert-danger py-2 small text-center"><?php echo $error; ?></div>
        <?php endif; ?>

        <form method="POST">
            <div class="mb-3">
                <label class="form-label small text-secondary">Email Address</label>
                <input type="email" name="email" class="form-control" required>
            </div>
            <div class="mb-3">
                <label class="form-label small text-secondary">Password</label>
                <input type="password" name="password" class="form-control" required>
            </div>
            
            <div class="text-end mb-3">
                <a href="forgot_password.php" class="text-decoration-none small text-secondary">Forgot password?</a>
            </div>

            <button type="submit" name="login_btn" class="btn btn-glow">Sign In</button>
        </form>

        <div class="text-center mt-4 small">
            Don't have an account? <a href="register.php" class="text-primary fw-bold text-decoration-none">Sign Up Free</a>
        </div>
    </div>

</body>
</html>