<?php
require __DIR__.'/_bootstrap.php';

use Zoo\Database;
use Zoo\Admin\AdminApiAuth;
use Zoo\Admin\WithdrawalAdminService;

try{
    AdminApiAuth::requireKey();
    $db=Database::connect();

    $pendingDeposits=$db->query("
      SELECT d.id,d.user_id,u.public_id,d.reference,d.method,d.amount,d.status,
             d.payer_reference,d.submitted_at,d.created_at,d.updated_at
      FROM player_deposits d
      LEFT JOIN users u ON u.id=d.user_id
      WHERE d.status='pending'
      ORDER BY (d.submitted_at IS NULL) ASC,d.submitted_at ASC,d.id ASC
      LIMIT 200
    ")->fetchAll(PDO::FETCH_ASSOC);

    $pendingWithdrawals=(new WithdrawalAdminService($db))->pending();

    $recentDeposits=$db->query("
      SELECT d.id,d.user_id,u.public_id,d.reference,d.method,d.amount,d.status,
             d.payer_reference,d.provider_reference,d.principal_credit,d.deposit_bonus,
             d.welcome_bonus,d.total_credit,d.confirmed_at,d.created_at,d.updated_at
      FROM player_deposits d
      LEFT JOIN users u ON u.id=d.user_id
      ORDER BY d.id DESC LIMIT 100
    ")->fetchAll(PDO::FETCH_ASSOC);

    $recentWithdrawals=$db->query("
      SELECT w.id,w.user_id,u.public_id,w.method,w.amount,w.destination,w.status,
             w.provider_txn_id,w.admin_note,w.created_at,w.reviewed_at,w.processed_at
      FROM withdrawals w
      LEFT JOIN users u ON u.id=w.user_id
      ORDER BY w.id DESC LIMIT 100
    ")->fetchAll(PDO::FETCH_ASSOC);

    $q=$db->query("
      SELECT COUNT(*) c,COALESCE(SUM(amount),0) a
      FROM player_deposits WHERE status='pending'
    "); $depPending=$q->fetch(PDO::FETCH_ASSOC);

    $q=$db->query("
      SELECT COUNT(*) c,COALESCE(SUM(amount),0) a
      FROM withdrawals WHERE status IN ('requested','review','approved','processing')
    "); $wdPending=$q->fetch(PDO::FETCH_ASSOC);

    $q=$db->query("
      SELECT COUNT(*) c,COALESCE(SUM(amount),0) a
      FROM player_deposits
      WHERE status='confirmed' AND confirmed_at>=CURDATE()
    "); $depToday=$q->fetch(PDO::FETCH_ASSOC);

    $q=$db->query("
      SELECT COUNT(*) c,COALESCE(SUM(amount),0) a
      FROM withdrawals
      WHERE status='paid' AND processed_at>=CURDATE()
    "); $wdToday=$q->fetch(PDO::FETCH_ASSOC);

    json_out([
      'ok'=>true,
      'summary'=>[
        'pending_deposits_count'=>(int)$depPending['c'],
        'pending_deposits_amount'=>(float)$depPending['a'],
        'pending_withdrawals_count'=>(int)$wdPending['c'],
        'pending_withdrawals_amount'=>(float)$wdPending['a'],
        'confirmed_deposits_today_count'=>(int)$depToday['c'],
        'confirmed_deposits_today_amount'=>(float)$depToday['a'],
        'paid_withdrawals_today_count'=>(int)$wdToday['c'],
        'paid_withdrawals_today_amount'=>(float)$wdToday['a']
      ],
      'pending_deposits'=>$pendingDeposits,
      'pending_withdrawals'=>$pendingWithdrawals,
      'recent_deposits'=>$recentDeposits,
      'recent_withdrawals'=>$recentWithdrawals
    ]);
}catch(Throwable $e){
    json_out(['ok'=>false,'error'=>$e->getMessage()],403);
}
