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

interface TokenProps {
  value: string;
}

export class Token extends ValueObject<TokenProps> {
  static create(token: string): Result<Token, Error> {
    const result = Token.validate(token);

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

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

  private static validate(token: string) {
    const schema = z.string().min(1, { message: "Invalid token string" });
    return schema.safeParse(token);
  }

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

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

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