<?php

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

use App\Models\JackpotPool;
use App\Support\ApiResponse;
use App\Models\JackpotPoolAdjustment;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use App\Support\PaginationTrait;
use Illuminate\Http\Request;
use App\Http\Controllers\Api\V1\Admin\Jackpot\Concerns\PresentsJackpotPoolAdjustment;

/**
 * GET /api/v1/admin/jackpot/pools/{pool}/adjustments — 奖池余额调整流水。
 */
final class AdminJackpotPoolAdjustmentIndexController extends Controller
{
    use PaginationTrait;
    use PresentsJackpotPoolAdjustment;

    public function __invoke(Request $request, JackpotPool $pool): JsonResponse
    {
        $perPage = $this->perPage($request, 'per_page', 10, 100);
        $page = $this->page($request);

        $paginator = JackpotPoolAdjustment::query()
            ->where('jackpot_pool_id', $pool->id)
            ->with('adminUser:id,username,name')
            ->orderByDesc('id')
            ->paginate($perPage, ['*'], 'page', $page);

        return ApiResponse::success([
            'items' => $paginator->getCollection()
                ->map(fn (JackpotPoolAdjustment $row) => $this->adjustmentRow($row))
                ->values()
                ->all(),
            'total' => $paginator->total(),
            'page' => $paginator->currentPage(),
            'per_page' => $paginator->perPage(),
        ]);
    }
}
