import { DomainEntity, EmailAddress, UniqueID } from "@/core/common/domain";
import { Result } from "@repo/rdx-utils";

export interface ILogoutDataProps {
  email: EmailAddress;
  tabId: UniqueID;
}

export interface ILogoutDataPrimitives {
  email: string;
  tabId: string;
}

export interface ILogoutData {
  email: EmailAddress;
  tabId: UniqueID;
}

export class LogoutData extends DomainEntity<ILogoutDataProps> implements ILogoutData {
  static create(props: ILogoutDataProps): Result<LogoutData, Error> {
    return Result.ok(new this(props));
  }

  static createFromPrimitives(values: ILogoutDataPrimitives): Result<LogoutData, Error> {
    const { email, tabId } = values;
    const emailOrError = EmailAddress.create(email);
    const tabIdOrError = UniqueID.create(tabId, false);

    const result = Result.combine([emailOrError, tabIdOrError]);

    if (result.isFailure) {
      return Result.fail(result.error);
    }

    return LogoutData.create({
      email: emailOrError.data,
      tabId: tabIdOrError.data,
    });
  }

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

  get tabId(): UniqueID {
    return this.props.tabId;
  }
}
