<?php

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

use App\Models\Player;
use App\Support\ApiResponse;
use App\Support\AdminSiteScope;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use App\Services\Player\PlayerPasswordService;
use App\Http\Requests\Admin\AdminPlayerPasswordResetRequest;

/** PUT /api/v1/admin/players/{player}/password */
final class AdminPlayerPasswordResetController extends Controller
{
    public function __invoke(
        AdminPlayerPasswordResetRequest $request,
        Player $player,
        PlayerPasswordService $passwords,
    ): JsonResponse {
        $admin = $request->lotteryAdmin();
        abort_if($admin === null, 401);

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

        $updated = $passwords->reset($player, (string) $request->validated('password'));

        return ApiResponse::success([
            'password_reset' => true,
            'player_id' => (int) $updated->id,
        ], request: $request);
    }
}
