import { UpdateInvoiceUseCase } from "@/contexts/invoicing/application";
import {
  ContactRepository,
  IInvoicingContext,
  InvoiceParticipantAddressRepository,
  InvoiceParticipantRepository,
} from "../../..";
import { InvoiceRepository } from "../../../Invoice.repository";
import {
  createContactMapper,
  createInvoiceMapper,
  createInvoiceParticipantAddressMapper,
  createInvoiceParticipantMapper,
} from "../../../mappers";
import { updateInvoicePresenter } from "./presenter";
import { UpdateInvoiceController } from "./update-invoice.controller";

export const updateInvoiceController = (context: IInvoicingContext) => {
  const adapter = context.adapter;
  const repoManager = context.repositoryManager;

  repoManager.registerRepository("Invoice", (params = { transaction: null }) => {
    const { transaction } = params;

    return new InvoiceRepository({
      transaction,
      adapter,
      mapper: createInvoiceMapper(context),
    });
  });

  repoManager.registerRepository("Participant", (params = { transaction: null }) => {
    const { transaction } = params;

    return new InvoiceParticipantRepository({
      transaction,
      adapter,
      mapper: createInvoiceParticipantMapper(context),
    });
  });

  repoManager.registerRepository("ParticipantAddress", (params = { transaction: null }) => {
    const { transaction } = params;

    return new InvoiceParticipantAddressRepository({
      transaction,
      adapter,
      mapper: createInvoiceParticipantAddressMapper(context),
    });
  });

  repoManager.registerRepository("Contact", (params = { transaction: null }) => {
    const { transaction } = params;

    return new ContactRepository({
      transaction,
      adapter,
      mapper: createContactMapper(context),
    });
  });

  const updateInvoiceUseCase = new UpdateInvoiceUseCase(context);

  return new UpdateInvoiceController(
    {
      useCase: updateInvoiceUseCase,
      presenter: updateInvoicePresenter,
    },
    context
  );
};
