import { DomainValidationError, ValueObject } from "@repo/rdx-ddd";
import { Maybe, Result } from "@repo/rdx-utils";
import { z } from "zod/v4";

interface VerifactuRecordUrlProps {
  value: string;
}

export class VerifactuRecordUrl extends ValueObject<VerifactuRecordUrlProps> {
  private static readonly MAX_LENGTH = 255;
  private static readonly FIELD = "verifactuRecordUrl";
  private static readonly ERROR_CODE = "INVALID_URL";

  protected static validate(value: string) {
    const schema = z
      .string()
      .trim()
      .max(VerifactuRecordUrl.MAX_LENGTH, {
        message: `Description must be at most ${VerifactuRecordUrl.MAX_LENGTH} characters long`,
      });
    return schema.safeParse(value);
  }

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

    if (!valueIsValid.success) {
      const detail = valueIsValid.error.message;
      return Result.fail(
        new DomainValidationError(VerifactuRecordUrl.ERROR_CODE, VerifactuRecordUrl.FIELD, detail)
      );
    }
    return Result.ok(new VerifactuRecordUrl({ value }));
  }

  static createNullable(value?: string): Result<Maybe<VerifactuRecordUrl>, Error> {
    if (!value || value.trim() === "") {
      return Result.ok(Maybe.none<VerifactuRecordUrl>());
    }

    return VerifactuRecordUrl.create(value).map((value) => Maybe.some(value));
  }

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

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

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