// Create bankAccount service functions
import { Request, Response } from 'express';
import * as bankAccountService from '@/modules/bankAccount/bankAccount.service';
import catchAsync from '@/shared/utils/catchAsync';
import responseCodes from '@/shared/utils/responseCode/responseCode';
import { pick } from '@/shared/utils';

const { BankAccountResponseCodes } = responseCodes;

export const createBankAccount = catchAsync(
  async (req: Request, res: Response) => {
    const { company } = req.user;
    const companyId = company?.id;

    const bankAccount = await bankAccountService.createBankAccount({
      ...req.body,
      company: companyId,
    });
    res.success(
      bankAccount,
      BankAccountResponseCodes.SUCCESS,
      'BankAccount Created Successfully',
    );
  },
);

export const updateBankAccount = catchAsync(
  async (req: Request, res: Response) => {
    const { id } = pick(req.params, ['id']);
    const updatedBankAccount = await bankAccountService.updateBankAccount(
      id,
      req.body,
    );
    res.success(
      updatedBankAccount,
      BankAccountResponseCodes.SUCCESS,
      'BankAccount Updated Successfully',
    );
  },
);

export const getBankAccountById = catchAsync(
  async (req: Request, res: Response) => {
    const { id } = pick(req.params, ['id']);
    const bankAccount = await bankAccountService.getBankAccountById(id);
    res.success(
      bankAccount,
      BankAccountResponseCodes.SUCCESS,
      'BankAccount Fetched Successfully',
    );
  },
);

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

    req.user.company &&
      filter.company === undefined &&
      (filter.company = req.user.company.id);

    const bankAccount = await bankAccountService.queryBankAccount(
      filter,
      options,
    );
    res.success(
      bankAccount,
      BankAccountResponseCodes.SUCCESS,
      'BankAccount Fetched Successfully',
    );
  },
);

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

    const company = await bankAccountService.deleteBankAccount(id);
    res.success(
      company,
      BankAccountResponseCodes.SUCCESS,
      'Company Deleted Successfully',
    );
  },
);
