import { Body, Controller, Delete, Get, Post, Req, Res } from "@nestjs/common";
import UserProvider from "./user.provider";
import express from "express";
import { UserDtoCreate, UserDtoLogIn } from "../../dto/user.dto";
import { ApiBody, ApiResponse } from "@nestjs/swagger";
import ResponseDto from "../../dto/response.dto";

@Controller("/api/user")
export default class UserController {

    constructor(private readonly userProvider: UserProvider){}

    @ApiBody({type: UserDtoLogIn})
    @ApiResponse({type: ResponseDto})
    @Post("/logIn")
    async logIn(@Req() request: express.Request, @Res({passthrough: true}) response: express.Response, @Body() payload: UserDtoLogIn){
        await this.userProvider.logIn(request, response, payload);
    }
    
    @Delete("/logOut")
    async logOut(@Res({passthrough: true}) response: express.Response){
        await this.userProvider.logOut(response);
    }

    @ApiResponse({type: ResponseDto})
    @ApiBody({type: UserDtoCreate})
    @Post("/create")
    async create(@Req() request: express.Request, @Body() payload: UserDtoCreate): Promise<ResponseDto>{
        return await this.userProvider.create(request, payload);
    }

    @Get("/isSessionActive")
    async isSessionActive(@Req() request: express.Request){
        return await this.userProvider.isSessionActive(request);
    }

}
