<?php
require_once __DIR__ . '/../../config/constants.php';
require_once __DIR__ . '/../../config/database.php';
require_once __DIR__ . '/../../config/email.php';
require_once __DIR__ . '/../../app/Database.php';
require_once __DIR__ . '/../../app/Mail.php';
require_once __DIR__ . '/../../app/Auth.php';
require_once __DIR__ . '/../../app/Subscription.php';
require_once __DIR__ . '/../../app/Helper.php';

$database = new Database($pdo);
$auth = new Auth($database);
$subscription = new Subscription($database);

$error = '';
$success = '';
$pageTitle = 'Daftar - KasirMurah.biz.id';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = $_POST['email'] ?? '';
    $password = $_POST['password'] ?? '';
    $password_confirm = $_POST['password_confirm'] ?? '';
    $name = $_POST['name'] ?? '';
    $company_name = $_POST['company_name'] ?? '';

    if (!Helper::validateEmail($email)) {
        $error = 'Format email tidak valid';
    } elseif (strlen($password) < 6) {
        $error = 'Password minimal 6 karakter';
    } elseif ($password !== $password_confirm) {
        $error = 'Password tidak cocok';
    } elseif (empty($name) || empty($company_name)) {
        $error = 'Semua field harus diisi';
    } else {
        $result = $auth->register($email, $password, $name, $company_name);

        if ($result['success']) {
            // Send welcome email
            $subscription->sendWelcomeEmail($email, $name);

            $success = 'Registrasi berhasil! <a href="login.php" style="font-weight:600">Login di sini</a>';
        } else {
            $error = $result['message'];
        }
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $pageTitle; ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <link href="../assets/css/style.css" rel="stylesheet">
</head>
<body class="auth-wrapper">
    <div class="auth-card" style="max-width:480px">
        <div class="auth-logo">
            <i class="fas fa-cash-register"></i> KasirMurah.biz.id
        </div>
        <div class="auth-subtitle">Buat akun baru — gratis 1 hari trial</div>

        <div class="alert alert-info mb-3" style="font-size:13px">
            <i class="fas fa-crown me-2"></i>
            <strong>Trial 1 hari gratis</strong> · Lanjut Rp 50.000/bulan
        </div>

        <?php if ($error): ?>
        <div class="alert alert-danger mb-3">
            <i class="fas fa-exclamation-circle me-2"></i><?php echo htmlspecialchars($error); ?>
        </div>
        <?php endif; ?>

        <?php if ($success): ?>
        <div class="alert alert-success mb-3">
            <i class="fas fa-check-circle me-2"></i><?php echo $success; ?>
        </div>
        <?php endif; ?>

        <form method="POST">
            <div class="row g-3">
                <div class="col-12">
                    <label class="form-label">Nama Lengkap</label>
                    <input type="text" class="form-control" name="name" placeholder="John Doe" required
                           value="<?php echo htmlspecialchars($_POST['name'] ?? ''); ?>">
                </div>
                <div class="col-12">
                    <label class="form-label">Nama Toko / Perusahaan</label>
                    <input type="text" class="form-control" name="company_name" placeholder="Toko ABC" required
                           value="<?php echo htmlspecialchars($_POST['company_name'] ?? ''); ?>">
                </div>
                <div class="col-12">
                    <label class="form-label">Email</label>
                    <input type="email" class="form-control" name="email" placeholder="nama@email.com" required
                           value="<?php echo htmlspecialchars($_POST['email'] ?? ''); ?>">
                </div>
                <div class="col-sm-6">
                    <label class="form-label">Password</label>
                    <input type="password" class="form-control" name="password" placeholder="Min. 6 karakter" required>
                </div>
                <div class="col-sm-6">
                    <label class="form-label">Konfirmasi Password</label>
                    <input type="password" class="form-control" name="password_confirm" placeholder="Ulangi password" required>
                </div>
                <div class="col-12 mt-2">
                    <button type="submit" class="btn btn-primary w-100 btn-lg">
                        <i class="fas fa-user-plus me-1"></i> Daftar Sekarang
                    </button>
                </div>
            </div>
        </form>

        <div class="text-center mt-4" style="font-size:14px;color:var(--gray)">
            Sudah punya akun? <a href="login.php" style="font-weight:600">Login di sini</a>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
