<?php
include '../database/db.php';
require_once '../PHPMailer/email_helper.php';

/* Kļūdas ziņojums */
$error = '';

/* Pārbaudām, vai forma ir iesniegta */
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    /* Iegūstam formas datus */
    $name         = $_POST['name'];
    $email        = $_POST['email'];
    $raw_password = $_POST['password'];
    $allow_email  = isset($_POST['allow_email_notifications']) ? 1 : 0;

    /* Paroles garuma pārbaude */
    if (strlen($raw_password) < 8) {
        $error = 'Parolei jābūt vismaz 8 rakstzīmes garai!';
    } else {

        /* Paroles šifrēšana */
        $password = password_hash($raw_password, PASSWORD_DEFAULT);

        /* Pārbaudām, vai e-pasts jau eksistē */
        $stmt = $pdo->prepare("SELECT id FROM users WHERE email = ?");
        $stmt->execute([$email]);
        $existing_user = $stmt->fetch();

        if ($existing_user) {
            $error = 'Šis e-pasts jau tiek izmantots!';
        } else {

            /* Jauna lietotāja izveide */
            $stmt = $pdo->prepare("
                INSERT INTO users (name, email, password_hash, allow_email_notifications)
                VALUES (?, ?, ?, ?)
            ");

            if ($stmt->execute([$name, $email, $password, $allow_email])) {

                /* Iegūstam jaunā lietotāja ID */
                $user_id = $pdo->lastInsertId();

                /* Izveidojam noklusējuma atgādinājumu iestatījumus */
                $pdo->prepare("
                    INSERT INTO user_notification_preferences
                    (user_id, reminder_1h, reminder_6h, reminder_12h, reminder_24h)
                    VALUES (?, 1, 0, 0, 1)
                ")->execute([$user_id]);

                /* Nosūtām sveiciena e-pastu, ja lietotājs atļāva paziņojumus */
                if ($allow_email) {
                    sendWelcomeEmail($email, $name);
                }

                /* Veiksmīga reģistrācija – novirzām uz login */
                header('Location: login.php?registered=1');
                exit();

            } else {
                $error = 'Reģistrācijas laikā radās kļūda!';
            }
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Register - Work Planner</title>
<link rel="stylesheet" href="../css/users.css">
</head>
<body>

<div class="form-container">
    <h2>Create Account</h2>

    <?php if (!empty($error)): ?>
        <p class="error"><?= htmlspecialchars($error) ?></p>
    <?php endif; ?>

    <form action="register.php" method="post">

        <label for="name">Name</label>
        <input type="text" name="name" id="name" required>

        <label for="email">Email</label>
        <input type="email" name="email" id="email" required>

        <label for="password">Password (min 8 characters)</label>
        <input type="password" name="password" id="password" required minlength="8">

        <div class="checkbox-container">
            <label class="checkbox-label">
                <input type="checkbox" name="allow_email_notifications" checked>
                <span>Allow email notifications for reminders</span>
            </label>
        </div>

        <button type="submit">Register</button>
    </form>

    <p class="form-footer">
        Already have an account?
        <a href="login.php">Login here</a>
    </p>
</div>

</body>
</html>