import { Body, Controller, Get, Post, Req, Res } from '@nestjs/common';
import { Request, Response, Request as RQ } from 'express';
import Auth from '@/entities/core/auth';
import { AuthService } from '../services/auth.service';
import { LoginDto } from '../dto/login.dto';
import { ChangePasswordDto } from '../dto/change-password.dto';

export class AuthenticationController<T extends Auth> {
  constructor(protected readonly service: AuthService<T>) {}

  @Post('login')
  async login(
    @Body() loginDto: LoginDto,
    @Res({ passthrough: true }) response: Response,
  ) {
    return this.service
      .validate(loginDto.input, loginDto.password)
      .then((admin) => this.service.login(admin, response));
  }

  @Post('logout')
  async logout(
    @Req() request: Request,
    @Res({ passthrough: true }) response: Response,
  ) {
    return await this.service.logout(request, response);
  }

  @Post('change-password')
  async changePassword(
    @Res({ passthrough: true }) response: Response,
    @Req() request: Request,
    @Body() data: ChangePasswordDto,
  ) {
    return await this.service.changePassword(data, request, response);
  }

  @Get('me')
  async me(@Req() request: Request) {
    return this.service.me(request);
  }
}
