import { Body, Controller, Get, Post, Query, Req } from '@nestjs/common';
import { Request } from 'express';
import { LoginDto } from './dto/login.dto';
import { LoginResponseDto } from './dto/login-response.dto';
import { AuthService } from './auth.service';
import { SignupDto } from './dto/signup.dto';
import { SignupResponseDto } from './dto/signup-response.dto';
import { SendFindIdCodeDto } from './dto/send-find-id-code.dto';
import { VerifyFindIdCodeDto } from './dto/verify-find-id-code.dto';
import { FindIdResponseDto } from './dto/find-id-response.dto';
import { SendResetPasswordCodeDto } from './dto/send-reset-password-code.dto';
import { VerifyResetPasswordCodeDto } from './dto/verify-reset-password-code.dto';
import { ResetPasswordDto } from './dto/reset-password.dto';
import { ResetPasswordResponseDto } from './dto/reset-password-response.dto';
import { SendSignupCodeDto } from './dto/send-signup-code.dto';
import { VerifySignupCodeDto } from './dto/verify-signup-code.dto';
import { Public } from '../common/decorators/public.decorator';

/**
 * 인증 관련 컨트롤러
 *
 * @description
 * 로그인, 회원가입, 아이디 찾기, 비밀번호 재설정 등 인증 관련 API
 *
 * @export
 * @class AuthController
 * @typedef {AuthController}
 */
@Controller('auth')
@Public() // 모든 엔드포인트가 공개 (인증 불필요)
export class AuthController {
	constructor(private readonly authService: AuthService) {}

	/**
	 * POST /auth/login - 사용자 로그인 처리
	 *
	 * @async
	 * @param {LoginDto} loginDto
	 * @returns {Promise<LoginResponseDto>}
	 */
	@Post('login')
	async login(@Body() loginDto: LoginDto): Promise<LoginResponseDto> {
		return this.authService.login(loginDto);
	}

	/**
	 * GET /auth/check-email - 이메일 중복 체크
	 *
	 * @async
	 * @param {string} email
	 * @returns {Promise<{ available: boolean; message: string }>}
	 */
	@Get('check-email')
	async checkEmail(
		@Query('email') email: string,
	): Promise<{ available: boolean; message: string }> {
		return this.authService.checkEmailDuplicate(email);
	}

	/**
	 * POST /auth/signup/send-code - 회원가입 이메일 인증번호 발송
	 *
	 * @async
	 * @param {SendSignupCodeDto} dto
	 * @returns {Promise<{ success: boolean; message: string; expiresIn: number }>}
	 */
	@Post('signup/send-code')
	async sendSignupCode(
		@Body() dto: SendSignupCodeDto,
	): Promise<{
		success: boolean;
		message: string;
		expiresIn: number;
	}> {
		return this.authService.sendSignupCode(dto);
	}

	/**
	 * POST /auth/signup/verify-code - 회원가입 이메일 인증번호 검증
	 *
	 * @async
	 * @param {VerifySignupCodeDto} dto
	 * @returns {Promise<{ success: boolean; message: string; verified: boolean }>}
	 */
	@Post('signup/verify-code')
	async verifySignupCode(
		@Body() dto: VerifySignupCodeDto,
	): Promise<{
		success: boolean;
		message: string;
		verified: boolean;
	}> {
		return this.authService.verifySignupCode(dto);
	}

	/**
	 * POST /auth/register - 회원가입
	 *
	 * @description
	 * 이메일 인증이 완료된 후에만 회원가입이 가능합니다.
	 * 먼저 /auth/signup/send-code로 인증번호를 받고,
	 * /auth/signup/verify-code로 인증을 완료한 후 호출하세요.
	 *
	 * @async
	 * @param {SignupDto} signupDto
	 * @param {Request} req
	 * @returns {Promise<SignupResponseDto>}
	 */
	@Post('register')
	async register(
		@Body() signupDto: SignupDto,
		@Req() req: Request,
	): Promise<SignupResponseDto> {
		const clientIp = req.ip || req.socket.remoteAddress || 'unknown';
		return this.authService.register(signupDto, clientIp);
	}

	/**
	 * POST /auth/find-id/send-code - 아이디 찾기 인증번호 발송
	 *
	 * @async
	 * @param {SendFindIdCodeDto} dto
	 * @returns {Promise<{ success: boolean; message: string; expiresIn: number }>}
	 */
	@Post('find-id/send-code')
	async sendFindIdCode(
		@Body() dto: SendFindIdCodeDto,
	): Promise<{
		success: boolean;
		message: string;
		expiresIn: number;
	}> {
		return this.authService.sendFindIdCode(dto);
	}

	/**
	 * POST /auth/find-id/verify-code - 아이디 찾기 인증번호 검증
	 *
	 * @async
	 * @param {VerifyFindIdCodeDto} dto
	 * @returns {Promise<FindIdResponseDto>}
	 */
	@Post('find-id/verify-code')
	async verifyFindIdCode(
		@Body() dto: VerifyFindIdCodeDto,
	): Promise<FindIdResponseDto> {
		return this.authService.verifyFindIdCode(dto);
	}

	/**
	 * POST /auth/reset-password/send-code - 비밀번호 재설정 인증번호 발송
	 *
	 * @async
	 * @param {SendResetPasswordCodeDto} dto
	 * @returns {Promise<{ success: boolean; message: string; expiresIn: number }>}
	 */
	@Post('reset-password/send-code')
	async sendResetPasswordCode(
		@Body() dto: SendResetPasswordCodeDto,
	): Promise<{
		success: boolean;
		message: string;
		expiresIn: number;
	}> {
		return this.authService.sendResetPasswordCode(dto);
	}

	/**
	 * POST /auth/reset-password/verify-code - 비밀번호 재설정 인증번호 검증
	 *
	 * @async
	 * @param {VerifyResetPasswordCodeDto} dto
	 * @returns {Promise<{ success: boolean; message: string; resetToken: string }>}
	 */
	@Post('reset-password/verify-code')
	async verifyResetPasswordCode(
		@Body() dto: VerifyResetPasswordCodeDto,
	): Promise<{ success: boolean; message: string; resetToken: string }> {
		return this.authService.verifyResetPasswordCode(dto);
	}

	/**
	 * POST /auth/reset-password - 비밀번호 재설정 실행
	 *
	 * @async
	 * @param {ResetPasswordDto} dto
	 * @returns {Promise<ResetPasswordResponseDto>}
	 */
	@Post('reset-password')
	async resetPassword(
		@Body() dto: ResetPasswordDto,
	): Promise<ResetPasswordResponseDto> {
		return this.authService.resetPassword(dto);
	}
}
