import { Request, Response } from 'express';
import catchAsync from '@/shared/utils/catchAsync';
import * as subCategoryService from './subCategory.service';
import responseCodes from '@/shared/utils/responseCode/responseCode';
import { pick } from '@/shared/utils';

const { SubCategoryResponseCodes } = responseCodes;

export const create = catchAsync(async (req: Request, res: Response) => {
  const result = await subCategoryService.create(req.body);
  res.success(result, SubCategoryResponseCodes.SUCCESS, 'SubCategory Created Successfully');
});

export const get = catchAsync(async (req: Request, res: Response) => {
  const result = await subCategoryService.getById(req.params.id);
  res.success(result, SubCategoryResponseCodes.SUCCESS, 'SubCategory Fetched Successfully');
});

export const update = catchAsync(async (req: Request, res: Response) => {
  await subCategoryService.update(req.params.id, req.body);
  res.success(true, SubCategoryResponseCodes.SUCCESS, 'SubCategory Updated Successfully');
});

export const remove = catchAsync(async (req: Request, res: Response) => {
  await subCategoryService.remove(req.params.id);
  res.success(true, SubCategoryResponseCodes.SUCCESS, 'SubCategory Deleted Successfully');
});

export const list = catchAsync(async (req: Request, res: Response) => {
  const filters = pick(req.query, ['search', 'categoryId']);

  const options = pick(req.query, [
    'sortBy',
    'limit',
    'page',
    'populate',
    'fields',
  ]);

  const result = await subCategoryService.querySubCategory(filters, options);
  res.success(result, SubCategoryResponseCodes.SUCCESS, 'SubCategory Fetched Successfully');
});
