import { ValueObject } from "@repo/rdx-ddd";
import { Result } from "@repo/rdx-utils";
import * as z from "zod/v4";

interface PlainPasswordProps {
  value: string;
}

export class PlainPassword extends ValueObject<PlainPasswordProps> {
  static create(plainTextPassword: string): Result<PlainPassword, Error> {
    const result = PlainPassword.validate(plainTextPassword);

    if (!result.success) {
      return Result.fail(new Error(result.error.errors[0].message));
    }

    return Result.ok(new PlainPassword({ value: result.data }));
  }

  private static validate(password: string) {
    const schema = z.string().min(6, { message: "Password must be at least 6 characters long" });
    return schema.safeParse(password);
  }

  getValue() {
    return this.props.value;
  }

  toString() {
    return this.props.value;
  }

  toPrimitive() {
    return this.props.value;
  }
}
