<?php
ob_start();
/**
 * Tom's Java Jive - Admin Email Settings
 */

$pageTitle = 'Email Settings';
require_once __DIR__ . '/includes/header.php';

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

    if ($section === 'cybermail') {
        setSetting('email_cybermail', [
            'api_key'    => trim($_POST['cybermail_api_key'] ?? ''),
            'from_email' => trim($_POST['from_email'] ?? ''),
            'from_name'  => trim($_POST['from_name'] ?? '')
        ]);
        setFlash('success', 'CyberMail settings updated');
    }

    if ($section === 'notifications') {
        setSetting('email_notifications', [
            'order_confirmation' => isset($_POST['notif_order_confirmation']),
            'order_shipped'      => isset($_POST['notif_order_shipped']),
            'order_delivered'    => isset($_POST['notif_order_delivered']),
            'abandoned_cart'     => isset($_POST['notif_abandoned_cart']),
            'low_stock'          => isset($_POST['notif_low_stock']),
            'admin_new_order'    => isset($_POST['notif_admin_new_order']),
            'admin_email'        => trim($_POST['admin_email'] ?? '')
        ]);
        setFlash('success', 'Notification settings updated');
    }

    if ($section === 'test') {
        $testEmail = trim($_POST['test_email'] ?? '');
        if ($testEmail && filter_var($testEmail, FILTER_VALIDATE_EMAIL)) {
            $sent = sendEmail($testEmail, 'Test Email from Tom\'s Java Jive',
                '<div style="font-family: Arial; padding: 20px;"><h2>Test Email</h2><p>If you received this, your CyberMail settings are working correctly!</p></div>'
            );
            if ($sent) {
                setFlash('success', 'Test email sent to ' . $testEmail);
            } else {
                setFlash('error', 'Failed to send test email. Check CYBERMAIL_API_KEY in config.');
            }
        }
    }

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

$cybermail = getSetting('email_cybermail', [
    'api_key'    => '',
    'from_email' => '',
    'from_name'  => "Tom's Java Jive"
]);

$notifications = getSetting('email_notifications', [
    'order_confirmation' => true,
    'order_shipped'      => true,
    'order_delivered'    => true,
    'abandoned_cart'     => false,
    'low_stock'          => true,
    'admin_new_order'    => true,
    'admin_email'        => ''
]);
?>

<div class="page-header">
    <h1 class="page-title">Email Settings</h1>
</div>

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

