<?php

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

use App\Models\JackpotPool;
use App\Support\ApiResponse;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;

/**
 * GET /api/v1/admin/jackpot/pools — Jackpot 奖池配置列表。
 */
final class AdminJackpotPoolIndexController extends Controller
{
    public function __invoke(): JsonResponse
    {
        $rows = JackpotPool::query()->orderBy('currency_code')->get();

        return ApiResponse::success([
            'items' => $rows->map(fn (JackpotPool $p) => $this->row($p))->values()->all(),
        ]);
    }

    /** @return array<string, mixed> */
    private function row(JackpotPool $p): array
    {
        return [
            'id' => (int) $p->id,
            'currency_code' => $p->currency_code,
            'current_amount' => (int) $p->current_amount,
            'contribution_rate' => (string) $p->contribution_rate,
            'trigger_threshold' => (int) $p->trigger_threshold,
            'payout_rate' => (string) $p->payout_rate,
            'force_trigger_draw_gap' => (int) $p->force_trigger_draw_gap,
            'min_bet_amount' => (int) $p->min_bet_amount,
            'combo_trigger_play_codes' => is_array($p->combo_trigger_play_codes) ? $p->combo_trigger_play_codes : [],
            'status' => (int) $p->status,
            'last_trigger_draw_id' => $p->last_trigger_draw_id !== null ? (int) $p->last_trigger_draw_id : null,
            'updated_at' => $p->updated_at?->toIso8601String(),
        ];
    }
}
