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

export interface IRegisterDataProps {
  username: Username;
  email: EmailAddress;
  hashPassword: HashPassword;
}

export interface IRegisterDataPrimitives {
  username: string;
  email: string;
  plainPassword: string;
}

export interface IRegisterData {
  username: Username;
  email: EmailAddress;
  hashPassword: HashPassword;
}

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

  static createFromPrimitives(props: IRegisterDataPrimitives): Result<RegisterData, Error> {
    const { username, email, plainPassword } = props;

    const userNameOrError = Username.create(username);
    const emailOrError = EmailAddress.create(email);
    const hashPasswordOrError = HashPassword.createFromPlainText(plainPassword);

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

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

    return RegisterData.create({
      username: userNameOrError.data,
      email: emailOrError.data,
      hashPassword: hashPasswordOrError.data,
    });
  }

  get username(): Username {
    return this.props.username;
  }

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

  get hashPassword(): HashPassword {
    return this.props.hashPassword;
  }
}
