<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\UserInvitation\AcceptInvitationRequest;
use App\Http\Requests\UserInvitation\InviteUserRequest;
use App\Http\Requests\UserInvitation\ListInvitationRequest;
use App\Services\UserInvitationService;
use Illuminate\Http\JsonResponse;

class UserInvitationController extends Controller
{
    public function __construct(
        private readonly UserInvitationService $service
    ) {}

    /**
     * 초대 목록 조회
     */
    public function index(ListInvitationRequest $request): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->service->index($request->validated()),
            __('message.fetched')
        );
    }

    /**
     * 사용자 초대 발송
     */
    public function invite(InviteUserRequest $request): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->service->invite($request->validated()),
            __('message.invitation.sent')
        );
    }

    /**
     * 초대 수락
     */
    public function accept(string $token, AcceptInvitationRequest $request): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->service->accept($token, $request->validated()),
            __('message.invitation.accepted')
        );
    }

    /**
     * 초대 취소
     */
    public function cancel(int $id): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->service->cancel($id),
            __('message.invitation.cancelled')
        );
    }

    /**
     * 초대 재발송
     */
    public function resend(int $id): JsonResponse
    {
        return ApiResponse::handle(
            fn () => $this->service->resend($id),
            __('message.invitation.resent')
        );
    }
}
