import { Request, Response } from 'express';

import * as projectChargeService from '@/modules/projectCharge/projectCharge.service';
import catchAsync from '@/shared/utils/catchAsync';
import responseCodes from '@/shared/utils/responseCode/responseCode';
import { pick } from '@/shared/utils';

const { ProjectChargeResponseCodes } = responseCodes;

export const createProjectCharge = catchAsync(async (req: Request, res: Response) => {
  const projectCharge = await projectChargeService.createProjectCharge(req.body);
  res.success(projectCharge, ProjectChargeResponseCodes.SUCCESS, 'ProjectCharge Created Successfully');
});

export const updateProjectCharge = catchAsync(async (req: Request, res: Response) => {
  const { id } = pick(req.params, ['id']);

  const updatedProjectCharge = await projectChargeService.updateProjectCharge(id, req.body);
  res.success(
    updatedProjectCharge,
    ProjectChargeResponseCodes.SUCCESS,
    'ProjectCharge Updated Successfully',
  );
});

export const deleteProjectChargeById = catchAsync(
  async (req: Request, res: Response) => {
    const { id } = pick(req.params, ['id']);

    const projectCharge = await projectChargeService.deleteProjectCharge(id);
    res.success(projectCharge, ProjectChargeResponseCodes.SUCCESS, 'ProjectCharge Deleted Successfully');
  },
);

export const getProjectChargeById = catchAsync(async (req: Request, res: Response) => {
  const { id } = pick(req.params, ['id']);

  const projectCharges = await projectChargeService.getProjectChargeById(id);

  res.success(
    projectCharges,
    ProjectChargeResponseCodes.SUCCESS,
    'ProjectCharge Fetched Successfully',
  );
});

export const getProjectCharges = catchAsync(async (req: Request, res: Response) => {
  const filter = pick(req.query, ['companyId', 'search', 'project']);
  const options = pick(req.query, ['sortBy', 'limit', 'page', 'populate', 'includeTimeStamps']);

  const projectCharges = await projectChargeService.queryProjectCharges(filter, options);

  res.success(
    projectCharges,
    ProjectChargeResponseCodes.SUCCESS,
    'ProjectCharge Fetched Successfully',
  );
});
