export type JsonPrimitive = string | number | boolean | null;
export type JsonValue = JsonPrimitive | JsonValue[] | { [key: string]: JsonValue };
export type Dict<T = unknown> = Record<string, T>;

export interface AuthContext {
  server: string | null;
  accessToken: string | null;
  projectId: string | null;
  headers: Record<string, string>;
}

export interface RuntimeContext {
  server: string;
  auth: AuthContext;
  scheme: string | null;
  timeout: number;
  requestId: string;
}

export interface GlobalArgs {
  server: string | null;
  authStdin: boolean;
  scheme: string | null;
  timeout: number;
  requestId: string | null;
}

export interface ParsedGlobalArgs {
  globals: GlobalArgs;
  rest: string[];
}

export type OptionKind = "string" | "boolean" | "repeat" | "number" | "integer";
export type OptionSchema = Record<string, OptionKind>;
export type ParsedOptionValue = string | number | boolean | string[];

export interface ParsedOptions {
  values: Record<string, ParsedOptionValue>;
  positionals: string[];
}

export interface RequestOptions {
  method: string;
  path: string;
  params?: Record<string, unknown>;
  body?: unknown;
  requireAuth?: boolean;
  requireProject?: boolean;
}

export type Handler = (ctx: RuntimeContext, argv: string[]) => Promise<void> | void;
export type HandlerMap = Record<string, Handler>;

export interface CommandDoc {
  ok: true;
  schema_version: string;
  command: string;
  summary: string;
  description: string;
  usage: string;
  options: CommandOptionDoc[];
  examples: string[];
  output: string;
  next_commands: string[];
}

export interface CommandOptionDoc {
  name: string;
  description: string;
  required: boolean;
  repeated: boolean;
  default: null;
}

export type HelpPayload =
  | CommandDoc
  | {
      ok: true;
      schema_version: string;
      summary: string;
      menu_level?: number;
      commands: Array<{ command: string; summary: string; usage?: string; example?: string }>;
    };
