<?php
// register.php
require 'config/db.php';
require_once 'addons/security_addon.php'; // Cargamos el addon para enviar mails

$error = "";

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = trim($_POST['username']);
    $email = trim($_POST['email']);
    $password = $_POST['password'];

    if (!empty($username) && !empty($email) && !empty($password)) {
        // Encriptar contraseña
        $hashed_password = password_hash($password, PASSWORD_DEFAULT);
        
        // Generar código de verificación de 6 dígitos
        $verification_code = rand(100000, 999999);

        try {
            // Insertamos al usuario con is_verified = 0 y el código generado
            $sql = "INSERT INTO users (username, email, password, verification_code, is_verified) VALUES (?, ?, ?, ?, 0)";
            $stmt = $pdo->prepare($sql);
            
            if ($stmt->execute([$username, $email, $hashed_password, $verification_code])) {
                
                // ENVIAR EL EMAIL DE BIENVENIDA
                sendVerificationEmail($email, $verification_code);

                // Iniciamos sesión automáticamente para mandarlo al Dashboard
                session_start();
                $_SESSION['user_id'] = $username;
                
                header("Location: dashboard.php?registered=true");
                exit;
            }
        } catch (PDOException $e) {
            $error = "Username or Email already exists.";
        }
    } else {
        $error = "Please fill all fields.";
    }
}

// Definimos constantes por si no están cargadas en el config
$primary_color = "#00ff88"; 
$site_name = "VVAULT";
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Join the Vault | Register</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        body { background: #090b0f; color: white; font-family: 'Inter', sans-serif; display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; }
        .auth-card { background: #12151c; padding: 40px; border-radius: 15px; border: 1px solid #2d333b; width: 100%; max-width: 400px; box-shadow: 0 20px 50px rgba(0,0,0,0.5); }
        
        .logo { font-size: 32px; font-weight: 900; text-align: center; margin-bottom: 30px; display: block; text-decoration: none; color: white; letter-spacing: -1px; }
        .logo span { color: <?php echo $primary_color; ?>; }
        
        .field-group { margin-bottom: 15px; position: relative; }
        .field-group i { position: absolute; left: 15px; top: 40px; color: #444; }
        
        label { font-size: 11px; color: #8b949e; text-transform: uppercase; font-weight: bold; margin-bottom: 8px; display: block; }
        
        input { width: 100%; padding: 12px 15px; background: #0d1117; border: 1px solid #2d333b; border-radius: 8px; color: white; box-sizing: border-box; font-size: 14px; transition: 0.3s; }
        input:focus { border-color: <?php echo $primary_color; ?>; outline: none; background: #161b22; }
        
        button { width: 100%; padding: 15px; background: <?php echo $primary_color; ?>; border: none; border-radius: 8px; font-weight: 900; cursor: pointer; margin-top: 15px; color: black; font-size: 14px; text-transform: uppercase; transition: 0.2s; }
        button:hover { transform: translateY(-2px); box-shadow: 0 5px 15px rgba(0,255,136,0.3); }
        
        .error { background: rgba(255, 77, 77, 0.1); border: 1px solid #ff4d4d; color: #ff4d4d; padding: 10px; border-radius: 8px; font-size: 13px; text-align: center; margin-bottom: 20px; }
    </style>
</head>
<body>
    <div class="auth-card">
        <a href="index.php" class="logo">V<span>VAULT</span></a>
        
        <?php if($error): ?> 
            <div class="error"><i class="fa-solid fa-circle-exclamation"></i> <?php echo $error; ?></div> 
        <?php endif; ?>

        <form method="POST">
            <div class="field-group">
                <label>Username</label>
                <input type="text" name="username" placeholder="e.g. Satoshi" required>
            </div>
            
            <div class="field-group">
                <label>Email Address</label>
                <input type="email" name="email" placeholder="name@domain.com" required>
            </div>
            
            <div class="field-group">
                <label>Password</label>
                <input type="password" name="password" placeholder="••••••••" required>
            </div>

            <button type="submit">START YOUR JOURNEY</button>
        </form>

        <p style="text-align: center; font-size: 13px; color: #8b949e; margin-top: 25px;">
            Already in the vault? <a href="login.php" style="color: <?php echo $primary_color; ?>; text-decoration: none; font-weight: bold;">Sign In</a>
        </p>
    </div>
</body>
</html>