<?php
include('header.php');
$page_title = WEB_NAME . " | API Documentation";

// Get user's API key
$api_key_val = '';
$api_enabled = 0;
try {
    $k_stmt = $pdo->prepare("SELECT api_key, api_enabled FROM users WHERE id = :uid");
    $k_stmt->execute([':uid' => $_SESSION['user_id']]);
    $k_row = $k_stmt->fetch(PDO::FETCH_ASSOC);
    $api_key_val = $k_row['api_key'] ?? '';
    $api_enabled = (int)($k_row['api_enabled'] ?? 0);
} catch (PDOException $e) {}

$base_url = $site_data['web_url'] . '/api/v1/';
?>
<title><?php echo htmlspecialchars($page_title); ?></title>

<div class="max-w-4xl mx-auto">

<!-- Hero -->
<div class="bg-gradient-to-br from-primary to-[#6b23c2] rounded-2xl p-6 mb-6 text-white relative overflow-hidden">
    <div class="absolute -right-10 -top-10 w-40 h-40 bg-white opacity-5 rounded-full"></div>
    <div class="relative z-10">
        <h2 class="text-xl font-bold mb-1 flex items-center gap-2">
            <i class="fas fa-code"></i> Reseller API
        </h2>
        <p class="text-sm opacity-80">Integrate our services into your platform. Buy numbers, data, airtime, and SMM services programmatically.</p>
    </div>
</div>

<!-- API Key Section -->
<div class="bg-white rounded-2xl border border-gray-100 p-5 mb-6 shadow-sm">
    <h3 class="font-bold text-sm mb-4 flex items-center gap-2">
        <i class="fas fa-key text-primary"></i> Your API Key
    </h3>
    
    <?php if ($api_enabled && !empty($api_key_val)): ?>
    <div class="bg-gray-50 border border-gray-200 rounded-xl p-4 mb-4">
        <div class="flex items-center justify-between gap-3">
            <code class="text-xs bg-white border border-gray-200 px-3 py-2 rounded-lg flex-1 overflow-x-auto font-mono" id="apiKeyDisplay"><?php echo htmlspecialchars($api_key_val); ?></code>
            <button onclick="copyApiKey()" class="px-3 py-2 bg-primary text-white rounded-lg text-xs font-bold flex-shrink-0">
                <i class="fas fa-copy mr-1"></i>Copy
            </button>
        </div>
        <p class="text-xs text-red-500 mt-2"><i class="fas fa-exclamation-triangle mr-1"></i>Keep this key secret. Anyone with it can use your balance.</p>
    </div>
    <div class="flex gap-3">
        <button onclick="regenerateKey()" class="px-4 py-2 bg-amber-500 text-white rounded-lg text-xs font-bold">
            <i class="fas fa-rotate mr-1"></i>Regenerate
        </button>
        <button onclick="revokeKey()" class="px-4 py-2 bg-red-500 text-white rounded-lg text-xs font-bold">
            <i class="fas fa-trash mr-1"></i>Revoke
        </button>
    </div>
    <?php else: ?>
    <p class="text-sm text-gray-600 mb-4">You haven't generated an API key yet. Click below to get started.</p>
    <button onclick="generateKey()" class="px-5 py-3 bg-primary text-white rounded-xl text-sm font-bold">
        <i class="fas fa-wand-magic-sparkles mr-2"></i>Generate API Key
    </button>
    <?php endif; ?>
</div>

<!-- Base URL -->
<div class="bg-white rounded-2xl border border-gray-100 p-5 mb-6 shadow-sm">
    <h3 class="font-bold text-sm mb-3"><i class="fas fa-link text-primary mr-2"></i>Base URL</h3>
    <code class="block bg-gray-50 border border-gray-200 rounded-xl p-3 text-xs font-mono"><?php echo htmlspecialchars($base_url); ?>?action=ACTION&api_key=YOUR_KEY</code>
