<?php
require_once 'includes/header.php';

$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = $_POST['username'] ?? '';
    $password = $_POST['password'] ?? '';
    $confirm_password = $_POST['confirm_password'] ?? '';
    $agree = $_POST['agree'] ?? '';

    if (empty($username) || empty($password)) {
        $error = 'Please fill in all fields.';
    } elseif ($password !== $confirm_password) {
        $error = 'Passwords do not match.';
    } elseif (!$agree) {
        $error = 'You must agree to the terms and privacy policy.';
    } else {
        try {
            $pdo = db();
            // Check if user exists
            $stmt = $pdo->prepare("SELECT id FROM users WHERE username = ?");
            $stmt->execute([$username]);
            if ($stmt->fetch()) {
                $error = 'Username already exists.';
            } else {
                // Generate unique 6-digit UID
                do {
                    $uid = rand(100000, 999999);
                    $stmt = $pdo->prepare("SELECT id FROM users WHERE uid = ?");
                    $stmt->execute([$uid]);
                } while ($stmt->fetch());

                $hash = password_hash($password, PASSWORD_DEFAULT);
                $stmt = $pdo->prepare("INSERT INTO users (username, uid, email, password_hash, balance_usdt) VALUES (?, ?, ?, ?, ?)");
                $stmt->execute([$username, $uid, $username . '@example.com', $hash, 10000.00]); // Give $10k demo balance
                
                $userId = $pdo->lastInsertId();
                $_SESSION['user_id'] = $userId;
                header('Location: index.php');
                exit;
            }
        } catch (Exception $e) {
            $error = 'Registration failed: ' . $e->getMessage();
        }
    }
}
?>

<div class="container my-5 py-5">
    <div class="row justify-content-center">
        <div class="col-md-5">
            <div class="card bg-dark border-secondary p-4 shadow-lg" style="border-radius: 20px;">
                <h2 class="text-center mb-4 fw-bold">Sign Up</h2>
                <p class="text-center text-muted mb-4">Join the world's leading crypto exchange</p>
                <?php if ($error): ?>
                    <div class="alert alert-danger"><?php echo $error; ?></div>
                <?php endif; ?>
                <form method="POST">
                    <div class="mb-3">
                        <label class="form-label">Username / Email</label>
                        <input type="text" name="username" class="form-control bg-dark text-white border-secondary py-2" required placeholder="Enter username">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Password</label>
                        <input type="password" name="password" class="form-control bg-dark text-white border-secondary py-2" required placeholder="Enter password">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Confirm Password</label>
                        <input type="password" name="confirm_password" class="form-control bg-dark text-white border-secondary py-2" required placeholder="Repeat password">
                    </div>
                    <div class="mb-3 form-check">
                        <input type="checkbox" name="agree" class="form-check-input" id="agree" required>
                        <label class="form-check-label small text-muted" for="agree">
                            I have read and agree to the <a href="#" class="text-accent">Terms of Service</a> and <a href="#" class="text-accent">Privacy Policy</a>.
                        </label>
                    </div>
                    <button type="submit" class="btn btn-primary w-100 py-3 fw-bold" style="border-radius: 12px; background-color: var(--okx-blue); border: none;">Create Account</button>
                    <div class="text-center mt-4">
                        <span class="text-muted">Already have an account?</span> <a href="login.php" class="text-accent text-decoration-none fw-bold">Login</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<style>
    .text-accent { color: var(--okx-blue); }
    .form-control:focus {
        background-color: #2b2f36;
        border-color: var(--okx-blue);
        box-shadow: none;
        color: #fff;
    }
</style>

<?php require_once 'includes/footer.php'; ?>