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

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

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

    if ($section === 'stripe') {
        setSetting('payment_stripe', [
            'enabled' => isset($_POST['stripe_enabled']),
            'test_mode' => isset($_POST['stripe_test_mode']),
            'publishable_key' => trim($_POST['stripe_publishable_key'] ?? ''),
            'secret_key' => trim($_POST['stripe_secret_key'] ?? ''),
            'webhook_secret' => trim($_POST['stripe_webhook_secret'] ?? '')
        ]);
        setFlash('success', 'Stripe settings updated');
    }

    if ($section === 'square') {
        setSetting('payment_square', [
            'enabled' => isset($_POST['square_enabled']),
            'sandbox' => isset($_POST['square_sandbox']),
            'app_id' => trim($_POST['square_app_id'] ?? ''),
            'location_id' => trim($_POST['square_location_id'] ?? ''),
            'access_token' => trim($_POST['square_access_token'] ?? ''),
            'webhook_signature_key' => trim($_POST['square_webhook_signature_key'] ?? '')
        ]);
        setFlash('success', 'Square settings updated');
    }

    if ($section === 'methods') {
        setSetting('payment_methods', [
            'card' => isset($_POST['method_card']),
            'cash' => isset($_POST['method_cash']),
            'wallet' => isset($_POST['method_wallet']),
            'gift_card' => isset($_POST['method_gift_card'])
        ]);
        setFlash('success', 'Payment methods updated');
    }

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

$stripe = getSetting('payment_stripe', [
    'enabled' => true,
    'test_mode' => true,
    'publishable_key' => '',
    'secret_key' => '',
    'webhook_secret' => ''
]);

$square = getSetting('payment_square', [
    'enabled' => defined('PAYMENT_PROCESSOR') && PAYMENT_PROCESSOR === 'square',
    'sandbox' => defined('SQUARE_ENV') && SQUARE_ENV === 'sandbox',
    'app_id' => defined('SQUARE_APP_ID') ? SQUARE_APP_ID : '',
    'location_id' => defined('SQUARE_LOCATION_ID') ? SQUARE_LOCATION_ID : '',
    'access_token' => defined('SQUARE_ACCESS_TOKEN') ? SQUARE_ACCESS_TOKEN : '',
    'webhook_signature_key' => defined('SQUARE_WEBHOOK_SIGNATURE_KEY') ? SQUARE_WEBHOOK_SIGNATURE_KEY : ''
]);

$methods = getSetting('payment_methods', [
    'card' => true,
    'cash' => true,
    'wallet' => true,
    'gift_card' => true
]);
?>

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