</div>

<!-- Endpoints -->
<div class="bg-white rounded-2xl border border-gray-100 p-5 mb-6 shadow-sm">
    <h3 class="font-bold text-sm mb-4"><i class="fas fa-list text-primary mr-2"></i>Available Endpoints</h3>
    
    <div class="space-y-4">
        <!-- Balance -->
        <div class="border border-gray-100 rounded-xl p-4">
            <div class="flex items-center gap-2 mb-2">
                <span class="bg-green-100 text-green-700 text-[10px] font-bold px-2 py-0.5 rounded">GET</span>
                <code class="text-xs font-mono font-bold">balance</code>
            </div>
            <p class="text-xs text-gray-600 mb-2">Check your account balance.</p>
            <code class="text-[11px] bg-gray-50 block p-2 rounded-lg">?action=balance&api_key=KEY</code>
        </div>

        <!-- SMS Countries -->
        <div class="border border-gray-100 rounded-xl p-4">
            <div class="flex items-center gap-2 mb-2">
                <span class="bg-green-100 text-green-700 text-[10px] font-bold px-2 py-0.5 rounded">GET</span>
                <code class="text-xs font-mono font-bold">sms_countries</code>
            </div>
            <p class="text-xs text-gray-600 mb-2">Get available countries for virtual numbers.</p>
            <code class="text-[11px] bg-gray-50 block p-2 rounded-lg">?action=sms_countries&api_key=KEY</code>
        </div>

        <!-- SMS Services -->
        <div class="border border-gray-100 rounded-xl p-4">
            <div class="flex items-center gap-2 mb-2">
                <span class="bg-green-100 text-green-700 text-[10px] font-bold px-2 py-0.5 rounded">GET</span>
                <code class="text-xs font-mono font-bold">sms_services</code>
            </div>
            <p class="text-xs text-gray-600 mb-2">Get services available for a country.</p>
            <code class="text-[11px] bg-gray-50 block p-2 rounded-lg">?action=sms_services&api_key=KEY&country_id=123</code>
        </div>

        <!-- SMS Buy -->
        <div class="border border-gray-100 rounded-xl p-4">
            <div class="flex items-center gap-2 mb-2">
                <span class="bg-green-100 text-green-700 text-[10px] font-bold px-2 py-0.5 rounded">GET</span>
                <code class="text-xs font-mono font-bold">sms_buy</code>
            </div>
            <p class="text-xs text-gray-600 mb-2">Purchase a virtual number.</p>
            <code class="text-[11px] bg-gray-50 block p-2 rounded-lg">?action=sms_buy&api_key=KEY&country_id=123&service_id=whatsapp</code>
        </div>

        <!-- SMS Check -->
        <div class="border border-gray-100 rounded-xl p-4">
            <div class="flex items-center gap-2 mb-2">
                <span class="bg-green-100 text-green-700 text-[10px] font-bold px-2 py-0.5 rounded">GET</span>
                <code class="text-xs font-mono font-bold">sms_check</code>
            </div>
            <p class="text-xs text-gray-600 mb-2">Check if SMS code has arrived.</p>
            <code class="text-[11px] bg-gray-50 block p-2 rounded-lg">?action=sms_check&api_key=KEY&order_id=ORDERID</code>
        </div>

        <!-- SMS Cancel -->
        <div class="border border-gray-100 rounded-xl p-4">
            <div class="flex items-center gap-2 mb-2">
                <span class="bg-green-100 text-green-700 text-[10px] font-bold px-2 py-0.5 rounded">GET</span>
                <code class="text-xs font-mono font-bold">sms_cancel</code>
            </div>
            <p class="text-xs text-gray-600 mb-2">Cancel a number and get refund.</p>
            <code class="text-[11px] bg-gray-50 block p-2 rounded-lg">?action=sms_cancel&api_key=KEY&order_id=ORDERID</code>
        </div>

        <!-- SMM Services -->
        <div class="border border-gray-100 rounded-xl p-4">
            <div class="flex items-center gap-2 mb-2">
                <span class="bg-green-100 text-green-700 text-[10px] font-bold px-2 py-0.5 rounded">GET</span>
                <code class="text-xs font-mono font-bold">smm_services</code>
            </div>
            <p class="text-xs text-gray-600 mb-2">Get all SMM services (followers, likes, views, etc).</p>
            <code class="text-[11px] bg-gray-50 block p-2 rounded-lg">?action=smm_services&api_key=KEY</code>
        </div>

        <!-- SMM Order -->
        <div class="border border-gray-100 rounded-xl p-4">
            <div class="flex items-center gap-2 mb-2">
                <span class="bg-blue-100 text-blue-700 text-[10px] font-bold px-2 py-0.5 rounded">POST</span>
                <code class="text-xs font-mono font-bold">smm_order</code>
            </div>
            <p class="text-xs text-gray-600 mb-2">Place an SMM order.</p>
            <code class="text-[11px] bg-gray-50 block p-2 rounded-lg">POST: action=smm_order&api_key=KEY&service_id=1&link=URL&quantity=1000</code>
        </div>

        <!-- SMM Status -->
        <div class="border border-gray-100 rounded-xl p-4">
            <div class="flex items-center gap-2 mb-2">
                <span class="bg-green-100 text-green-700 text-[10px] font-bold px-2 py-0.5 rounded">GET</span>
                <code class="text-xs font-mono font-bold">smm_status</code>
            </div>
            <p class="text-xs text-gray-600 mb-2">Check SMM order status.</p>
            <code class="text-[11px] bg-gray-50 block p-2 rounded-lg">?action=smm_status&api_key=KEY&order_id=123</code>
        </div>

        <!-- VTU Plans -->
        <div class="border border-gray-100 rounded-xl p-4">
            <div class="flex items-center gap-2 mb-2">
                <span class="bg-green-100 text-green-700 text-[10px] font-bold px-2 py-0.5 rounded">GET</span>
                <code class="text-xs font-mono font-bold">vtu_plans</code>
            </div>
            <p class="text-xs text-gray-600 mb-2">Get data plans for a network.</p>
            <code class="text-[11px] bg-gray-50 block p-2 rounded-lg">?action=vtu_plans&api_key=KEY&network=MTN</code>
        </div>

        <!-- VTU Buy Data -->
        <div class="border border-gray-100 rounded-xl p-4">
            <div class="flex items-center gap-2 mb-2">
                <span class="bg-blue-100 text-blue-700 text-[10px] font-bold px-2 py-0.5 rounded">POST</span>
                <code class="text-xs font-mono font-bold">vtu_buy_data</code>
            </div>
            <p class="text-xs text-gray-600 mb-2">Purchase a data plan.</p>
            <code class="text-[11px] bg-gray-50 block p-2 rounded-lg">POST: action=vtu_buy_data&api_key=KEY&plan_id=5&phone=08012345678</code>
        </div>

        <!-- VTU Buy Airtime -->
        <div class="border border-gray-100 rounded-xl p-4">
            <div class="flex items-center gap-2 mb-2">
                <span class="bg-blue-100 text-blue-700 text-[10px] font-bold px-2 py-0.5 rounded">POST</span>
                <code class="text-xs font-mono font-bold">vtu_buy_airtime</code>
            </div>
            <p class="text-xs text-gray-600 mb-2">Purchase airtime.</p>
            <code class="text-[11px] bg-gray-50 block p-2 rounded-lg">POST: action=vtu_buy_airtime&api_key=KEY&network=MTN&phone=08012345678&amount=500</code>
        </div>

        <!-- Cable Plans -->
        <div class="border border-gray-100 rounded-xl p-4">
            <div class="flex items-center gap-2 mb-2">
                <span class="bg-green-100 text-green-700 text-[10px] font-bold px-2 py-0.5 rounded">GET</span>
                <code class="text-xs font-mono font-bold">cable_plans</code>
            </div>
            <p class="text-xs text-gray-600 mb-2">Get cable TV bouquets. Providers: GOTV, DSTV, STARTIMES</p>
            <code class="text-[11px] bg-gray-50 block p-2 rounded-lg">?action=cable_plans&api_key=KEY&provider=GOTV</code>
        </div>

        <!-- Cable Buy -->
        <div class="border border-gray-100 rounded-xl p-4">
            <div class="flex items-center gap-2 mb-2">
                <span class="bg-blue-100 text-blue-700 text-[10px] font-bold px-2 py-0.5 rounded">POST</span>
                <code class="text-xs font-mono font-bold">cable_buy</code>
            </div>
            <p class="text-xs text-gray-600 mb-2">Subscribe to a cable TV bouquet.</p>
            <code class="text-[11px] bg-gray-50 block p-2 rounded-lg">POST: action=cable_buy&api_key=KEY&plan_id=5&smart_card_no=7012345678&phone=08012345678</code>
        </div>

        <!-- Electricity Buy -->
        <div class="border border-gray-100 rounded-xl p-4">
            <div class="flex items-center gap-2 mb-2">
                <span class="bg-blue-100 text-blue-700 text-[10px] font-bold px-2 py-0.5 rounded">POST</span>
                <code class="text-xs font-mono font-bold">electricity_buy</code>
            </div>
            <p class="text-xs text-gray-600 mb-2">Buy electricity token. Discos: IKEDC, EKEDC, AEDC, KEDCO, PHED, JED, IBEDC, KAEDCO, BEDC</p>
            <code class="text-[11px] bg-gray-50 block p-2 rounded-lg">POST: action=electricity_buy&api_key=KEY&disco=IKEDC&meter_no=12345678&amount=5000&phone=080...&type=prepaid</code>
        </div>

        <!-- Gift Card List -->
        <div class="border border-gray-100 rounded-xl p-4">
            <div class="flex items-center gap-2 mb-2">
                <span class="bg-green-100 text-green-700 text-[10px] font-bold px-2 py-0.5 rounded">GET</span>
                <code class="text-xs font-mono font-bold">giftcard_list</code>
            </div>
            <p class="text-xs text-gray-600 mb-2">List available gift cards. Optional country filter.</p>
            <code class="text-[11px] bg-gray-50 block p-2 rounded-lg">?action=giftcard_list&api_key=KEY&country=US</code>
        </div>

        <!-- Gift Card Buy -->
        <div class="border border-gray-100 rounded-xl p-4">
            <div class="flex items-center gap-2 mb-2">
                <span class="bg-blue-100 text-blue-700 text-[10px] font-bold px-2 py-0.5 rounded">POST</span>
                <code class="text-xs font-mono font-bold">giftcard_buy</code>
            </div>
            <p class="text-xs text-gray-600 mb-2">Purchase a gift card.</p>
            <code class="text-[11px] bg-gray-50 block p-2 rounded-lg">POST: action=giftcard_buy&api_key=KEY&product_id=15&quantity=1</code>
        </div>

        <!-- Virtual Card Create -->
        <div class="border border-gray-100 rounded-xl p-4">
            <div class="flex items-center gap-2 mb-2">
                <span class="bg-blue-100 text-blue-700 text-[10px] font-bold px-2 py-0.5 rounded">POST</span>
                <code class="text-xs font-mono font-bold">vcard_create</code>
            </div>
            <p class="text-xs text-gray-600 mb-2">Create and fund a virtual card.</p>
            <code class="text-[11px] bg-gray-50 block p-2 rounded-lg">POST: action=vcard_create&api_key=KEY&currency=USD&amount=5</code>
        </div>

        <!-- Virtual Card List -->
        <div class="border border-gray-100 rounded-xl p-4">
            <div class="flex items-center gap-2 mb-2">
                <span class="bg-green-100 text-green-700 text-[10px] font-bold px-2 py-0.5 rounded">GET</span>
                <code class="text-xs font-mono font-bold">vcard_list</code>
            </div>
            <p class="text-xs text-gray-600 mb-2">List your virtual cards with details.</p>
            <code class="text-[11px] bg-gray-50 block p-2 rounded-lg">?action=vcard_list&api_key=KEY</code>
        </div>
    </div>
