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

const { RulesResponseCodes } = responseCodes;

export const getRules = catchAsync(async (req: Request, res: Response) => {
  const { companyId, key } = pick(req.query, ['companyId', 'key']);
  const rules = key ? await rulesService.getRules(companyId, key) : await rulesService.getRules(companyId);
  res.success(rules, RulesResponseCodes.SUCCESS, 'Rules fetched successfully');
});

export const updateRules = catchAsync(async (req: Request, res: Response) => {
  const { companyId } = pick(req.params, ['companyId']);
  await rulesService.updateRules(companyId, req.body);
  res.success(true, RulesResponseCodes.SUCCESS, 'Rules updated successfully');
});
