<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\User\PasswordChangeRequest;
use App\Http\Requests\User\SwitchTenantRequest;
use App\Http\Requests\User\UserUpdateRequest;
use App\Services\MemberService;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function __construct(private MemberService $service) {}

    public function index(Request $request)
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->getMembers($request->all());
        }, __('message.user.fetched'));
    }

    public function show($userNo)
    {
        return ApiResponse::handle(function () use ($userNo) {
            return $this->service->getMember($userNo);
        }, __('message.user.fetched'));
    }

    public function me(Request $request)
    {
        return ApiResponse::handle(function () {
            $userId = (int) app('api_user');

            return MemberService::getUserInfoForLogin($userId);
        }, __('message.user.me_fetched'));
    }

    public function meUpdate(UserUpdateRequest $request)
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->getMyUpdate($request);
        }, __('message.user.me_updated'));
    }

    public function changePassword(PasswordChangeRequest $request)
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->setMyPassword($request);
        }, __('message.user.password_changed'));
    }

    public function tenants(Request $request)
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->getMyTenants($request);
        }, __('message.user.tenants_fetched'));
    }

    public function switchTenant(SwitchTenantRequest $request)
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->switchMyTenant($request->validated()['tenant_id']);
        }, __('message.user.tenant_switched'));
    }
}