</div>

<!-- Response Format -->
<div class="bg-white rounded-2xl border border-gray-100 p-5 mb-6 shadow-sm">
    <h3 class="font-bold text-sm mb-3"><i class="fas fa-code text-primary mr-2"></i>Response Format</h3>
    <p class="text-xs text-gray-600 mb-3">All responses are JSON. Success and error examples:</p>
    <pre class="bg-gray-900 text-green-400 rounded-xl p-4 text-[11px] overflow-x-auto mb-3">// Success
{
  "status": "success",
  "balance": "15000.00",
  "currency": "NGN"
}

// Error
{
  "status": "error",
  "message": "Insufficient balance."
}</pre>
</div>

</div>

<script>
function copyApiKey() {
    var key = document.getElementById('apiKeyDisplay').innerText;
    navigator.clipboard.writeText(key).then(function() {
        Swal.fire({icon:'success',title:'Copied!',text:'API key copied to clipboard.',timer:1500,showConfirmButton:false});
    });
}

function generateKey() {
    Swal.fire({title:'Generate API Key?',text:'This will create a new API key for your account.',icon:'question',showCancelButton:true,confirmButtonColor:'#4D148C',confirmButtonText:'Generate'}).then(function(r){
        if(!r.isConfirmed) return;
        fetch('ajax/api_key.php',{method:'POST',headers:{'Content-Type':'application/x-www-form-urlencoded'},body:'action=generate'})
        .then(function(r){return r.json();})
        .then(function(d){
            if(d.status==='1'){Swal.fire({icon:'success',title:'Done!',text:d.msg,timer:2000,showConfirmButton:false}).then(function(){location.reload();});}
            else{Swal.fire('Error',d.msg,'error');}
        });
    });
}

