<?php
/** @var \BotGuard\Core\View $this @var \BotGuard\Core\Csrf $csrf @var array $smtp @var array $encryptions @var bool $configured */
$this->layout('layouts.app');
?>
<h1 class="h4 mb-1">SMTP &amp; Email</h1>
<p class="text-secondary small mb-4">
    Configure outgoing mail for transactional emails (welcome messages, alerts).
    Status:
    <?php if ($configured): ?>
        <span class="badge text-bg-success">Configured</span>
    <?php else: ?>
        <span class="badge text-bg-secondary">Not configured</span>
    <?php endif; ?>
</p>

<div class="row g-4" style="max-width:820px">
    <div class="col-lg-7">
        <div class="card border-0 shadow-sm">
            <div class="card-body">
                <form method="post" action="/admin/smtp" novalidate>
                    <?= $csrf->field() ?>
                    <div class="row g-3">
                        <div class="col-8 mb-2">
                            <label class="form-label">SMTP host</label>
                            <input type="text" name="smtp_host" class="form-control" value="<?= e($smtp['host']) ?>" placeholder="smtp.mailgun.org" required>
                        </div>
                        <div class="col-4 mb-2">
                            <label class="form-label">Port</label>
                            <input type="number" name="smtp_port" class="form-control" value="<?= e($smtp['port']) ?>" required>
                        </div>
                    </div>
                    <div class="mb-2">
                        <label class="form-label">Encryption</label>
                        <select name="smtp_encryption" class="form-select">
                            <?php foreach ($encryptions as $enc): ?>
                                <option value="<?= e($enc) ?>" <?= $enc === $smtp['encryption'] ? 'selected' : '' ?>>
                                    <?= strtoupper($enc) ?><?= $enc === 'tls' ? ' (STARTTLS, port 587)' : ($enc === 'ssl' ? ' (implicit, port 465)' : ' (none)') ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="mb-2">
                        <label class="form-label">Username</label>
                        <input type="text" name="smtp_username" class="form-control" value="<?= e($smtp['username']) ?>" autocomplete="off">
                    </div>
                    <div class="mb-2">
                        <label class="form-label">Password</label>
                        <input type="password" name="smtp_password" class="form-control" autocomplete="new-password"
                               placeholder="<?= $smtp['has_password'] ? '•••••••• (unchanged)' : 'SMTP password' ?>">
                        <div class="form-text">Stored encrypted. Leave blank to keep the current password.</div>
                    </div>
                    <hr>
                    <div class="row g-3">
                        <div class="col-md-6 mb-2">
                            <label class="form-label">From address</label>
                            <input type="email" name="smtp_from_address" class="form-control" value="<?= e($smtp['from_address']) ?>" required>
                        </div>
                        <div class="col-md-6 mb-2">
                            <label class="form-label">From name</label>
                            <input type="text" name="smtp_from_name" class="form-control" value="<?= e($smtp['from_name']) ?>">
                        </div>
                    </div>
                    <button class="btn btn-primary mt-2" type="submit"><i class="bi bi-save"></i> Save settings</button>
                </form>
            </div>
        </div>
    </div>
    <div class="col-lg-5">
        <div class="card border-0 bg-body-tertiary">
            <div class="card-body">
                <h6><i class="bi bi-send text-primary"></i> Send a test email</h6>
                <p class="small text-secondary">Verify delivery with a quick test message.</p>
                <form method="post" action="/admin/smtp/test">
                    <?= $csrf->field() ?>
                    <div class="input-group">
                        <input type="email" name="test_email" class="form-control" placeholder="you@example.com">
                        <button class="btn btn-outline-primary" type="submit">Send test</button>
                    </div>
                    <div class="form-text">Save your settings first. Leave blank to send to your own address.</div>
                </form>
            </div>
        </div>
    </div>
</div>
