<?php
/**
 * Tom's Java Jive - Admin Integrations Settings
 */

require_once __DIR__ . '/../includes/auth.php';
AdminAuth::require();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $section = $_POST['section'] ?? '';

    if ($section === 'email') {
        setSetting('cybermail_api_key',    trim($_POST['cybermail_api_key']    ?? ''));
        setSetting('cybermail_from_email', trim($_POST['cybermail_from_email'] ?? ''));
        setSetting('cybermail_from_name',  trim($_POST['cybermail_from_name']  ?? ''));
        setSetting('email_notifications_enabled', isset($_POST['email_notifications_enabled']) ? '1' : '0');
        setFlash('success', 'Email settings saved.');
    }

    if ($section === 'twilio') {
        setSetting('twilio_account_sid',        trim($_POST['twilio_account_sid']    ?? ''));
        setSetting('twilio_auth_token',         trim($_POST['twilio_auth_token']     ?? ''));
        setSetting('twilio_phone_number',       trim($_POST['twilio_phone_number']   ?? ''));
        setSetting('sms_notifications_enabled', isset($_POST['sms_notifications_enabled']) ? '1' : '0');
        setFlash('success', 'Twilio settings saved.');
    }

    if ($section === 'push') {
        setSetting('vapid_public_key',           trim($_POST['vapid_public_key']  ?? ''));
        setSetting('vapid_private_key',          trim($_POST['vapid_private_key'] ?? ''));
        setSetting('push_notifications_enabled', isset($_POST['push_notifications_enabled']) ? '1' : '0');
        setFlash('success', 'Push notification settings saved.');
    }

    if ($section === 'loyalty') {
        setSetting('loyalty_enabled', isset($_POST['loyalty_enabled']) ? '1' : '0');
        setFlash('success', 'Loyalty program settings saved.');
    }

    header('Location: /admin/integrations.php');
    exit;
}

ob_start();
$pageTitle   = 'Integrations';
$currentPage = 'integrations';
require_once __DIR__ . '/includes/header.php';

$email = [
    'api_key'   => getSetting('cybermail_api_key',    ''),
    'from'      => getSetting('cybermail_from_email', 'noreply@tomsjavajive.com'),
    'from_name' => getSetting('cybermail_from_name',  "Tom's Java Jive"),
    'enabled'   => getSetting('email_notifications_enabled', '0'),
];
$tw = [
    'sid'     => getSetting('twilio_account_sid',        ''),
    'token'   => getSetting('twilio_auth_token',         ''),
    'phone'   => getSetting('twilio_phone_number',       ''),
    'enabled' => getSetting('sms_notifications_enabled', '0'),
];
$push = [
    'pub'     => getSetting('vapid_public_key',           ''),
    'priv'    => getSetting('vapid_private_key',          ''),
    'enabled' => getSetting('push_notifications_enabled', '0'),
];
$loyaltyEnabled = getSetting('loyalty_enabled', '1') === '1';
?>

