<?php
require __DIR__ . '/includes/functions.php';
$currentPage = 'account';
$pageTitle = 'Create Customer Account | Furnify';
$errors = array();
$notice = '';

if (customer_current()) {
    header('Location: customer-dashboard.php');
    exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!verify_csrf_token((string) (isset($_POST['csrf_token']) ? $_POST['csrf_token'] : ''))) {
        $errors[] = 'Security token expired. Please try again.';
    }
    $password = (string) (isset($_POST['password']) ? $_POST['password'] : '');
    $confirm = (string) (isset($_POST['password_confirm']) ? $_POST['password_confirm'] : '');
    if ($password !== $confirm) {
        $errors[] = 'Password confirmation does not match.';
    }
    if (!$errors) {
        $result = customer_register_account(array(
            'full_name' => isset($_POST['full_name']) ? $_POST['full_name'] : '',
            'phone' => isset($_POST['phone']) ? $_POST['phone'] : '',
            'email' => isset($_POST['email']) ? $_POST['email'] : '',
            'city' => isset($_POST['city']) ? $_POST['city'] : '',
            'address' => isset($_POST['address']) ? $_POST['address'] : '',
            'password' => $password,
        ));
        if ($result['ok']) {
            $customer = $result['customer'];
            start_secure_session();
            $_SESSION['customer_id'] = (int) $customer['id'];
            if (customer_show_debug_verification_link() && $result['token'] !== '') {
                $_SESSION['last_verification_link'] = customer_verification_url($customer['id'], $result['token']);
            }
            header('Location: customer-dashboard.php?registered=1');
            exit;
        }
        $errors[] = $result['error'];
    }
}

require __DIR__ . '/includes/header.php';
?>
<section class="page-section">
    <div class="container narrow">
        <div class="premium-panel p-4 p-lg-5">
            <p class="eyebrow">Customer account</p>
            <h1>Create your Furnify account</h1>
            <p class="lead">Track orders, Re-Up requests, custom sofa quotes, payment status, and delivery updates in one dashboard.</p>
            <?php if (!customer_accounts_available()): ?>
                <div class="alert alert-warning">Customer accounts require MySQL. Import the updated schema/migration and enable <code>data/database.php</code>.</div>
            <?php endif; ?>
            <?php foreach ($errors as $error): ?><div class="alert alert-danger"><?= e($error) ?></div><?php endforeach; ?>
            <form method="post" class="row g-3">
                <input type="hidden" name="csrf_token" value="<?= e(csrf_token()) ?>">
                <div class="col-md-6"><label class="form-label">Full name *</label><input class="form-control" name="full_name" required maxlength="140" autocomplete="name" value="<?= e(isset($_POST['full_name']) ? $_POST['full_name'] : '') ?>"></div>
                <div class="col-md-6"><label class="form-label">Mobile number *</label><input class="form-control" name="phone" required maxlength="40" autocomplete="tel" value="<?= e(isset($_POST['phone']) ? $_POST['phone'] : '') ?>"></div>
                <div class="col-md-6"><label class="form-label">Email *</label><input class="form-control" type="email" name="email" required maxlength="180" autocomplete="email" value="<?= e(isset($_POST['email']) ? $_POST['email'] : '') ?>"></div>
                <div class="col-md-6"><label class="form-label">City / Municipality</label><input class="form-control" name="city" maxlength="140" autocomplete="address-level2" value="<?= e(isset($_POST['city']) ? $_POST['city'] : '') ?>"></div>
                <div class="col-12"><label class="form-label">Address / landmark</label><input class="form-control" name="address" maxlength="255" autocomplete="street-address" value="<?= e(isset($_POST['address']) ? $_POST['address'] : '') ?>"></div>
                <div class="col-md-6"><label class="form-label">Password *</label><input class="form-control" type="password" name="password" required minlength="8" autocomplete="new-password"><div class="form-text">At least 8 characters.</div></div>
                <div class="col-md-6"><label class="form-label">Confirm password *</label><input class="form-control" type="password" name="password_confirm" required minlength="8" autocomplete="new-password"></div>
                <div class="col-12"><button class="btn btn-primary btn-lg w-100" type="submit" <?= customer_accounts_available() ? '' : 'disabled' ?>>Create Account</button></div>
            </form>
            <p class="mt-4 mb-0 text-center">Already registered? <a href="login.php">Login here</a>.</p>
        </div>
    </div>
</section>
<?php require __DIR__ . '/includes/footer.php'; ?>
