<?php

namespace App\Http\Controllers\Api\V1\Admin\Jackpot;

use Illuminate\Http\Request;
use App\Support\AdminApiList;
use App\Models\JackpotPayoutLog;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;

/**
 * GET /api/v1/admin/jackpot/payout-logs — Jackpot 派彩（爆池）记录。
 */
final class AdminJackpotPayoutLogIndexController extends Controller
{
    public function __invoke(Request $request): JsonResponse
    {
        $p = AdminApiList::readPaging($request);
        $drawNo = trim((string) $request->query('draw_no', ''));

        $q = JackpotPayoutLog::query()
            ->with(['draw:id,draw_no', 'pool:id,currency_code'])
            ->orderByDesc('id');

        if ($drawNo !== '') {
            $q->whereHas('draw', fn ($d) => $d->where('draw_no', 'like', '%'.$drawNo.'%'));
        }

        $paginator = $q->paginate($p['perPage'], ['*'], 'page', $p['page']);

        return AdminApiList::json($paginator, fn (JackpotPayoutLog $r) => [
            'id' => (int) $r->id,
            'draw_id' => (int) $r->draw_id,
            'draw_no' => $r->draw?->draw_no,
            'jackpot_pool_id' => (int) $r->jackpot_pool_id,
            'currency_code' => $r->pool?->currency_code,
            'trigger_type' => $r->trigger_type,
            'total_payout_amount' => (int) $r->total_payout_amount,
            'winner_count' => (int) $r->winner_count,
            'trigger_snapshot_json' => $r->trigger_snapshot_json,
            'created_at' => $r->created_at?->toIso8601String(),
        ]);
    }
}