<style>
.integration-card { background: var(--admin-surface); border-radius: var(--admin-radius); margin-bottom: 1.5rem; }
.integration-header { display: flex; justify-content: space-between; align-items: center; padding: 1.25rem 1.5rem; border-bottom: 1px solid var(--admin-border); }
.integration-title { display: flex; align-items: center; gap: 1rem; }
.integration-icon { width: 48px; height: 48px; border-radius: 10px; display: flex; align-items: center; justify-content: center; font-size: 1.5rem; }
.integration-icon.email   { background: #0ea5e9; color: white; }
.integration-icon.twilio  { background: #F22F46; color: white; }
.integration-icon.push    { background: #8B5CF6; color: white; }
.integration-icon.loyalty { background: #F59E0B; color: white; }
.integration-body { padding: 1.5rem; }
.status-badge { padding: .375rem .75rem; border-radius: 20px; font-size: .75rem; font-weight: 600; }
.status-badge.configured     { background: rgba(16,185,129,.1); color: var(--admin-success); }
.status-badge.not-configured { background: rgba(245,158,11,.1);  color: var(--admin-warning); }
.status-badge.enabled  { background: rgba(16,185,129,.1); color: var(--admin-success); }
.status-badge.disabled { background: rgba(239,68,68,.1);  color: var(--admin-error); }
.key-input { font-family: monospace; font-size: .875rem; }
.help-text { font-size: .75rem; color: var(--admin-text-muted); margin-top: .25rem; }
.help-link { color: var(--admin-primary); }
</style>

<div class="page-header">
    <h1 class="page-title">Integrations</h1>
    <p class="text-muted">Configure third-party service integrations</p>
</div>

<?php if (hasFlash('success')): ?>
    <div class="alert alert-success mb-2"><i class="fas fa-check-circle"></i> <?= getFlash('success') ?></div>
<?php endif; ?>

<!-- Email — CyberMail -->
<div class="integration-card">
    <div class="integration-header">
        <div class="integration-title">
            <div class="integration-icon email"><i class="fas fa-envelope"></i></div>
            <div>
                <h3 style="margin:0;">Email — CyberMail</h3>
                <p style="margin:.25rem 0 0;color:var(--admin-text-muted);font-size:.875rem;">Transactional email — order confirmations, shipping updates, password resets</p>
            </div>
        </div>
        <?php $emailOk = !empty($email['api_key']); $emailOn = $email['enabled'] === '1'; ?>
        <span class="status-badge <?= $emailOk && $emailOn ? 'enabled' : ($emailOk ? 'configured' : 'not-configured') ?>">
            <?= $emailOk && $emailOn ? 'Enabled' : ($emailOk ? 'Configured' : 'Not Configured') ?>
        </span>
    </div>
    <div class="integration-body">
        <form method="POST">
            <input type="hidden" name="section" value="email">
            <div class="form-group">
                <label class="form-label">CyberMail API Key</label>
                <input type="password" name="cybermail_api_key" class="form-input key-input"
                       value="<?= htmlspecialchars($email['api_key']) ?>" placeholder="sk_live_...">
                <p class="help-text">Manage at <a href="https://platform.cyberpersons.com/email/api-keys/" target="_blank" class="help-link">CyberMail Dashboard</a></p>
            </div>
            <div class="form-row">
                <div class="form-group">
                    <label class="form-label">From Email</label>
                    <input type="email" name="cybermail_from_email" class="form-input"
                           value="<?= htmlspecialchars($email['from']) ?>">
                </div>
                <div class="form-group">
                    <label class="form-label">From Name</label>
                    <input type="text" name="cybermail_from_name" class="form-input"
                           value="<?= htmlspecialchars($email['from_name']) ?>">
                </div>
            </div>
            <div class="form-group">
                <label class="form-checkbox">
                    <input type="checkbox" name="email_notifications_enabled" value="1" <?= $emailOn ? 'checked' : '' ?>>
                    Enable email notifications
                </label>
            </div>
            <div style="display:flex;gap:.5rem;">
                <button type="submit" class="btn btn-primary"><i class="fas fa-save"></i> Save Settings</button>
                <button type="button" class="btn btn-secondary" onclick="testEmail()"><i class="fas fa-paper-plane"></i> Send Test Email</button>
            </div>
        </form>
    </div>
</div>

<!-- Twilio SMS -->
<div class="integration-card">
    <div class="integration-header">
        <div class="integration-title">
            <div class="integration-icon twilio"><i class="fas fa-sms"></i></div>
            <div>
                <h3 style="margin:0;">Twilio SMS</h3>
                <p style="margin:.25rem 0 0;color:var(--admin-text-muted);font-size:.875rem;">SMS notifications for orders, shipping, and promotions</p>
            </div>
        </div>
        <?php $twOk = !empty($tw['sid']) && !empty($tw['token']); $twOn = $tw['enabled'] === '1'; ?>
        <span class="status-badge <?= $twOk && $twOn ? 'enabled' : ($twOk ? 'configured' : 'not-configured') ?>">
            <?= $twOk && $twOn ? 'Enabled' : ($twOk ? 'Configured' : 'Not Configured') ?>
        </span>
    </div>
    <div class="integration-body">
        <form method="POST">
            <input type="hidden" name="section" value="twilio">
            <div class="form-row">
                <div class="form-group">
                    <label class="form-label">Account SID</label>
                    <input type="text" name="twilio_account_sid" class="form-input key-input" value="<?= htmlspecialchars($tw['sid']) ?>" placeholder="ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx">
                </div>
                <div class="form-group">
                    <label class="form-label">Auth Token</label>
                    <input type="password" name="twilio_auth_token" class="form-input key-input" value="<?= htmlspecialchars($tw['token']) ?>" placeholder="xxxxxxxx...">
                </div>
            </div>
            <div class="form-group">
                <label class="form-label">Twilio Phone Number</label>
                <input type="text" name="twilio_phone_number" class="form-input" value="<?= htmlspecialchars($tw['phone']) ?>" placeholder="+1234567890">
                <p class="help-text">Get credentials at <a href="https://console.twilio.com/" target="_blank" class="help-link">Twilio Console</a></p>
            </div>
            <div class="form-group">
                <label class="form-checkbox">
                    <input type="checkbox" name="sms_notifications_enabled" value="1" <?= $twOn ? 'checked' : '' ?>>
                    Enable SMS notifications
                </label>
            </div>
            <div style="display:flex;gap:.5rem;">
                <button type="submit" class="btn btn-primary"><i class="fas fa-save"></i> Save Settings</button>
                <button type="button" class="btn btn-secondary" onclick="testTwilio()"><i class="fas fa-sms"></i> Send Test SMS</button>
            </div>
        </form>
    </div>
</div>

<!-- Push Notifications -->
<div class="integration-card">
    <div class="integration-header">
        <div class="integration-title">
            <div class="integration-icon push"><i class="fas fa-bell"></i></div>
            <div>
                <h3 style="margin:0;">Push Notifications</h3>
                <p style="margin:.25rem 0 0;color:var(--admin-text-muted);font-size:.875rem;">Web push for order updates and promotions</p>
            </div>
        </div>
        <?php $pushOk = !empty($push['pub']) && !empty($push['priv']); $pushOn = $push['enabled'] === '1'; ?>
        <span class="status-badge <?= $pushOk && $pushOn ? 'enabled' : ($pushOk ? 'configured' : 'not-configured') ?>">
            <?= $pushOk && $pushOn ? 'Enabled' : ($pushOk ? 'Configured' : 'Not Configured') ?>
        </span>
    </div>
    <div class="integration-body">
        <form method="POST">
            <input type="hidden" name="section" value="push">
            <div class="form-group">
                <label class="form-label">VAPID Public Key</label>
                <input type="text" name="vapid_public_key" class="form-input key-input" value="<?= htmlspecialchars($push['pub']) ?>" placeholder="BNxx...">
            </div>
            <div class="form-group">
                <label class="form-label">VAPID Private Key</label>
                <input type="password" name="vapid_private_key" class="form-input key-input" value="<?= htmlspecialchars($push['priv']) ?>" placeholder="xx...">
                <p class="help-text">Generate at <a href="https://web-push-codelab.glitch.me/" target="_blank" class="help-link">Web Push Codelab</a></p>
            </div>
            <div class="form-group">
                <label class="form-checkbox">
                    <input type="checkbox" name="push_notifications_enabled" value="1" <?= $pushOn ? 'checked' : '' ?>>
                    Enable push notifications
                </label>
            </div>
            <button type="submit" class="btn btn-primary"><i class="fas fa-save"></i> Save Settings</button>
        </form>
    </div>
</div>

<!-- Loyalty Program -->
<div class="integration-card">
    <div class="integration-header">
        <div class="integration-title">
            <div class="integration-icon loyalty"><i class="fas fa-crown"></i></div>
            <div>
                <h3 style="margin:0;">Loyalty Program</h3>
                <p style="margin:.25rem 0 0;color:var(--admin-text-muted);font-size:.875rem;">Reward customers with points and tiers</p>
            </div>
        </div>
        <span class="status-badge <?= $loyaltyEnabled ? 'enabled' : 'disabled' ?>"><?= $loyaltyEnabled ? 'Enabled' : 'Disabled' ?></span>
    </div>
    <div class="integration-body">
        <form method="POST">
            <input type="hidden" name="section" value="loyalty">
            <div class="form-group">
                <label class="form-checkbox">
                    <input type="checkbox" name="loyalty_enabled" value="1" <?= $loyaltyEnabled ? 'checked' : '' ?>>
                    Enable loyalty program
                </label>
            </div>
            <div style="background:var(--admin-bg);padding:1.5rem;border-radius:var(--admin-radius);margin:1rem 0;">
                <h4 style="margin:0 0 1rem;">Tier Structure</h4>
                <table class="table" style="margin:0;">
                    <thead><tr><th>Tier</th><th>Min Points</th><th>Multiplier</th><th>Key Benefits</th></tr></thead>
                    <tbody>
                        <tr><td><span style="color:#CD7F32;"><i class="fas fa-coffee"></i></span> Bronze Bean</td><td>0</td><td>1x</td><td>1 point/$1, Birthday reward</td></tr>
                        <tr><td><span style="color:#C0C0C0;"><i class="fas fa-mug-hot"></i></span> Silver Roast</td><td>500</td><td>1.25x</td><td>Free shipping $25+, Double points weekends</td></tr>
                        <tr><td><span style="color:#FFD700;"><i class="fas fa-crown"></i></span> Gold Blend</td><td>1,500</td><td>1.5x</td><td>Free shipping all orders, Priority support</td></tr>
                        <tr><td><span style="color:#E5E4E2;"><i class="fas fa-gem"></i></span> Platinum Reserve</td><td>5,000</td><td>2x</td><td>Express shipping, VIP events, Account manager</td></tr>
                    </tbody>
                </table>
                <p class="text-muted" style="margin:1rem 0 0;font-size:.875rem;">100 points = $1 credit &bull; Points earned on every purchase</p>
            </div>
            <button type="submit" class="btn btn-primary"><i class="fas fa-save"></i> Save Settings</button>
        </form>
    </div>
</div>

<!-- Test Modal -->
<div class="modal-overlay" id="testModal">
    <div class="modal">
        <div class="modal-header">
            <h3 class="modal-title" id="testModalTitle">Send Test</h3>
            <button type="button" class="modal-close" onclick="Modal.close('testModal')">&times;</button>
        </div>
        <form id="testForm">
            <div class="modal-body">
                <div class="form-group">
                    <label class="form-label" id="testInputLabel">Recipient</label>
                    <input type="text" id="testRecipient" class="form-input" required>
                </div>
                <div id="testResult" style="display:none;padding:1rem;border-radius:var(--admin-radius);margin-top:1rem;"></div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" onclick="Modal.close('testModal')">Cancel</button>
                <button type="submit" class="btn btn-primary" id="testSubmitBtn"><i class="fas fa-paper-plane"></i> Send</button>
            </div>
        </form>
    </div>
</div>

<script>
let testType = '';
function testEmail() {
    testType = 'email';
    document.getElementById('testModalTitle').textContent = 'Send Test Email';
    document.getElementById('testInputLabel').textContent = 'Recipient Email';
    document.getElementById('testRecipient').type = 'email';
    document.getElementById('testRecipient').placeholder = 'test@example.com';
    document.getElementById('testResult').style.display = 'none';
    Modal.open('testModal');
}
function testTwilio() {
    testType = 'sms';
    document.getElementById('testModalTitle').textContent = 'Send Test SMS';
    document.getElementById('testInputLabel').textContent = 'Phone Number';
    document.getElementById('testRecipient').type = 'tel';
    document.getElementById('testRecipient').placeholder = '+1234567890';
    document.getElementById('testResult').style.display = 'none';
    Modal.open('testModal');
}
document.getElementById('testForm').addEventListener('submit', async function(e) {
    e.preventDefault();
    const recipient = document.getElementById('testRecipient').value;
    const resultDiv = document.getElementById('testResult');
    const submitBtn = document.getElementById('testSubmitBtn');
    submitBtn.disabled = true;
    submitBtn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Sending...';
    try {
        const r = await fetch('/api/test-notification.php', {
            method: 'POST',
            headers: {'Content-Type': 'application/json'},
            body: JSON.stringify({type: testType, recipient})
        });
        const data = await r.json();
        resultDiv.style.display = 'block';
        if (data.success) {
            resultDiv.style.background = 'rgba(16,185,129,.1)';
            resultDiv.innerHTML = '<i class="fas fa-check-circle" style="color:var(--admin-success);"></i> ' + (data.message || 'Sent!');
        } else {
            resultDiv.style.background = 'rgba(239,68,68,.1)';
            resultDiv.innerHTML = '<i class="fas fa-times-circle" style="color:var(--admin-error);"></i> ' + (data.error || 'Failed');
        }
    } catch (err) {
        resultDiv.style.display = 'block';
        resultDiv.style.background = 'rgba(239,68,68,.1)';
        resultDiv.innerHTML = '<i class="fas fa-times-circle" style="color:var(--admin-error);"></i> ' + err.message;
    } finally {
        submitBtn.disabled = false;
        submitBtn.innerHTML = '<i class="fas fa-paper-plane"></i> Send';
    }
});
</script>

<?php require_once __DIR__ . '/includes/footer.php'; ?>
