import {
  AggregateRoot,
  type EmailAddress,
  type PhoneNumber,
  type PostalAddress,
  type TINNumber,
  UniqueID,
} from "@/core/common/domain";
import { Maybe, Result } from "@repo/rdx-utils";

export interface IContactProps {
  reference: string;
  isFreelancer: boolean;
  name: string;
  tin: TINNumber;
  address: PostalAddress;
  email: EmailAddress;
  phone: PhoneNumber;
  legalRecord: string;
  defaultTax: number;
  status: string;
  langCode: string;
  currencyCode: string;

  tradeName: Maybe<string>;
  website: Maybe<string>;
  fax: Maybe<PhoneNumber>;
}

export interface IContact {
  id: UniqueID;
  reference: string;
  name: string;
  tin: TINNumber;
  address: PostalAddress;
  email: EmailAddress;
  phone: PhoneNumber;
  legalRecord: string;
  defaultTax: number;
  langCode: string;
  currencyCode: string;

  tradeName: Maybe<string>;
  fax: Maybe<PhoneNumber>;
  website: Maybe<string>;

  isContact: boolean;
  isFreelancer: boolean;
  isActive: boolean;
}

export class Contact extends AggregateRoot<IContactProps> implements IContact {
  static create(props: IContactProps, id?: UniqueID): Result<Contact, Error> {
    const contact = new Contact(props, id);

    // Reglas de negocio / validaciones
    // ...
    // ...

    // 🔹 Disparar evento de dominio "ContactAuthenticatedEvent"
    //const { contact } = props;
    //user.addDomainEvent(new ContactAuthenticatedEvent(id, contact.toString()));

    return Result.ok(contact);
  }

  get reference() {
    return this.props.reference;
  }

  get name() {
    return this.props.name;
  }

  get tradeName() {
    return this.props.tradeName;
  }

  get tin(): TINNumber {
    return this.props.tin;
  }

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

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

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

  get fax(): Maybe<PhoneNumber> {
    return this.props.fax;
  }

  get website() {
    return this.props.website;
  }

  get legalRecord() {
    return this.props.legalRecord;
  }

  get defaultTax() {
    return this.props.defaultTax;
  }

  get langCode() {
    return this.props.langCode;
  }

  get currencyCode() {
    return this.props.currencyCode;
  }

  get isContact(): boolean {
    return !this.props.isFreelancer;
  }

  get isFreelancer(): boolean {
    return this.props.isFreelancer;
  }

  get isActive(): boolean {
    return this.props.status === "active";
  }
}
