import type { CommandContext } from "./types";

interface InvokerLike {
  id: string;
  attributes: { permissions?: string[]; companyId?: string | null } | null;
}

interface ResolverContext {
  invoker?: InvokerLike | null;
  user: InvokerLike;
}

// Matches SDK's unauthenticatedTailorUser.id
const UNAUTHENTICATED_ACTOR_ID = "00000000-0000-0000-0000-000000000000";

export function createContext(context: ResolverContext): CommandContext {
  if (context.invoker === null) {
    return { actorId: UNAUTHENTICATED_ACTOR_ID, permissions: [], companyId: null };
  }
  const principal = context.invoker ?? context.user;
  return {
    actorId: principal.id,
    permissions: [...new Set(principal.attributes?.permissions ?? [])],
    companyId: principal.attributes?.companyId,
  };
}