<?php if (hasFlash('success')): ?>
    <div class="alert alert-success"><i class="fas fa-check-circle"></i> <?= getFlash('success') ?></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 active"><i class="fas fa-credit-card"></i> Payments</a>
                <a href="/admin/emails.php" class="nav-item"><i class="fas fa-envelope"></i> Emails</a>
            </div>
        </div>
    </div>

    <div>
        <!-- Square Settings -->
        <form method="POST">
            <input type="hidden" name="section" value="square">
            <div class="admin-card">
                <div class="admin-card-header">
                    <h3 class="admin-card-title"><i class="fas fa-square" style="color: #006AFF;"></i> Square</h3>
                </div>
                <div class="admin-card-body">
                    <div class="form-group">
                        <label style="display: flex; align-items: center; gap: 0.5rem; cursor: pointer;">
                            <input type="checkbox" name="square_enabled" <?= $square['enabled'] ? 'checked' : '' ?>>
                            Enable Square payments
                        </label>
                    </div>

                    <div class="form-group">
                        <label style="display: flex; align-items: center; gap: 0.5rem; cursor: pointer;">
                            <input type="checkbox" name="square_sandbox" <?= $square['sandbox'] ? 'checked' : '' ?>>
                            Sandbox mode (use sandbox app/location/token)
                        </label>
                    </div>

                    <div class="form-group">
                        <label class="form-label">Application ID</label>
                        <input type="text" name="square_app_id" class="form-input"
                               value="<?= htmlspecialchars($square['app_id']) ?>"
                               placeholder="sq0idp-...">
                    </div>

                    <div class="form-group">
                        <label class="form-label">Location ID</label>
                        <input type="text" name="square_location_id" class="form-input"
                               value="<?= htmlspecialchars($square['location_id']) ?>"
                               placeholder="L...">
                    </div>

                    <div class="form-group">
                        <label class="form-label">Access Token</label>
                        <input type="password" name="square_access_token" class="form-input"
                               value="<?= htmlspecialchars($square['access_token']) ?>"
                               placeholder="EAAA...">
                    </div>

                    <div class="form-group mb-0">
                        <label class="form-label">Webhook Signature Key</label>
                        <input type="password" name="square_webhook_signature_key" class="form-input"
                               value="<?= htmlspecialchars($square['webhook_signature_key']) ?>"
                               placeholder="...">
                        <small class="text-muted">From the Square Developer Dashboard's webhook subscription for this site</small>
                    </div>

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

        <!-- Stripe Settings (legacy) -->
        <form method="POST">
            <input type="hidden" name="section" value="stripe">
            <div class="admin-card">
                <div class="admin-card-header">
                    <h3 class="admin-card-title"><i class="fab fa-stripe" style="color: #635BFF;"></i> Stripe <small class="text-muted">(legacy)</small></h3>
                </div>
                <div class="admin-card-body">
                    <div class="form-group">
                        <label style="display: flex; align-items: center; gap: 0.5rem; cursor: pointer;">
                            <input type="checkbox" name="stripe_enabled" <?= $stripe['enabled'] ? 'checked' : '' ?>>
                            Enable Stripe payments
                        </label>
                    </div>

                    <div class="form-group">
                        <label style="display: flex; align-items: center; gap: 0.5rem; cursor: pointer;">
                            <input type="checkbox" name="stripe_test_mode" <?= $stripe['test_mode'] ? 'checked' : '' ?>>
                            Test mode (use test keys)
                        </label>
                    </div>

                    <div class="form-group">
                        <label class="form-label">Publishable Key</label>
                        <input type="text" name="stripe_publishable_key" class="form-input"
                               value="<?= htmlspecialchars($stripe['publishable_key']) ?>"
                               placeholder="pk_test_... or pk_live_...">
                    </div>

                    <div class="form-group">
                        <label class="form-label">Secret Key</label>
                        <input type="password" name="stripe_secret_key" class="form-input"
                               value="<?= htmlspecialchars($stripe['secret_key']) ?>"
                               placeholder="sk_test_... or sk_live_...">
                    </div>

                    <div class="form-group mb-0">
                        <label class="form-label">Webhook Secret</label>
                        <input type="password" name="stripe_webhook_secret" class="form-input"
                               value="<?= htmlspecialchars($stripe['webhook_secret']) ?>"
                               placeholder="whsec_...">
                        <small class="text-muted">Get this from your Stripe webhook settings</small>
                    </div>

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

        <!-- POS Payment Methods -->
        <form method="POST">
            <input type="hidden" name="section" value="methods">
            <div class="admin-card">
                <div class="admin-card-header">
                    <h3 class="admin-card-title">POS Payment Methods</h3>
                </div>
                <div class="admin-card-body">
                    <p class="text-muted" style="margin-bottom: 1rem;">Select which payment methods are available in the Point of Sale system.</p>

                    <div class="form-group">
                        <label style="display: flex; align-items: center; gap: 0.5rem; cursor: pointer;">
                            <input type="checkbox" name="method_card" <?= $methods['card'] ? 'checked' : '' ?>>
                            <i class="fas fa-credit-card"></i> Card (Terminal)
                        </label>
                    </div>

                    <div class="form-group">
                        <label style="display: flex; align-items: center; gap: 0.5rem; cursor: pointer;">
                            <input type="checkbox" name="method_cash" <?= $methods['cash'] ? 'checked' : '' ?>>
                            <i class="fas fa-money-bill"></i> Cash
                        </label>
                    </div>

                    <div class="form-group">
                        <label style="display: flex; align-items: center; gap: 0.5rem; cursor: pointer;">
                            <input type="checkbox" name="method_wallet" <?= $methods['wallet'] ? 'checked' : '' ?>>
                            <i class="fas fa-wallet"></i> Customer Wallet
                        </label>
                    </div>

                    <div class="form-group mb-0">
                        <label style="display: flex; align-items: center; gap: 0.5rem; cursor: pointer;">
                            <input type="checkbox" name="method_gift_card" <?= $methods['gift_card'] ? 'checked' : '' ?>>
                            <i class="fas fa-gift"></i> Gift Card
                        </label>
                    </div>

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

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