import { Result } from "@repo/rdx-utils";
import { z } from "zod/v4";
import { translateZodValidationError } from "../helpers";
import { ValueObject } from "./value-object";

interface SlugProps {
  value: string;
}

export class Slug extends ValueObject<SlugProps> {
  protected static readonly MIN_LENGTH = 2;
  protected static readonly MAX_LENGTH = 100;

  protected static validate(value: string) {
    const schema = z
      .string()
      .trim()
      .regex(/^[a-z0-9-]+$/, {
        message: "Slug must contain only lowercase letters, numbers, and hyphens",
      })
      .min(Slug.MIN_LENGTH, { message: `Slug must be at least ${Slug.MIN_LENGTH} characters long` })
      .max(Slug.MAX_LENGTH, { message: `Slug must be at most ${Slug.MAX_LENGTH} characters long` });
    return schema.safeParse(value);
  }

  static create(value: string) {
    const valueIsValid = Slug.validate(value);

    if (!valueIsValid.success) {
      return Result.fail(translateZodValidationError("Slug creation failed", valueIsValid.error));
    }
    // biome-ignore lint/style/noNonNullAssertion: <explanation>
    return Result.ok(new Slug({ value: valueIsValid.data! }));
  }

  getProps(): string {
    return this.props.value;
  }

  toPrimitive(): string {
    return this.getProps();
  }

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