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

const { InvoiceSettingsResponseCodes } = responseCodes;

export const getInvoiceSettings = catchAsync(
  async (req: Request, res: Response) => {
    const filter = pick(req.query, ['company']);
    const options = pick(req.query, [
      'sortBy',
      'limit',
      'page',
      'fields',
      'populate',
      'includeTimeStamps',
      'alias',
    ]);

    const settings = await invoiceSettingsService.getInvoiceSettings(
      filter,
      options,
    );
    res.success(
      settings,
      InvoiceSettingsResponseCodes.SUCCESS,
      'Invoice settings fetched successfully',
    );
  },
);

export const upsertInvoiceSettings = catchAsync(
  async (req: Request, res: Response) => {
    const invoiceSettings = await invoiceSettingsService.upsertInvoiceSettings({
      ...req.body,
    });

    res.success(
      invoiceSettings,
      InvoiceSettingsResponseCodes.SUCCESS,
      'Invoice settings saved successfully',
    );
  },
);
