<?php
require_once 'db/config.php';

$companyName = "FinMox";
$employeeHandbookUrl = "#";
$error_message = null;

if (isset($_GET['token'])) {
    $token = $_GET['token'];
    $pdo = db();

    // Find user by token and check if it has expired
    $stmt = $pdo->prepare("SELECT * FROM users WHERE magic_token = ? AND magic_token_expires_at > NOW()");
    $stmt->execute([$token]);
    $user = $stmt->fetch();

    if ($user) {
        // Token is valid, log the user in
        session_start();
        $_SESSION['user_id'] = $user['id'];
        $_SESSION['role_id'] = $user['role_id']; // Store role for future use

        // Invalidate the token so it can't be reused
        $stmt = $pdo->prepare("UPDATE users SET magic_token = NULL, magic_token_expires_at = NULL WHERE id = ?");
        $stmt->execute([$user['id']]);

    } else {
        // Token is invalid or expired
        $error_message = "This link is invalid or has expired. Please request a new one from HR.";
    }
} else if (!isset($_SESSION['user_id'])){
    // Only show an error if no token and not already logged in.
    $error_message = "No authentication token provided. This page is only accessible via a special link.";
}

// If there's an error, display it and stop rendering the page.
if ($error_message) {
    echo "<p style='color: red;'>" . htmlspecialchars($error_message) . "</p>";
    exit;
}

// The magic link token would be validated here in a real scenario.
// For now, we just display the page.
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Welcome to <?php echo htmlspecialchars($companyName); ?></title>
    <script src="https://cdn.tailwindcss.com"></script>
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');
        * { font-family: 'Inter', sans-serif; }
        body {
            background-color: #f3f4f6; /* A light, neutral background */
            -webkit-font-smoothing: antialiased;
        }
    </style>
</head>
<body class="flex items-center justify-center min-h-screen">

    <div class="text-center max-w-2xl mx-auto p-8 bg-white rounded-xl shadow-lg">
        
        <!-- Company Logo -->
        <div class="mb-6">
            <img src="assets/pasted-20251120-051320-b2b0cdfa.png" alt="<?php echo htmlspecialchars($companyName); ?> Logo" class="mx-auto" style="height: 40px;">
        </div>

        <!-- Welcome Message -->
        <h1 class="text-4xl font-bold text-gray-900">Welcome Aboard!</h1>
        <p class="mt-3 text-lg text-gray-600">
            We are thrilled to have you join the <?php echo htmlspecialchars($companyName); ?> team. We've prepared a few things to get you started.
        </p>

        <!-- Onboarding Button -->
        <div class="mt-8">
            <a href="employee_view.php" class="inline-block bg-blue-600 hover:bg-blue-700 text-white font-bold text-lg py-3 px-10 rounded-lg transition-transform transform hover:scale-105">
                Start Your Onboarding
            </a>
        </div>

        <!-- Resource Links -->
        <div class="mt-10 text-sm">
            <p class="text-gray-500">Need to review some documents before you start?</p>
            <a href="<?php echo htmlspecialchars($employeeHandbookUrl); ?>" class="text-blue-600 hover:underline mt-1">
                Read the Employee Handbook
            </a>
        </div>

    </div>

</body>
</html>
