import {
  ISequelizeAdapter,
  SequelizeRepository,
} from "@/contexts/common/infrastructure/sequelize";
import { UniqueID } from "@shared/contexts";
import { Transaction } from "sequelize";
import { InvoiceParticipantAddress } from "../domain";
import { IInvoiceParticipantAddressMapper } from "./mappers";

export class InvoiceParticipantAddressRepository extends SequelizeRepository<InvoiceParticipantAddress> {
  protected mapper: IInvoiceParticipantAddressMapper;

  public constructor(props: {
    mapper: IInvoiceParticipantAddressMapper;
    adapter: ISequelizeAdapter;
    transaction: Transaction;
  }) {
    const { adapter, mapper, transaction } = props;
    super({ adapter, transaction });
    this.mapper = mapper;
  }

  public async getById(
    id: UniqueID,
  ): Promise<InvoiceParticipantAddress | null> {
    const rawParticipant: any = await this._getById(
      "InvoiceParticipantAddress_Model",
      id,
      {
        include: [{ all: true }],
      },
    );

    if (!rawParticipant === true) {
      return null;
    }

    return this.mapper.mapToDomain(rawParticipant);
  }

  public async exists(id: UniqueID): Promise<boolean> {
    return this._exists("CustomerAddress", "id", id.toString());
  }
}
