import { IInvoicingContext } from "#/server/intrastructure";
import { InvoiceRepository } from "#/server/intrastructure/Invoice.repository";

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
  );
};
