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

const RoleSchema = z.enum(["Admin", "User", "Manager", "Editor"]);

interface UserRolesProps {
  value: string[];
}

export class UserRoles extends ValueObject<UserRolesProps> {
  static create(roles: string[]): Result<UserRoles, Error> {
    const result = UserRoles.validate(roles);

    return result.success
      ? Result.ok(new UserRoles({ value: result.data }))
      : Result.fail(new Error("Invalid user roles"));
  }

  private static validate(roles: string[]) {
    return z.array(RoleSchema).safeParse(roles);
  }

  hasRole(role: string): boolean {
    return this.props.value.includes(role);
  }

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

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