<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\Authz\UserRoleGrantRequest;
use App\Services\Authz\UserRoleService;

class UserRoleController extends Controller
{
    public function index($id)
    {
        return ApiResponse::handle(function () use ($id) {
            return UserRoleService::list((int) $id);
        }, __('message.fetched'));
    }

    public function grant($id, UserRoleGrantRequest $request)
    {
        return ApiResponse::handle(function () use ($id, $request) {
            return UserRoleService::grant((int) $id, $request->validated());
        }, __('message.updated'));
    }

    public function revoke($id, UserRoleGrantRequest $request)
    {
        return ApiResponse::handle(function () use ($id, $request) {
            return UserRoleService::revoke((int) $id, $request->validated());
        }, __('message.updated'));
    }

    public function sync($id, UserRoleGrantRequest $request)
    {
        return ApiResponse::handle(function () use ($id, $request) {
            return UserRoleService::sync((int) $id, $request->validated());
        }, __('message.updated'));
    }
}
