<?php

namespace Modules\User\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Services\Response\ResponseService;
use Auth;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

use Modules\User\Requests\Api\EditUserRequest;
use Modules\User\Resources\Api\UserResource;
use Modules\User\Services\UserService;
class UserController extends Controller
{

    public function __construct(
        private UserService $userService,
    ) {
    }

    /**
     * Get User.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function show(Request $request): JsonResponse
    {
        $id = auth()->id();
        $user = $this->userService->getItem($id);

        $response = [
            'status' => 200,
            'message' => '',
            'data' => new UserResource($user)
        ];
        return ResponseService::response($response);
    }
    /**
     * Edit User.
     *
     * @param EditUserRequest $request
     * @return JsonResponse
     */
    public function update(EditUserRequest $request): JsonResponse
    {
        $id = auth()->id();
        $credentials = $request->only('photo', 'name', 'last_name', 'email', 'password', 'password_confirmation');

        $user = $this->userService->update($credentials, $id);

        $response = [
            'status' => 200,
            'message' => '',
            'data' => new UserResource($user)
        ];
        return ResponseService::response($response);
    }
    /**
     * logout User.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function logout(Request $request): JsonResponse
    {
        $id = auth()->id();

        $this->userService->update(['api_token' => null], $id);
        Auth::logout();
        $response = [
            'status' => 401,
            'message' => __('users.logout'),
        ];
        return ResponseService::response($response);
    }
    /**
     * remove User.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function delete(Request $request): JsonResponse
    {
        $id = auth()->id();

        $this->userService->destroy($id);

        $response = [
            'status' => 401,
            'message' => __('users.account_deleted'),
        ];
        return ResponseService::response($response);
    }
}
