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

export class CustomerNotExistsInCompanySpecification extends CompositeSpecification<UniqueID> {
  constructor(
    private readonly service: CustomerService,
    private readonly companyId: UniqueID,
    private readonly transaction?: any
  ) {
    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;
  }
}
