import { DomainValidationError, ValueObject } from "@repo/rdx-ddd";
import { Result } from "@repo/rdx-utils";

interface ICustomerInvoiceStatusProps {
  value: string;
}

export enum INVOICE_STATUS {
  DRAFT = "draft", // <- Proforma
  SENT = "sent", // <- Proforma
  APPROVED = "approved", // <- Proforma
  REJECTED = "rejected", // <- Proforma
  ISSUED = "issued", // <- Factura y enviada a Veri*Factu
}
export class CustomerInvoiceStatus extends ValueObject<ICustomerInvoiceStatusProps> {
  private static readonly ALLOWED_STATUSES = ["draft", "sent", "approved", "rejected", "issued"];
  private static readonly FIELD = "invoiceStatus";
  private static readonly ERROR_CODE = "INVALID_INVOICE_STATUS";

  private static readonly TRANSITIONS: Record<string, string[]> = {
    draft: [INVOICE_STATUS.SENT],
    sent: [INVOICE_STATUS.APPROVED, INVOICE_STATUS.REJECTED],
    approved: [INVOICE_STATUS.ISSUED],
    rejected: [INVOICE_STATUS.DRAFT],
  };

  static create(value: string): Result<CustomerInvoiceStatus, Error> {
    if (!CustomerInvoiceStatus.ALLOWED_STATUSES.includes(value)) {
      const detail = `Estado de la factura no válido: ${value}`;
      return Result.fail(
        new DomainValidationError(
          CustomerInvoiceStatus.ERROR_CODE,
          CustomerInvoiceStatus.FIELD,
          detail
        )
      );
    }

    return Result.ok(
      value === "rejected"
        ? CustomerInvoiceStatus.createRejected()
        : value === "sent"
          ? CustomerInvoiceStatus.createSent()
          : value === "issued"
            ? CustomerInvoiceStatus.createIssued()
            : value === "approved"
              ? CustomerInvoiceStatus.createApproved()
              : CustomerInvoiceStatus.createDraft()
    );
  }

  public static createDraft(): CustomerInvoiceStatus {
    return new CustomerInvoiceStatus({ value: INVOICE_STATUS.DRAFT });
  }

  public static createIssued(): CustomerInvoiceStatus {
    return new CustomerInvoiceStatus({ value: INVOICE_STATUS.ISSUED });
  }

  public static createSent(): CustomerInvoiceStatus {
    return new CustomerInvoiceStatus({ value: INVOICE_STATUS.SENT });
  }

  public static createApproved(): CustomerInvoiceStatus {
    return new CustomerInvoiceStatus({ value: INVOICE_STATUS.APPROVED });
  }

  public static createRejected(): CustomerInvoiceStatus {
    return new CustomerInvoiceStatus({ value: INVOICE_STATUS.REJECTED });
  }

  isDraft(): boolean {
    return this.props.value === INVOICE_STATUS.DRAFT;
  }

  isApproved(): boolean {
    return this.props.value === INVOICE_STATUS.APPROVED;
  }

  getProps(): string {
    return this.props.value;
  }

  toPrimitive() {
    return this.getProps();
  }

  canTransitionTo(nextStatus: string): boolean {
    return CustomerInvoiceStatus.TRANSITIONS[this.props.value].includes(nextStatus);
  }

  transitionTo(nextStatus: string): Result<CustomerInvoiceStatus, Error> {
    if (!this.canTransitionTo(nextStatus)) {
      return Result.fail(
        new Error(`Transición no permitida de ${this.props.value} a ${nextStatus}`)
      );
    }
    return CustomerInvoiceStatus.create(nextStatus);
  }

  toString() {
    return String(this.props.value);
  }
}
