<?php

namespace App\Domains\Admin\Controllers;

use App\Domains\Admin\Actions\UpdateUser\UpdateUserAction;
use App\Domains\Admin\Actions\UpdateUser\UpdateUserRequest;
use App\Scopes\CommonController;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class UserUpdateController extends CommonController
{
    public function __invoke(int $id, Request $request): JsonResponse
    {
        $user = $this->adminUsers->findById($id);

        $action = new UpdateUserAction(new UpdateUserRequest(
            user: $user,
            data: $request->all(),
            isOwnUser: auth()->id() === $user->id,
            isLvTenant: $this->tenant->slug === 'lv',
        ));

        $response = $action->execute();

        return response()->json([
            'status' => $response->success ? 'success' : 'error',
            'message' => $response->message,
        ]);
    }
}
