import { Type } from "typebox";

import { MAX_OPTIONS, MAX_QUESTIONS } from "./normalize.ts";

const OptionSchema = Type.Object({
  value: Type.String({ minLength: 1, maxLength: 128, description: "Stable machine-readable value returned when selected" }),
  label: Type.String({ minLength: 1, maxLength: 160, description: "User-visible option label" }),
  description: Type.Optional(Type.String({ minLength: 1, maxLength: 500, description: "Optional explanation shown below the label" })),
});

const QuestionSchema = Type.Object({
  id: Type.String({
    minLength: 1,
    maxLength: 64,
    pattern: "^[A-Za-z][A-Za-z0-9_-]*$",
    description: "Unique machine-readable question identifier",
  }),
  label: Type.Optional(Type.String({ minLength: 1, maxLength: 48, description: "Short progress label; defaults to Q1, Q2, ..." })),
  prompt: Type.String({ minLength: 1, maxLength: 1_000, description: "Full question shown to the user" }),
  type: Type.String({ enum: ["select", "text"], description: "select for choices; text for a free-form answer" }),
  options: Type.Optional(Type.Array(OptionSchema, {
    maxItems: MAX_OPTIONS,
    description: "Required and non-empty for select questions; omit for text questions",
  })),
  allowCustom: Type.Optional(Type.Boolean({ description: "For select questions, append a free-form answer choice (default: true)" })),
  required: Type.Optional(Type.Boolean({ description: "Whether the question must be answered (default: true); optional questions can be skipped" })),
});

export const AskUserParamsSchema = Type.Object({
  questions: Type.Array(QuestionSchema, {
    minItems: 1,
    maxItems: MAX_QUESTIONS,
    description: "One or more questions to present in a single sequential interaction",
  }),
});
