import { CompositeSpecification, UniqueID } from "@repo/rdx-ddd";
import { CustomerApplicationService } from "../../application";
import { logger } from "../../helpers";

export class CustomerNotExistsInCompanySpecification extends CompositeSpecification<UniqueID> {
  constructor(
    private readonly service: CustomerApplicationService,
    private readonly companyId: UniqueID,
    private readonly transaction?: unknown
  ) {
    super();
  }

  public async isSatisfiedBy(customerId: UniqueID): Promise<boolean> {
    const existsCheck = await this.service.existsByIdInCompany(
      this.companyId,
      customerId,
      this.transaction
    );

    if (existsCheck.isFailure) {
      throw existsCheck.error;
    }

    const customerExists = existsCheck.data;
    logger.debug(
      `customerExists => ${customerExists}, ${JSON.stringify({ customerId, companyId: this.companyId }, null, 2)}`,
      {
        label: "CustomerNotExistsInCompanySpecification",
      }
    );

    return customerExists === false;
  }
}