function regenerateKey() {
    Swal.fire({title:'Regenerate Key?',text:'Your old key will stop working immediately.',icon:'warning',showCancelButton:true,confirmButtonColor:'#f59e0b',confirmButtonText:'Regenerate'}).then(function(r){
        if(!r.isConfirmed) return;
        fetch('ajax/api_key.php',{method:'POST',headers:{'Content-Type':'application/x-www-form-urlencoded'},body:'action=regenerate'})
        .then(function(r){return r.json();})
        .then(function(d){
            if(d.status==='1'){Swal.fire({icon:'success',title:'Regenerated!',text:d.msg,timer:2000,showConfirmButton:false}).then(function(){location.reload();});}
            else{Swal.fire('Error',d.msg,'error');}
        });
    });
}

function revokeKey() {
    Swal.fire({title:'Revoke API Key?',text:'This will disable your API access. You can generate a new key later.',icon:'warning',showCancelButton:true,confirmButtonColor:'#ef4444',confirmButtonText:'Revoke'}).then(function(r){
        if(!r.isConfirmed) return;
        fetch('ajax/api_key.php',{method:'POST',headers:{'Content-Type':'application/x-www-form-urlencoded'},body:'action=revoke'})
        .then(function(r){return r.json();})
        .then(function(d){
            if(d.status==='1'){Swal.fire({icon:'success',title:'Revoked!',text:d.msg,timer:2000,showConfirmButton:false}).then(function(){location.reload();});}
            else{Swal.fire('Error',d.msg,'error');}
        });
    });
}
</script>

<?php include('footer.php'); ?>
