<?php
require_once 'includes/config.php';
require_once 'includes/auth.php';

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = clean($_POST['username'] ?? '');
    $password = $_POST['password'] ?? '';
    
    if (login($username, $password)) {
        if (isAdmin()) {
            header('Location: admin/index.php');
        } else {
            header('Location: dashboard.php');
        }
        exit();
    } else {
        $error = 'Nom d\'utilisateur ou mot de passe incorrect';
        logError('Tentative de connexion échouée', ['username' => $username]);
    }
}

// Générer token CSRF pour le formulaire
$csrf_token = generateCSRFToken();
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>afyakili checkup</title>
    <link rel="stylesheet" href="assets/css/style.css">
</head>
<body>
    <div class="floating-particles" id="particles"></div>
    
    <div class="container">
        <div class="login-container">
            <div class="logo">
                <img src="assets/img/logo.png" width="50%" height="50%" alt="Logo Afyakili">
                <p>checkup</p>
            </div>
            <p class="tagline">Votre compagnon du bien-être mental</p>
            
            <?php if ($error): ?>
                <div class="alert alert-error"><?php echo htmlspecialchars($error); ?></div>
            <?php endif; ?>
            
            <form method="POST" action="" class="login-form">
                <input type="hidden" name="csrf_token" value="<?php echo $csrf_token; ?>">
                <div class="form-group">
                    <input type="text" name="username" placeholder="Nom d'utilisateur ou email" required>
                </div>
                <div class="form-group">
                    <input type="password" name="password" placeholder="Mot de passe" required>
                </div>
                <button type="submit" class="btn-primary">Se connecter</button>
            </form>
            
            <div class="login-links">
                <a href="register.php">Créer un compte</a>
                <a href="reset_password.php">Mot de passe oublié ?</a>
            </div>
        </div>
    </div>
    
    <script>
        // Animation des particules flottantes
        function createParticles() {
            const particlesContainer = document.getElementById('particles');
            if (!particlesContainer) return;
            
            const particleCount = 30;
            
            for (let i = 0; i < particleCount; i++) {
                const particle = document.createElement('div');
                particle.className = 'particle';
                
                const size = Math.random() * 2 + 1;
                particle.style.width = `${size}px`;
                particle.style.height = `${size}px`;
                particle.style.left = `${Math.random() * 100}%`;
                particle.style.top = `${Math.random() * 100}%`;
                particle.style.opacity = Math.random() * 0.5 + 0.1;
                
                const duration = Math.random() * 30 + 20;
                const delay = Math.random() * 10;
                particle.style.animation = `float ${duration}s linear ${delay}s infinite`;
                
                particlesContainer.appendChild(particle);
            }
        }
        
        document.addEventListener('DOMContentLoaded', createParticles);
    </script>
    
    <style>
        .floating-particles {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            pointer-events: none;
            z-index: -1;
        }
        
        .particle {
            position: absolute;
            background: rgba(255, 255, 255, 0.5);
            border-radius: 50%;
            animation: float linear infinite;
        }
        
        @keyframes float {
            0% { transform: translateY(0) rotate(0deg); }
            100% { transform: translateY(-100vh) rotate(360deg); }
        }
    </style>
</body>
</html>