<?php

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

use App\Models\Player;
use App\Support\ApiResponse;
use App\Support\AdminSiteScope;
use App\Support\PlayerApiPresenter;
use App\Http\Controllers\Controller;
use App\Services\AuditLogger;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

/** POST /api/v1/admin/players/{player}/unfreeze */
final class AdminPlayerUnfreezeController extends Controller
{
    public function __invoke(Request $request, Player $player): JsonResponse
    {
        $admin = $request->lotteryAdmin();
        abort_if($admin === null, 401);

        if ($denied = AdminSiteScope::denyUnlessPlayerAccessible($admin, $player)) {
            return $denied;
        }

        $before = PlayerApiPresenter::listItem($player);

        $player->forceFill(['status' => 0])->save();

        AuditLogger::recordForAdmin(
            $request->user(),
            $request,
            'player_manage',
            'unfreeze',
            'player',
            (string) $player->id,
            $before,
            PlayerApiPresenter::listItem($player->fresh(['wallets'])),
        );

        return ApiResponse::success(PlayerApiPresenter::listItem($player->fresh(['wallets'])));
    }
}
