import httpStatus from 'http-status';
import { Request, Response } from 'express';

import catchAsync from '@/shared/utils/catchAsync';
import ApiError from '@/shared/utils/errors/ApiError';
import { getObjectId } from '@/shared/utils/commonHelper';
import { pick } from '@/shared/utils';
import * as propertySizeService from '@/modules/master/property/propertySize/propertySize.service';
import responseCodes from '@/shared/utils/responseCode/responseCode';

const { PropertySizeResponseCodes } = responseCodes;

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

export const get = catchAsync(async (req: Request, res: Response) => {
  const { id } = req.params;
  if (!getObjectId(id))
    throw new ApiError(httpStatus.BAD_REQUEST, 'Invalid ID');

  const result = await propertySizeService.getById(id);
  res.success(
    result,
    PropertySizeResponseCodes.SUCCESS,
    'PropertySize Fetched Successfully',
  );
});

export const update = catchAsync(async (req: Request, res: Response) => {
  const { id } = req.params;
  if (!getObjectId(id))
    throw new ApiError(httpStatus.BAD_REQUEST, 'Invalid ID');

  await propertySizeService.update(id, req.body);
  res.success(
    true,
    PropertySizeResponseCodes.SUCCESS,
    'PropertySize Update Successfully',
  );
});

export const remove = catchAsync(async (req: Request, res: Response) => {
  const { id } = req.params;
  if (!getObjectId(id))
    throw new ApiError(httpStatus.BAD_REQUEST, 'Invalid ID');

  await propertySizeService.remove(id);
  res.success(
    true,
    PropertySizeResponseCodes.SUCCESS,
    'PropertySize Delete Successfully',
  );
});

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

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

  const result = await propertySizeService.queryPropertySize(filters, options);
  res.success(
    result,
    PropertySizeResponseCodes.SUCCESS,
    'PropertySize Fetched Successfully',
  );
});
