<?php

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

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

/**
 * PUT /api/v1/admin/jackpot/pools/{pool} — 更新奖池运营参数（蓄水比例、阈值等）。
 */
final class AdminJackpotPoolUpdateController extends Controller
{
    public function __invoke(Request $request, JackpotPool $pool): JsonResponse
    {
        $data = $request->validate([
            'current_amount' => 'prohibited',
            'contribution_rate' => 'sometimes|numeric|min:0|max:1',
            'trigger_threshold' => 'sometimes|integer|min:0',
            'payout_rate' => 'sometimes|numeric|min:0|max:1',
            'force_trigger_draw_gap' => 'sometimes|integer|min:0',
            'min_bet_amount' => 'sometimes|integer|min:0',
            'combo_trigger_play_codes' => 'sometimes|array',
            'combo_trigger_play_codes.*' => 'string|max:32',
            'status' => 'sometimes|integer|in:0,1',
        ]);

        $pool->fill($data);
        $pool->save();

        return ApiResponse::success([
            'id' => (int) $pool->id,
            'currency_code' => $pool->currency_code,
            'current_amount' => (int) $pool->current_amount,
            'contribution_rate' => (string) $pool->contribution_rate,
            'trigger_threshold' => (int) $pool->trigger_threshold,
            'payout_rate' => (string) $pool->payout_rate,
            'force_trigger_draw_gap' => (int) $pool->force_trigger_draw_gap,
            'min_bet_amount' => (int) $pool->min_bet_amount,
            'combo_trigger_play_codes' => is_array($pool->combo_trigger_play_codes) ? $pool->combo_trigger_play_codes : [],
            'status' => (int) $pool->status,
            'last_trigger_draw_id' => $pool->last_trigger_draw_id !== null ? (int) $pool->last_trigger_draw_id : null,
            'updated_at' => $pool->updated_at?->toIso8601String(),
        ]);
    }
}
