import { asyncHandler } from "@helper/async-handler.helper";
import { ClothesService } from "./clothes.service";
import { Request, Response, NextFunction } from "express";

export class ClothesController {
    constructor(
        private readonly clothesService: ClothesService
    ) { }

    create = asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
        const files = req.files as Express.Multer.File[];
        const userId = (req as any).user.id;
        const response = await this.clothesService.create(req.body, userId, files);
        res.status(response.statusCode).json(response);
    })

    findAll = asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
        const page = parseInt(req.query.page as string) || 1;
        const limit = parseInt(req.query.limit as string) || 10;
        const userId = req.query.userId ? parseInt(req.query.userId as string) : undefined;
        const response = await this.clothesService.findAll(page, limit, userId);
        res.status(response.statusCode).json(response);
    })

    findOne = asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
        const id = parseInt(req.params.id);
        const response = await this.clothesService.findOne(id);
        res.status(response.statusCode).json(response);
    })

    update = asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
        const id = parseInt(req.params.id);
        const files = req.files as Express.Multer.File[];
        const response = await this.clothesService.update(id, req.body, files);
        res.status(response.statusCode).json(response);
    })

    delete = asyncHandler(async (req: Request, res: Response, next: NextFunction) => {
        const id = parseInt(req.params.id);
        const response = await this.clothesService.delete(id);
        res.status(response.statusCode).json(response);
    })
}
