import {
  Controller,
  Post,
  Body,
  Headers,
  HttpCode,
  HttpStatus,
} from '@nestjs/common';
import { AuthService } from '../../application/services/auth.service';
import {
  CreateAnonymousDto,
  SendCodeDto,
  VerifyCodeDto,
  RefreshTokenDto,
} from '../../application/dtos/auth.dto';

@Controller('auth')
export class AuthController {
  constructor(private readonly authService: AuthService) {}

  @Post('anonymous')
  @HttpCode(HttpStatus.OK)
  async createAnonymousSession(@Body() dto: CreateAnonymousDto) {
    const result = await this.authService.createAnonymousSession(dto.fingerprint || '');
    return {
      success: true,
      data: result,
    };
  }

  @Post('send-code')
  @HttpCode(HttpStatus.OK)
  async sendVerificationCode(@Body() dto: SendCodeDto) {
    const result = await this.authService.sendVerificationCode(dto.phone);
    return {
      success: true,
      data: result,
    };
  }

  @Post('verify-phone')
  @HttpCode(HttpStatus.OK)
  async verifyPhone(
    @Body() dto: VerifyCodeDto,
    @Headers('x-user-id') userId?: string,
  ) {
    const result = await this.authService.verifyAndLogin(
      dto.phone,
      dto.code,
      userId,
    );
    return {
      success: true,
      data: result,
    };
  }

  @Post('refresh')
  @HttpCode(HttpStatus.OK)
  async refreshToken(@Body() dto: RefreshTokenDto) {
    const result = await this.authService.refreshToken(dto.token);
    return {
      success: true,
      data: result,
    };
  }

  @Post('logout')
  @HttpCode(HttpStatus.OK)
  async logout() {
    return {
      success: true,
      message: 'Logged out successfully',
    };
  }
}
