import { Body, Controller, Get, ParseIntPipe, Post, Query, Req } from "@nestjs/common";
import { ArticleCreateDto } from "../../dto/article.dto";
import ResponseDto from "../../dto/response.dto";
import ArticleProvider from "./article.provider";
import { ApiBody, ApiQuery, ApiResponse } from "@nestjs/swagger";

@Controller("/api/v1/article")
export default class ArticleController {

    constructor(private readonly articleProvider: ArticleProvider){}

    @ApiBody({type: ArticleCreateDto, isArray: true})
    @ApiResponse({type: ResponseDto})
    @Post("/create")
    async create(@Req() request: Request, @Body() payload: ArticleCreateDto[]): Promise<ResponseDto>{
        return await this.articleProvider.create(request, payload);
    }

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

    @ApiQuery({name: "searchBy"})
    @ApiQuery({name: "text"})
    @ApiResponse({type: ResponseDto})
    @Get("/searchArticle")
    async searchArticle(@Req() request: Request, @Query("searchBy") searchBy: 'code' | 'title' | 'barcode', @Query("text") text: string): Promise<ResponseDto>{
        return await this.articleProvider.searchArticle(request, searchBy, text);
    }

    @ApiQuery({name: "stockFor"})
    @ApiQuery({name: "idArticle"})
    @Get("/getArticleStock")
    async getArticleStock(@Req() request: Request, @Query("stockFor") stockFor: 'article' | 'store' | 'storage', @Query("idArticle") idArticle: number): Promise<ResponseDto>{
        return await this.articleProvider.getArticleStock(request, stockFor, idArticle);
    }

}