<?php
if ($c['level'] == "superadmin") {

    require_once __DIR__ . '/../../../../function/class.goldslot.php';

    // Ambil config
    $config = mysqli_fetch_assoc(mysqli_query($koneksi, "SELECT api_token, callback_token FROM tb_goldslot_config LIMIT 1"));

    if (!$config || empty($config['api_token'])) {
        echo "<script>alert('Konfigurasi GoldSlotPalace belum diatur!'); window.location='?page=goldslot&sub=config';</script>";
        exit;
    }

    $goldslot = new GoldSlotPalace($config['api_token'], $config['callback_token']);

    // Test 1: Agent Info
    $agentRes = $goldslot->getAgentInfo();
    $agentOk  = GoldSlotPalace::isSuccess($agentRes);

    // Test 2: Provider List (lang=1 karena max 6)
    $provRes  = $goldslot->getProviders(1);
    $provOk   = GoldSlotPalace::isSuccess($provRes);

    // Test 3: Callback Test
    $cbRes    = $goldslot->testCallback();
    $cbOk     = GoldSlotPalace::isSuccess($cbRes);
    ?>

    <div class="page-breadcrumb">
        <div class="row">
            <div class="col-5 align-self-center">
                <h4 class="page-title">GoldSlotPalace - Test Connection</h4>
            </div>
        </div>
    </div>

    <div class="container-fluid">
        <div class="row">
            <div class="col-lg-12">
                <div class="card">
                    <div class="card-body">
                        <h4 class="card-title">Hasil Test Koneksi API GoldSlotPalace</h4>
                        <hr>

                        <table class="table table-bordered">
                            <thead>
                                <tr>
                                    <th>Test</th>
                                    <th>Status</th>
                                    <th>Response Code</th>
                                    <th>Message</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr>
                                    <td><strong>Agent Info</strong></td>
                                    <td>
                                        <?php if ($agentOk): ?>
                                            <span class="badge bg-success">SUCCESS</span>
                                        <?php else: ?>
                                            <span class="badge bg-danger">FAILED</span>
                                        <?php endif; ?>
                                    </td>
                                    <td><?= $agentRes['code'] ?? '-'; ?></td>
                                    <td>
                                        <?= htmlspecialchars($agentRes['message'] ?? ''); ?>
                                        <?php if ($agentOk): ?>
                                            <br><small class="text-muted">Agent: <?= htmlspecialchars($agentRes['data']['name'] ?? '-'); ?>, Balance: <?= number_format($agentRes['data']['balance'] ?? 0); ?></small>
                                        <?php endif; ?>
                                    </td>
                                </tr>
                                <tr>
                                    <td><strong>Provider List</strong></td>
                                    <td>
                                        <?php if ($provOk): ?>
                                            <span class="badge bg-success">SUCCESS</span>
                                        <?php else: ?>
                                            <span class="badge bg-danger">FAILED</span>
                                        <?php endif; ?>
                                    </td>
                                    <td><?= $provRes['code'] ?? '-'; ?></td>
                                    <td>
                                        <?= htmlspecialchars($provRes['message'] ?? ''); ?>
                                        <?php if ($provOk): ?>
                                            <br><small class="text-muted"><?= count($provRes['data'] ?? []); ?> provider(s) tersedia</small>
                                        <?php endif; ?>
                                    </td>
                                </tr>
                                <tr>
                                    <td><strong>Callback Test</strong></td>
                                    <td>
                                        <?php if ($cbOk): ?>
                                            <span class="badge bg-success">SUCCESS</span>
                                        <?php else: ?>
                                            <span class="badge bg-danger">FAILED</span>
                                        <?php endif; ?>
                                    </td>
                                    <td><?= $cbRes['code'] ?? '-'; ?></td>
                                    <td><?= htmlspecialchars($cbRes['message'] ?? ''); ?></td>
                                </tr>
                            </tbody>
                        </table>

                        <?php if ($agentOk && $provOk && $cbOk): ?>
                            <div class="alert alert-success">
                                <i class="ti-check"></i> Semua test berhasil! API GoldSlotPalace terhubung dengan baik.
                            </div>
                        <?php else: ?>
                            <div class="alert alert-warning">
                                <i class="ti-alert"></i> Beberapa test gagal. Periksa kembali API token dan koneksi.
                            </div>
                        <?php endif; ?>

                        <a href="?page=goldslot&sub=config" class="btn btn-secondary">
                            <i class="ti-arrow-left"></i> Kembali ke Konfigurasi
                        </a>
                        <a href="?page=goldslot&sub=test_connection" class="btn btn-info">
                            <i class="ti-reload"></i> Test Ulang
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <?php

} else {
    echo "<script>window.location='?page=dashboard';</script>";
}
?>