<?php
require 'includes/db.php';
require 'includes/auth.php'; // Your login session check

$merchant_id = $_SESSION['merchant_id'] ?? null;
if (!$merchant_id) {
    header("Location: login.php");
    exit;
}

// Fetch merchant data
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$merchant_id]);
$merchant = $stmt->fetch(PDO::FETCH_ASSOC);

// Save webhook details
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $webhook_url = trim($_POST['webhook_url'] ?? '');
    $webhook_secret = trim($_POST['webhook_secret'] ?? '');

    $stmt = $pdo->prepare("UPDATE users SET webhook_url = ?, webhook_secret = ? WHERE id = ?");
    $stmt->execute([$webhook_url, $webhook_secret, $merchant_id]);

    $merchant['webhook_url'] = $webhook_url;
    $merchant['webhook_secret'] = $webhook_secret;
    $success = "✅ Webhook details updated.";
}

// Fetch transactions
$stmt = $pdo->prepare("SELECT * FROM transactions WHERE user_id = ? ORDER BY id DESC LIMIT 20");
$stmt->execute([$merchant_id]);
$transactions = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<!DOCTYPE html>
<html>
<head>
    <title>Merchant Dashboard</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css" rel="stylesheet">
    <style>
        body { background-color: #f8f9fa; }
        .dashboard-card {
            background: #fff;
            border-radius: 12px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.05);
            padding: 25px;
            margin-bottom: 30px;
        }
    </style>
</head>
<body class="p-4">
    <div class="container">
        <h2 class="mb-4"><i class="bi bi-person-check"></i> Merchant Dashboard</h2>

        <?php if (isset($success)): ?>
            <div class="alert alert-success"><?= htmlspecialchars($success) ?></div>
        <?php endif; ?>

        <div class="dashboard-card mb-4">
            <h4><i class="bi bi-wallet2"></i> Account Details</h4>
            <p><strong>Name:</strong> <?= htmlspecialchars($merchant['name']) ?></p>
            <p><strong>Merchant Key:</strong> <code><?= htmlspecialchars($merchant['merchant_key']) ?></code></p>
            <p><strong>Balance:</strong> ₹<?= number_format($merchant['balance'], 2) ?> <?= htmlspecialchars($merchant['currency']) ?></p>
        </div>

        <div class="dashboard-card mb-4">
            <h4><i class="bi bi-link-45deg"></i> Webhook Settings</h4>
            <form method="POST">
                <div class="mb-3">
                    <label class="form-label">Webhook URL</label>
                    <input type="url" name="webhook_url" value="<?= htmlspecialchars($merchant['webhook_url']) ?>" class="form-control" placeholder="https://yourserver.com/callback" required>
                </div>
                <div class="mb-3">
                    <label class="form-label">Webhook Secret</label>
                    <input type="text" name="webhook_secret" value="<?= htmlspecialchars($merchant['webhook_secret']) ?>" class="form-control" placeholder="Used to sign callbacks" required>
                </div>
                <button class="btn btn-primary"><i class="bi bi-save"></i> Save Webhook Settings</button>
            </form>
            <p class="text-muted mt-2 small">
                We'll POST transaction notifications to your webhook with a signed HMAC header.
            </p>
        </div>

        <div class="dashboard-card">
            <h4><i class="bi bi-clock-history"></i> Recent Transactions</h4>
            <?php if (empty($transactions)): ?>
                <div class="alert alert-warning">No transactions found.</div>
            <?php else: ?>
                <div class="table-responsive">
                    <table class="table table-bordered table-hover align-middle">
                        <thead class="table-dark">
                            <tr>
                                <th>ID</th>
                                <th>Reference</th>
                                <th>Type</th>
                                <th>Amount</th>
                                <th>Fee</th>
                                <th>Status</th>
                                <th>Created At</th>
                            </tr>
                        </thead>
                        <tbody>
                        <?php foreach ($transactions as $txn): ?>
                            <tr>
                                <td><?= $txn['id'] ?></td>
                                <td><?= htmlspecialchars($txn['reference']) ?></td>
                                <td><?= ucfirst($txn['type']) ?></td>
                                <td><?= number_format($txn['amount'], 2) ?> <?= htmlspecialchars($txn['currency']) ?></td>
                                <td><?= number_format($txn['fee'], 2) ?></td>
                                <td>
                                    <span class="badge bg-<?= $txn['status'] === 'success' ? 'success' : ($txn['status'] === 'pending' ? 'warning' : 'danger') ?>">
                                        <?= ucfirst($txn['status']) ?>
                                    </span>
                                </td>
                                <td><?= $txn['created_at'] ?></td>
                            </tr>
                        <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            <?php endif; ?>
        </div>
    </div>
</body>
</html>
