<?= $this->extend('layouts/main') ?>
<?= $this->section('content') ?>

<div class="container">
    <div class="card auth-card shadow-sm" style="max-width: 480px;">
        <div class="card-body p-4">
            <h4 class="mb-1 text-center">Verify Your Account</h4>
            <p class="text-muted text-center small mb-4">We've sent verification codes to your email and mobile number. Enter them below to activate your account.</p>

            <?php if (session()->getFlashdata('error')): ?>
                <div class="alert alert-danger py-2"><?= esc(session()->getFlashdata('error')) ?></div>
            <?php endif; ?>
            <?php if (session()->getFlashdata('success')): ?>
                <div class="alert alert-success py-2"><?= esc(session()->getFlashdata('success')) ?></div>
            <?php endif; ?>

            <?php $debugOtp = session()->getFlashdata('debug_otp'); ?>
            <?php if ($debugOtp): ?>
                <div class="alert alert-warning py-2 small">
                    <strong>Dev/Test mode only</strong> — SMS/email delivery may not be configured on this
                    installation, so codes are shown here for testing. This box will not appear in production.
                    <?php if (! empty($debugOtp['email'])): ?><div>Email code: <strong><?= esc($debugOtp['email']) ?></strong></div><?php endif; ?>
                    <?php if (! empty($debugOtp['mobile'])): ?><div>Mobile code: <strong><?= esc($debugOtp['mobile']) ?></strong></div><?php endif; ?>
                </div>
            <?php endif; ?>

            <!-- Email verification -->
            <div class="border rounded p-3 mb-3">
                <div class="d-flex justify-content-between align-items-center mb-2">
                    <strong>Email: <?= esc($email) ?></strong>
                    <?= $emailVerified ? '<span class="badge bg-success">Verified</span>' : '<span class="badge bg-secondary">Pending</span>' ?>
                </div>
                <?php if (! $emailVerified): ?>
                    <form method="post" action="<?= base_url('verify-account/otp') ?>" class="d-flex gap-2">
                        <?= csrf_field() ?>
                        <input type="hidden" name="channel" value="email">
                        <input type="text" name="code" class="form-control" placeholder="6-digit code" maxlength="6" required>
                        <button type="submit" class="btn btn-primary">Verify</button>
                    </form>
                    <form method="post" action="<?= base_url('verify-account/resend') ?>" class="mt-2">
                        <?= csrf_field() ?>
                        <input type="hidden" name="channel" value="email">
                        <button type="submit" class="btn btn-link btn-sm p-0">Resend code</button>
                    </form>
                <?php endif; ?>
            </div>

            <!-- Mobile verification -->
            <div class="border rounded p-3 mb-3">
                <div class="d-flex justify-content-between align-items-center mb-2">
                    <strong>Mobile: <?= esc($phone) ?></strong>
                    <?= $mobileVerified ? '<span class="badge bg-success">Verified</span>' : '<span class="badge bg-secondary">Pending</span>' ?>
                </div>
                <?php if (! $mobileVerified): ?>
                    <form method="post" action="<?= base_url('verify-account/otp') ?>" class="d-flex gap-2">
                        <?= csrf_field() ?>
                        <input type="hidden" name="channel" value="mobile">
                        <input type="text" name="code" class="form-control" placeholder="6-digit code" maxlength="6" required>
                        <button type="submit" class="btn btn-primary">Verify</button>
                    </form>
                    <form method="post" action="<?= base_url('verify-account/resend') ?>" class="mt-2">
                        <?= csrf_field() ?>
                        <input type="hidden" name="channel" value="mobile">
                        <button type="submit" class="btn btn-link btn-sm p-0">Resend code</button>
                    </form>
                <?php endif; ?>
            </div>

            <p class="text-center text-muted small mb-0">
                Didn't get a code? Check your spam folder, or if SMS delivery isn't configured on this
                installation yet, an administrator can find the code in the server logs.
            </p>
        </div>
    </div>
</div>
<?= $this->endSection() ?>
