import { Request, Response } from 'express';

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

const { PaymentPlanResponseCodes } = responseCodes;

export const createPaymentPlan = catchAsync(
  async (req: Request, res: Response) => {
    const paymentPlan = await paymentPlanService.createPaymentPlan(req.body);
    res.success(
      paymentPlan,
      PaymentPlanResponseCodes.SUCCESS,
      'PaymentPlan Created Successfully',
    );
  },
);

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

    const updatedPaymentPlan = await paymentPlanService.updatePaymentPlan(
      id,
      req.body,
    );
    res.success(
      updatedPaymentPlan,
      PaymentPlanResponseCodes.SUCCESS,
      'PaymentPlan Updated Successfully',
    );
  },
);

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

    const paymentPlan = await paymentPlanService.deletePaymentPlan(id);
    res.success(
      paymentPlan,
      PaymentPlanResponseCodes.SUCCESS,
      'PaymentPlan Deleted Successfully',
    );
  },
);

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

    const paymentPlans = await paymentPlanService.getPaymentPlanById(id);

    res.success(
      paymentPlans,
      PaymentPlanResponseCodes.SUCCESS,
      'PaymentPlan Fetched Successfully',
    );
  },
);

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

    const paymentPlans = await paymentPlanService.queryPaymentPlans(
      filter,
      options,
      company?.id,
    );

    res.success(
      paymentPlans,
      PaymentPlanResponseCodes.SUCCESS,
      'PaymentPlan Fetched Successfully',
    );
  },
);
