<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

session_start();

require_once __DIR__ . '/db_connect.php';

$message = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $email = trim($_POST['email'] ?? '');
    $password = $_POST['password'] ?? '';

    if ($email !== '' && $password !== '') {

        // Standalone RBE authentication
        $stmt = $conn_rbe->prepare("
            SELECT
                id,
                password_hash,
                is_active,
                is_deleted
            FROM rbe_users
            WHERE email = ?
              AND is_deleted = 0
              AND is_active = 1
            LIMIT 1
        ");

        $stmt->execute([$email]);
        $user = $stmt->fetch(PDO::FETCH_ASSOC);

        if (
            $user &&
            !empty($user['password_hash']) &&
            password_verify($password, $user['password_hash'])
        ) {

            // Get RBE role
            $stmt = $conn_rbe->prepare("
                SELECT role
                FROM rbe_roles
                WHERE user_id = ?
                LIMIT 1
            ");

            $stmt->execute([$user['id']]);
            $role = $stmt->fetchColumn();

            if ($role) {

                $_SESSION['user_id'] = (int)$user['id'];
                $_SESSION['role'] = $role;

                switch ($role) {

                    case 'admin':
                        header('Location: admin-dashboard.php');
                        exit;

                    case 'landlord':
                        header('Location: landlord-dashboard.php');
                        exit;

                    case 'tenant':
                        header('Location: tenant-dashboard.php');
                        exit;

                    case 'agent':
                        header('Location: agents/dashboard.php');
                        exit;

                    default:
                        $message = 'Unknown role assigned. Please contact support.';
                        break;
                }

            } else {

                $message = 'Your account has no RBE role assigned. Please contact support or register as an Agent.';
            }

        } else {

            $message = 'Invalid email or password.';
        }

    } else {

        $message = 'Please enter both email and password.';
    }
}

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

<head>
    <meta charset="UTF-8">
    <title>RBE Utilities Login</title>

    <meta
        name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0"
    >

    <style>

        body {
            font-family: Arial, sans-serif;
            background: #051a25;
            color: #ffffff;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
        }

        .container {
            width: 90%;
            max-width: 400px;
            background: #122c4c;
            padding: 20px;
            border-radius: 12px;
            box-shadow: 0 0 15px rgba(0,0,0,0.4);
            text-align: center;
        }

        img.logo {
            width: 200px;
            max-width: 100%;
            height: auto;
            margin-bottom: 20px;
        }

        h2 {
            margin-top: 0;
        }

        form {
            display: flex;
            flex-direction: column;
            align-items: center;
        }

        input[type="text"],
        input[type="password"] {
            width: 100%;
            padding: 12px;
            margin: 10px 0;
            border-radius: 8px;
            border: none;
            box-sizing: border-box;
        }

        button {
            background: #7ed957;
            color: #000;
            padding: 12px;
            margin-top: 10px;
            border: none;
            border-radius: 8px;
            font-weight: bold;
            cursor: pointer;
            width: 100%;
        }

        button:hover {
            background: #00cc77;
        }

        .message {
            color: #ff6666;
            margin-top: 10px;
        }

        .register-link {
            margin-top: 15px;
        }

        .register-link a {
            display: inline-block;
            background: #0f2536;
            color: #7ed957;
            padding: 10px 15px;
            border-radius: 8px;
            text-decoration: none;
            font-weight: bold;
        }

        .register-link a:hover {
            background: #0b1c28;
        }

        .forgot-password {
            margin-top: 15px;
        }

        .forgot-password a {
            color: #7ed957;
        }

        nav {
            width: 100%;
            background: #061423;
            box-shadow: 0 4px 10px rgba(0,0,0,0.3);
            padding: 15px 0;
            display: flex;
            justify-content: center;
            align-items: center;
            position: absolute;
            top: 0;
            left: 0;
            z-index: 10;
        }

        nav div {
            display: flex;
            gap: 30px;
        }

        nav a {
            color: #7ed957;
            font-weight: bold;
            text-decoration: none;
            font-size: 16px;
        }

    </style>
</head>

<body>

<nav>
    <div>
        <a href="https://rbelectrical.enterprises/">
            Home
        </a>

        <a href="https://rbelectrical.enterprises/#about">
            About
        </a>

        <a href="https://rbelectrical.enterprises/#contact">
            Contact
        </a>
    </div>
</nav>

<div class="container">

    <img
        src="https://rbelectrical.enterprises/wp-content/uploads/2024/07/RBE-Electrical-Logo-clear2-250x125.png"
        alt="RBE Logo"
        class="logo"
    >

    <h2>Login to Your RBE Account</h2>

    <form method="post">

        <input
            type="text"
            name="email"
            placeholder="Email"
            autocomplete="username"
            required
        >

        <input
            type="password"
            name="password"
            placeholder="Password"
            autocomplete="current-password"
            required
        >

        <button type="submit">
            Login
        </button>

        <p class="forgot-password">
            <a href="forgot-password.php">
                Forgot Password?
            </a>
        </p>

    </form>

    <?php if ($message !== ''): ?>

        <div class="message">
            <?php echo htmlspecialchars($message, ENT_QUOTES, 'UTF-8'); ?>
        </div>

    <?php endif; ?>

    <div class="register-link">
        <a href="agent-register.php">
            Register as Agent
        </a>
    </div>

</div>

</body>
</html>