<section class="card">
    <div class="toolbar"><h2>My Integration Profile</h2><span class="badge">Per User</span></div>
    <p class="muted">Each user can store their own sending criteria and credentials here. Leave personal fields blank to use the admin-level defaults.</p>
    <form method="post" action="index.php?route=settings/saveMyProfile">
        <?= csrf_input() ?>
        <div class="grid two">
            <div>
                <h3>My Defaults</h3>
                <div class="form-grid">
                    <select name="preferred_channel">
                        <option value="email" <?= ($profile['preferred_channel'] ?? 'email') === 'email' ? 'selected' : '' ?>>Prefer Email</option>
                        <option value="sms" <?= ($profile['preferred_channel'] ?? '') === 'sms' ? 'selected' : '' ?>>Prefer SMS</option>
                    </select>
                    <input name="signature_name" placeholder="Signature Name" value="<?= e($profile['signature_name'] ?? '') ?>">
                    <input name="reply_to_email" placeholder="Reply-To Email" value="<?= e($profile['reply_to_email'] ?? '') ?>">
                    <input name="default_sms_footer" placeholder="My SMS Footer" value="<?= e($profile['default_sms_footer'] ?? '') ?>">
                    <input name="default_email_footer" placeholder="My Email Footer" value="<?= e($profile['default_email_footer'] ?? '') ?>">
                </div>
                <h3>My Twilio Profile</h3>
                <div class="form-grid">
                    <label><input type="checkbox" name="use_personal_twilio" value="1" <?= !empty($profile['use_personal_twilio']) ? 'checked' : '' ?>> Use my own Twilio credentials</label>
                    <input name="twilio_sid" placeholder="My Twilio SID" value="<?= e($profile['twilio_sid'] ?? '') ?>">
                    <input name="twilio_token" placeholder="My Twilio Token" value="<?= e($profile['twilio_token'] ?? '') ?>">
                    <input name="twilio_from_number" placeholder="My Twilio From Number" value="<?= e($profile['twilio_from_number'] ?? '') ?>">
                </div>
            </div>
            <div>
                <h3>My Mail Profile</h3>
                <p class="muted small">Your personal From and Reply-To values still apply when the system uses SendGrid API. Enable personal SMTP only if you want to override the shared delivery transport.</p>
                <div class="form-grid">
                    <label><input type="checkbox" name="use_personal_mail" value="1" <?= !empty($profile['use_personal_mail']) ? 'checked' : '' ?>> Use my own mail credentials</label>
                    <input name="mail_from_name" placeholder="My From Name" value="<?= e($profile['mail_from_name'] ?? '') ?>">
                    <input name="mail_from_email" placeholder="My From Email" value="<?= e($profile['mail_from_email'] ?? '') ?>">
                    <label><input type="checkbox" name="smtp_enabled" value="1" <?= !empty($profile['smtp_enabled']) ? 'checked' : '' ?>> Use SMTP</label>
                    <input name="smtp_host" placeholder="My SMTP Host" value="<?= e($profile['smtp_host'] ?? '') ?>">
                    <input name="smtp_port" placeholder="My SMTP Port" value="<?= e($profile['smtp_port'] ?? '587') ?>">
                    <input name="smtp_username" placeholder="My SMTP Username" value="<?= e($profile['smtp_username'] ?? '') ?>">
                    <input name="smtp_password" placeholder="My SMTP Password" value="<?= e($profile['smtp_password'] ?? '') ?>">
                    <select name="smtp_secure">
                        <option value="tls" <?= ($profile['smtp_secure'] ?? 'tls') === 'tls' ? 'selected' : '' ?>>TLS</option>
                        <option value="ssl" <?= ($profile['smtp_secure'] ?? '') === 'ssl' ? 'selected' : '' ?>>SSL</option>
                        <option value="none" <?= ($profile['smtp_secure'] ?? '') === 'none' ? 'selected' : '' ?>>None</option>
                    </select>
                </div>
            </div>
        </div>
        <button class="btn primary">Save My Profile</button>
    </form>
</section>


<section class="grid two">
    <div class="card">
        <div class="toolbar"><h2>Security</h2><span class="badge">2FA</span></div>
        <?php if (!empty($user['two_factor_enabled'])): ?>
            <p class="muted">Two-factor authentication is currently enabled for your account.</p>
            <form method="post" action="index.php?route=settings/disableTwoFactor">
                <?= csrf_input() ?>
                <button class="btn danger">Disable Two-Factor Authentication</button>
            </form>
        <?php else: ?>
            <p class="muted">Scan this secret with Google Authenticator, 1Password, Authy, or another TOTP app, then enter the current 6-digit code to enable 2FA.</p>
            <div class="card" style="background:#f7f9fc;margin:12px 0;">
                <div><strong>Secret:</strong> <code><?= e($pendingTwoFactorSecret ?? '') ?></code></div>
                <div class="small muted" style="margin-top:8px;word-break:break-all;"><strong>OTP URI:</strong> <?= e($pendingTwoFactorUri ?? '') ?></div>
            </div>
            <form method="post" action="index.php?route=settings/enableTwoFactor">
                <?= csrf_input() ?>
                <label>Authenticator Code</label>
                <input type="text" name="code" inputmode="numeric" pattern="[0-9]{6}" maxlength="6" required>
                <button class="btn primary">Enable Two-Factor Authentication</button>
            </form>
        <?php endif; ?>
    </div>
    <div class="card">
        <h2>Secure Credentials</h2>
        <p class="muted">Sensitive Twilio, SendGrid, SMTP, IMAP, and per-user secret values are stored encrypted at rest when saved through the admin panel.</p>
        <ul class="clean-list">
            <li>Global Twilio token encryption</li>
            <li>Global SendGrid API key encryption</li>
            <li>Global SMTP password encryption</li>
            <li>Global IMAP password encryption</li>
            <li>Personal Twilio token encryption</li>
            <li>Personal SMTP password encryption</li>
        </ul>
    </div>
</section>
