import { Body, Controller, Get, Patch, Post, UseGuards } from '@nestjs/common';
import {
  CurrentUser,
  Public,
  SignInService,
} from '@nestjs/authentication';
import type { AppUser } from './auth.types.js';
import { AdminGuard } from './admin.guard.js';
import { AuthService } from './auth.service.js';

@Controller()
export class AuthController {
  constructor(
    private readonly authService: AuthService,
    private readonly signInService: SignInService,
  ) {}

  @Public()
  @Post('auth/sign-in')
  signIn(@Body() body: unknown) {
    return this.authService.signIn(body, 'ADMIN');
  }

  @UseGuards(AdminGuard)
  @Get('auth/me')
  currentUser(@CurrentUser() user: AppUser) {
    return { user };
  }

  @Post('auth/sign-out')
  async signOut() {
    await this.signInService.signOut();
    return { success: true };
  }

  @Public()
  @Post('account/register')
  register(@Body() body: unknown) {
    return this.authService.register(body);
  }

  @Public()
  @Post('account/sign-in')
  customerSignIn(@Body() body: unknown) {
    return this.authService.signIn(body, 'CUSTOMER');
  }

  @Get('account/me')
  account(@CurrentUser() user: AppUser) {
    return { user };
  }

  @Patch('account/me')
  updateAccount(@CurrentUser() user: AppUser, @Body() body: unknown) {
    return this.authService.updateProfile(user, body);
  }
}
