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

interface CountryProps {
  value: string;
}

export class Country extends ValueObject<CountryProps> {
  private static readonly MAX_LENGTH = 255;

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

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

    if (!valueIsValid.success) {
      return Result.fail(
        translateZodValidationError("Country creation failed", valueIsValid.error)
      );
    }
    return Result.ok(new Country({ value }));
  }

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

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

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