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

export interface ILoginDataProps {
  email: EmailAddress;
  plainPassword: PlainPassword;
  tabId: UniqueID;
}

export interface ILoginDataPrimitives {
  email: string;
  plainPassword: string;
  tabId: string;
}

export interface ILoginData {
  email: EmailAddress;
  plainPassword: PlainPassword;
  tabId: UniqueID;
}

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

  static createFromPrimitives(values: ILoginDataPrimitives): Result<LoginData, Error> {
    const { email, plainPassword, tabId } = values;
    const emailOrError = EmailAddress.create(email);
    const plainPasswordOrError = PlainPassword.create(plainPassword);
    const tabIdOrError = UniqueID.create(tabId, false);

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

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

    return LoginData.create({
      email: emailOrError.data,
      plainPassword: plainPasswordOrError.data,
      tabId: tabIdOrError.data,
    });
  }

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

  get plainPassword(): PlainPassword {
    return this.props.plainPassword;
  }

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