import {
  generateCodeDate,
  generateRandom,
} from 'src/modules/transaction/vip-code/domain/usecases/managers/helpers/generate-random.helper';
import { ILike } from 'typeorm';

export async function generateInvoiceCodeHelper(dataService, code) {
  const month_year = generateCodeDate();
  const char = generateRandom(1);
  const number = generateRandom(1, true);
  let query: any = {
    invoice_code: ILike(`%${month_year}%`),
  };

  if (code == 'RF') {
    query = {
      code: ILike(`%${month_year}%`),
    };
  }

  if (code == 'PYM') {
    query = {
      payment_code: ILike(`%${month_year}%`),
    };
  }

  const invoice_code = await dataService.getManyByOptions({
    where: query,
  });
  const current_number = invoice_code.length + 1;

  return `${code}-${month_year}${char}${number}/${current_number
    .toString()
    .padStart(5, '0')}`;
}
