import { Request, Response } from 'express';

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

const { QuotationResponseCodes } = responseCodes;

export const createQuotation = catchAsync(async (req: Request, res: Response) => {
  const quotation = await quotationService.createQuotation(req.body);
  res.success(quotation, QuotationResponseCodes.SUCCESS, 'Quotation Created Successfully');
});

export const updateQuotation = catchAsync(async (req: Request, res: Response) => {
  const { id } = pick(req.params, ['id']);
  const updatedQuotation = await quotationService.updateQuotation(id, req.body);
  res.success(
    updatedQuotation,
    QuotationResponseCodes.SUCCESS,
    'Quotation Updated Successfully',
  );
});

export const deleteQuotationById = catchAsync(async (req: Request, res: Response) => {
  const { id } = pick(req.params, ['id']);
  const quotation = await quotationService.deleteQuotation(id);
  res.success(quotation, QuotationResponseCodes.SUCCESS, 'Quotation Deleted Successfully');
});

export const getQuotationById = catchAsync(async (req: Request, res: Response) => {
  const { id } = pick(req.params, ['id']);
  const quotation = await quotationService.getQuotationById(id);
  res.success(quotation, QuotationResponseCodes.SUCCESS, 'Quotation Fetched Successfully');
});

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

  res.success(quotations, QuotationResponseCodes.SUCCESS, 'Quotation Fetched Successfully');
});
