<?php

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

use App\Models\Draw;
use App\Lottery\ErrorCode;
use App\Support\ApiMessage;
use App\Support\ApiResponse;
use Illuminate\Http\Request;
use App\Models\DrawResultBatch;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use App\Services\Draw\DrawPendingResultBatchDiscardService;

/**
 * DELETE /api/v1/admin/draws/{draw}/result-batches/{batch} — 删除待审核开奖批次。
 */
final class DrawResultBatchDestroyController extends Controller
{
    public function __construct(
        private readonly DrawPendingResultBatchDiscardService $discardService,
    ) {}

    public function __invoke(Request $request, Draw $draw, DrawResultBatch $batch): JsonResponse
    {
        if ((int) $batch->draw_id !== (int) $draw->id) {
            return ApiResponse::error(
                trans('api.not_found', [], $request->lotteryLocale()),
                ErrorCode::NotFound->value,
                null,
                404,
            );
        }

        try {
            $draw = $this->discardService->discard($draw, $batch);
        } catch (\RuntimeException $e) {
            return ApiMessage::runtimeErrorResponse($request, $e);
        }

        return ApiResponse::success([
            'draw_no' => $draw->draw_no,
            'status' => $draw->status,
            'deleted_batch_id' => (int) $batch->id,
        ]);
    }
}
