import { UserType } from '@gadmin2/nest-common';
import { Controller, Get, Req, Res } from '@nestjs/common';
import { ApiOkResponse, ApiProperty } from '@nestjs/swagger';
import { Request } from 'express';
import { AppService } from './app.service';
import { AllowUnauthorizedRequest } from './lib/auth.guard';

class UserInfo {
  @ApiProperty()
  userid: string;

  @ApiProperty()
  username: string;
}
@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get()
  index(@Res() res) {
    res.status(302).redirect('/dashboard');
  }

  @Get('/userinfo')
  @ApiOkResponse({ type: UserInfo })
  userInfo(@Req() request: Request & { user: UserType }) {
    return request?.user || { userid: '', username: '' };
  }

  @Get('/hello')
  hello(): string {
    return this.appService.getHello();
  }

  @Get('/login-callback')
  @AllowUnauthorizedRequest()
  loginCallback(@Req() request: Request, @Res() res) {
    return this.appService.loginCallback(request, res);
  }

  @Get('login')
  @AllowUnauthorizedRequest()
  login(@Req() request: Request, @Res() res) {
    return this.appService.login(request, res);
  }

  @Get('logout')
  @AllowUnauthorizedRequest()
  logout(@Req() request: Request, @Res() res) {
    return this.appService.logout(request, res);
  }
}
