<?php

ini_set('display_errors', 1);
error_reporting(E_ALL);

session_start();

require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../services/AuthService.php';

$pdo = db();
$message = '';

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

    $name = trim($_POST['name']);
    $email = trim($_POST['email']);
    $password = $_POST['password'];

    if (AuthService::register($pdo, $name, $email, $password)) {
        $message = "Registration successful. You can now login.";
    } else {
        $message = "Registration failed.";
    }
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Register</title>
    <link rel="stylesheet" href="assets/style.css">
</head>
<body>

<div class="header">
    <h1>Trading Assistant</h1>
</div>

<div class="container">

    <div class="card" style="max-width:400px; margin:auto;">

        <h2>Create Account</h2>

        <?php if ($message): ?>
            <p style="color:green;"><?= htmlspecialchars($message) ?></p>
        <?php endif; ?>

        <form method="post">

            <label>Full Name</label><br>
            <input type="text" name="name" required><br><br>

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

            <label>Password</label><br>
            <input type="password" name="password" required><br><br>

            <button class="button">Register</button>

        </form>

        <hr style="margin:20px 0;">

        <p>
            Already have an account?
            <a href="login.php">Login here</a>
        </p>

    </div>

</div>

</body>
</html>