<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
require_once __DIR__ . '/../config/app.php';
require_once __DIR__ . '/../config/database.php';

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

$error = "";

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

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

    $stmt = $pdo->prepare("
    SELECT users.*, roles.name AS role_name
    FROM users
    JOIN roles ON users.role_id = roles.id
    WHERE users.email = ? AND users.status = 'ACTIVE'
");

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

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

    $_SESSION['user_id']   = $user['id'];
    $_SESSION['role_id']   = $user['role_id'];
    $_SESSION['full_name'] = $user['full_name'];
    $_SESSION['role_name'] = $user['role_name'];  // FIXED
    $_SESSION['is_super_admin'] = ($user['role_name'] === 'SUPER_ADMIN');
    $_SESSION['company_id'] = $user['company_id'] ?? null;

    // Log login
    $log = $pdo->prepare("INSERT INTO audit_logs (user_id, action, ip_address) VALUES (?, ?, ?)");
    $log->execute([$user['id'], 'LOGIN_SUCCESS', $_SERVER['REMOTE_ADDR']]);
    

if ((int)$user['must_change_password'] === 1) {
    header("Location: /core/public/force_password_change.php");
    exit;
}

// Role-based landing (safe)
if (($user['role_name'] ?? '') === 'INSTALLER') {
    header("Location: /core/public/installer/network/dashboard.php");
    exit;
}

header("Location: index.php");
exit;

}else {
        $error = "Invalid login credentials.";
    }
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Login - <?php echo SYSTEM_NAME; ?></title>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap" rel="stylesheet">
    <style>
        body {
            font-family: 'Poppins', sans-serif;
            background: #f4f6f9;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }
        .login-box {
            background: white;
            padding: 40px;
            border-radius: 12px;
            box-shadow: 0 5px 20px rgba(0,0,0,0.08);
            width: 350px;
        }
        h2 {
            text-align: center;
            color: #f2531e;
            margin-bottom: 25px;
        }
        input {
            width: 100%;
            padding: 12px;
            margin-bottom: 15px;
            border: 1px solid #ddd;
            border-radius: 8px;
        }
        button {
            width: 100%;
            padding: 12px;
            background: #f2531e;
            border: none;
            color: white;
            border-radius: 8px;
            cursor: pointer;
        }
        button:hover {
            background: #d84315;
        }
        .error {
            color: red;
            text-align: center;
            margin-bottom: 10px;
        }
    </style>
</head>
<body>

<div class="login-box">
    <h2><?php echo SYSTEM_NAME; ?> Login</h2>

    <?php if ($error): ?>
        <div class="error"><?php echo $error; ?></div>
    <?php endif; ?>

    <form method="POST">
        <input type="email" name="email" placeholder="Email Address" required>
        <input type="password" name="password" placeholder="Password" required>
        <button type="submit">Login</button>
    </form>
</div>

</body>
</html>
