import {
  type EmailAddress,
  type Name,
  type PostalAddress,
  ValueObject,
} from "@/core/common/domain";
import { Result } from "@repo/rdx-utils";
import { PhoneNumber } from "libphonenumber-js";
import { InvoiceAddressType } from "../../value-objects";

export interface IInvoiceAddressProps {
  type: InvoiceAddressType;
  title: Name;
  address: PostalAddress;
  email: EmailAddress;
  phone: PhoneNumber;
}

export interface IInvoiceAddress {
  type: InvoiceAddressType;
  title: Name;
  address: PostalAddress;
  email: EmailAddress;
  phone: PhoneNumber;
}

export class InvoiceAddress extends ValueObject<IInvoiceAddressProps> implements IInvoiceAddress {
  public static create(props: IInvoiceAddressProps) {
    return Result.ok(new this(props));
  }

  public static createShippingAddress(props: IInvoiceAddressProps) {
    return Result.ok(
      new this({
        ...props,
        type: InvoiceAddressType.create("shipping").data,
      })
    );
  }

  public static createBillingAddress(props: IInvoiceAddressProps) {
    return Result.ok(
      new this({
        ...props,
        type: InvoiceAddressType.create("billing").data,
      })
    );
  }

  get title(): Name {
    return this.props.title;
  }

  get address(): PostalAddress {
    return this.props.address;
  }

  get email(): EmailAddress {
    return this.props.email;
  }

  get phone(): PhoneNumber {
    return this.props.phone;
  }

  get type(): InvoiceAddressType {
    return this.props.type;
  }

  getValue(): IInvoiceAddressProps {
    return this.props;
  }

  toPrimitive() {
    return {
      type: this.type.toString(),
      title: this.title.toString(),
      address: this.address.toString(),
      email: this.email.toString(),
      phone: this.phone.toString(),
    };
  }
}
