<?php

namespace App\Modules\Auth\Controllers;

use App\Http\Controllers\Controller;
use App\Modules\Auth\Requests\RegisterRequest;
use App\Modules\Auth\DTOs\RegisterDTO;
use App\Modules\Auth\Services\AuthService;
use App\Modules\Auth\Requests\VerifyOtpRequest;
use App\Modules\Auth\DTOs\VerifyOtpDTO;

use App\Modules\Auth\Requests\ResendOtpRequest;
use App\Modules\Auth\DTOs\ResendOtpDTO;

use App\Modules\Auth\Requests\LoginRequest;
use App\Modules\Auth\DTOs\LoginDTO;

use App\Modules\Auth\Requests\ForgotPasswordRequest;
use App\Modules\Auth\DTOs\ForgotPasswordDTO;

use App\Modules\Auth\Requests\ResetPasswordRequest;
use App\Modules\Auth\DTOs\ResetPasswordDTO;

use App\Modules\Auth\Requests\UpdateProfileRequest;
use App\Modules\Auth\DTOs\UpdateProfileDTO;


use App\Modules\Auth\Requests\SocialLoginRequest;
use App\Modules\Auth\DTOs\SocialLoginDTO;


class AuthController extends Controller
{
    public function __construct(
        protected AuthService $service
    ) {}

    public function register(RegisterRequest $request)
    {
        $dto = new RegisterDTO(...$request->validated());
        return $this->service->register($dto);
    }


    public function verifyOtp(VerifyOtpRequest $request)
    {
        $dto = new VerifyOtpDTO(...$request->validated());
        return $this->service->verifyOtp($dto);
    }

    public function resendOtp(ResendOtpRequest $request)
    {
        $dto = new ResendOtpDTO(...$request->validated());
        return $this->service->resendOtp($dto);
    }

    public function login(LoginRequest $request)
    {
        $dto = new LoginDTO(
            $request->login,
            $request->password
        );

        return $this->service->login($dto);
    }

    public function forgotPassword(ForgotPasswordRequest $request)
    {
        $dto = new ForgotPasswordDTO(...$request->validated());
        return $this->service->forgotPassword($dto);
    }

    

    public function resetPassword(ResetPasswordRequest $request)
    {
        $dto = new ResetPasswordDTO(...$request->validated());

        return $this->service->resetPassword($dto);
    }

    public function profile()
    {
        return $this->service->profile(auth()->user());
    }

    public function update(UpdateProfileRequest $request)
    {
        $validated = $request->validated();

        $dto = new UpdateProfileDTO(
            $validated['name'] ?? null,
            $validated['phone_number'] ?? null,
            $validated['is_traveler'] ?? null,
            $validated['address'] ?? null,
            $validated['current_password'] ?? null,
            $validated['new_password'] ?? null,
            
        );


        return $this->service->updateProfile(auth()->user(), $dto, $request);
    }

    public function logout()
    {
        return $this->service->logout(auth()->user());
    }

    

    public function socialLogin(SocialLoginRequest $request)
    {
        $dto = new SocialLoginDTO(...$request->validated());

        $result = $this->service->socialLogin($dto);
        return $result;
    }
    
}

