import { Body, Controller, Delete, Get, ParseIntPipe, Post, Query, Req } from "@nestjs/common";
import CommonProvider from "./common.provider";
import { CommonListDto } from "../../dto/common.dto";
import ResponseDto from "../../dto/response.dto";
import { ApiBody, ApiQuery, ApiResponse } from "@nestjs/swagger";
import express from "express";

@Controller("/api/v1/common")
export default class CommonController {

    constructor(private readonly commonProvider: CommonProvider){}

    @ApiBody({type: CommonListDto})
    @ApiResponse({type: ResponseDto})
    @Post("/list")
    async list(@Body() payload: CommonListDto, @Query("search") search?: string): Promise<ResponseDto>{
        return await this.commonProvider.list(payload, search);
    }

    @ApiResponse({type: ResponseDto})
    @ApiQuery({name: "entity"})
    @ApiQuery({name: "restorePassword", required: false})
    @Post("/store")
    async store(@Req() request: express.Request, @Body() payload: any, @Query("entity") entity: string, @Query("restorePassword") restorePassword?: boolean): Promise<ResponseDto>{
        return await this.commonProvider.store(request, payload, entity, restorePassword);
    }

    @ApiResponse({type: ResponseDto})
    @ApiQuery({name: "id"})
    @ApiQuery({name: "entity"})
    @Delete("/remove")
    async remove(@Req() request: express.Request, @Query("id", ParseIntPipe) id: number, @Query("entity") entity: string): Promise<ResponseDto>{
        return await this.commonProvider.remove(request, id, entity);
    }

    @ApiResponse({type: ResponseDto})
    @ApiQuery({name: "id"})
    @ApiQuery({name: "entity"})
    @Get("/preventiveTrash")
    async preventiveTrash(@Req() request: express.Request, @Query("entity") entity: string, @Query("id", ParseIntPipe) id: number){
        return await this.commonProvider.preventiveTrash(request, entity, id);
    }

    @ApiResponse({type: ResponseDto})
    @ApiQuery({name: "id"})
    @ApiQuery({name: "entity"})
    @Get("/postSave")
    async postSave(@Req() request: express.Request, @Query("entity") entity: string, @Query("id", ParseIntPipe) id: number): Promise<ResponseDto>{
        return await this.commonProvider.postSave(request, entity, id);
    }

    @ApiResponse({type: ResponseDto})
    @ApiQuery({name: "entity"})
    @Post("/preSave")
    async preSave(@Req() request: express.Request, @Query("entity") entity: string, @Body() payload: any): Promise<ResponseDto>{
        return await this.commonProvider.preSave(request, entity, payload);
    }

}