<div style="display: grid; grid-template-columns: 200px 1fr; gap: 1.5rem;">
    <div>
        <div class="admin-card">
            <div class="admin-card-body" style="padding: 0.5rem;">
                <a href="/admin/settings.php" class="nav-item"><i class="fas fa-store"></i> General</a>
                <a href="/admin/shipping.php" class="nav-item"><i class="fas fa-truck"></i> Shipping</a>
                <a href="/admin/payments.php" class="nav-item"><i class="fas fa-credit-card"></i> Payments</a>
                <a href="/admin/emails.php" class="nav-item active"><i class="fas fa-envelope"></i> Emails</a>
            </div>
        </div>
    </div>

    <div>
        <!-- CyberMail Settings -->
        <form method="POST">
            <input type="hidden" name="section" value="cybermail">
            <div class="admin-card">
                <div class="admin-card-header">
                    <h3 class="admin-card-title"><i class="fas fa-paper-plane"></i> CyberMail Configuration</h3>
                </div>
                <div class="admin-card-body">
                    <div class="form-group">
                        <label class="form-label">CyberMail API Key</label>
                        <input type="password" name="cybermail_api_key" class="form-input"
                               value="<?= htmlspecialchars($cybermail['api_key']) ?>"
                               placeholder="sk_live_...">
                        <small class="text-muted">Manage at <a href="https://platform.cyberpersons.com/email/api-keys/" target="_blank">CyberMail Dashboard</a></small>
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label class="form-label">From Email</label>
                            <input type="email" name="from_email" class="form-input"
                                   value="<?= htmlspecialchars($cybermail['from_email']) ?>"
                                   placeholder="noreply@orbishosting.com">
                        </div>
                        <div class="form-group">
                            <label class="form-label">From Name</label>
                            <input type="text" name="from_name" class="form-input"
                                   value="<?= htmlspecialchars($cybermail['from_name']) ?>"
                                   placeholder="Tom's Java Jive">
                        </div>
                    </div>

                    <button type="submit" class="btn btn-primary">Save CyberMail Settings</button>
                </div>
            </div>
        </form>

        <!-- Notification Settings -->
        <form method="POST">
            <input type="hidden" name="section" value="notifications">
            <div class="admin-card">
                <div class="admin-card-header">
                    <h3 class="admin-card-title">Email Notifications</h3>
                </div>
                <div class="admin-card-body">
                    <h4 style="margin-bottom: 1rem; font-size: 0.9rem; color: var(--admin-text-muted);">Customer Notifications</h4>

                    <div class="form-group">
                        <label style="display: flex; align-items: center; gap: 0.5rem; cursor: pointer;">
                            <input type="checkbox" name="notif_order_confirmation" <?= $notifications['order_confirmation'] ? 'checked' : '' ?>>
                            Order confirmation email
                        </label>
                    </div>

                    <div class="form-group">
                        <label style="display: flex; align-items: center; gap: 0.5rem; cursor: pointer;">
                            <input type="checkbox" name="notif_order_shipped" <?= $notifications['order_shipped'] ? 'checked' : '' ?>>
                            Order shipped notification
                        </label>
                    </div>

                    <div class="form-group">
                        <label style="display: flex; align-items: center; gap: 0.5rem; cursor: pointer;">
                            <input type="checkbox" name="notif_order_delivered" <?= $notifications['order_delivered'] ? 'checked' : '' ?>>
                            Order delivered notification
                        </label>
                    </div>

                    <div class="form-group">
                        <label style="display: flex; align-items: center; gap: 0.5rem; cursor: pointer;">
                            <input type="checkbox" name="notif_abandoned_cart" <?= $notifications['abandoned_cart'] ? 'checked' : '' ?>>
                            Abandoned cart reminders
                        </label>
                    </div>

                    <hr style="margin: 1.5rem 0;">
                    <h4 style="margin-bottom: 1rem; font-size: 0.9rem; color: var(--admin-text-muted);">Admin Notifications</h4>

                    <div class="form-group">
                        <label style="display: flex; align-items: center; gap: 0.5rem; cursor: pointer;">
                            <input type="checkbox" name="notif_admin_new_order" <?= $notifications['admin_new_order'] ? 'checked' : '' ?>>
                            New order notification
                        </label>
                    </div>

                    <div class="form-group">
                        <label style="display: flex; align-items: center; gap: 0.5rem; cursor: pointer;">
                            <input type="checkbox" name="notif_low_stock" <?= $notifications['low_stock'] ? 'checked' : '' ?>>
                            Low stock alerts
                        </label>
                    </div>

                    <div class="form-group mb-0">
                        <label class="form-label">Admin Email Address</label>
                        <input type="email" name="admin_email" class="form-input"
                               value="<?= htmlspecialchars($notifications['admin_email']) ?>"
                               placeholder="admin@yourdomain.com">
                    </div>

                    <button type="submit" class="btn btn-primary mt-2">Save Notification Settings</button>
                </div>
            </div>
        </form>

        <!-- Test Email -->
        <form method="POST">
            <input type="hidden" name="section" value="test">
            <div class="admin-card">
                <div class="admin-card-header">
                    <h3 class="admin-card-title">Test Email</h3>
                </div>
                <div class="admin-card-body">
                    <div class="form-group mb-0">
                        <label class="form-label">Send test email to:</label>
                        <div style="display: flex; gap: 0.5rem;">
                            <input type="email" name="test_email" class="form-input" placeholder="your@email.com" required>
                            <button type="submit" class="btn btn-secondary">Send Test</button>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>

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