export * as ConfigVoice from "./voice"

import { Option, Schema } from "effect"

export const Provider = Schema.Literals(["deepgram", "groq", "openai-whisper"]).annotate({
  description: "Cloud speech-to-text provider used by desktop voice input",
})

export const Info = Schema.Struct({
  enabled: Schema.optional(Schema.Boolean).annotate({
    description: "Enable microphone voice control in the desktop app",
  }),
  provider: Schema.optional(Provider),
  language: Schema.optional(Schema.String).annotate({
    description: "BCP-47 language code passed to the STT provider (for example en)",
  }),
  hotkey: Schema.optional(Schema.String).annotate({
    description: "Global accelerator for push-to-talk / toggle (for example Alt+Space)",
  }),
  commands: Schema.optional(Schema.Boolean).annotate({
    description: "Parse short spoken phrases as voice commands instead of dictation",
  }),
  autoSend: Schema.optional(Schema.Boolean).annotate({
    description: "Automatically submit the prompt after a final transcription",
  }),
}).annotate({
  identifier: "VoiceConfig",
  description: "Desktop microphone voice control settings",
})

export type Info = typeof Info.Type
export type Resolved = Required<Info>

export const defaults = {
  enabled: true,
  provider: "deepgram",
  language: "en",
  hotkey: "Alt+Space",
  commands: true,
  autoSend: false,
} satisfies Resolved

export function normalize(input: unknown): Resolved {
  const parsed = Schema.decodeUnknownOption(Info)(input)
  if (!Option.isSome(parsed)) return { ...defaults }
  return {
    enabled: parsed.value.enabled ?? defaults.enabled,
    provider: parsed.value.provider ?? defaults.provider,
    language: parsed.value.language ?? defaults.language,
    hotkey: parsed.value.hotkey ?? defaults.hotkey,
    commands: parsed.value.commands ?? defaults.commands,
    autoSend: parsed.value.autoSend ?? defaults.autoSend,
  }
}
