import type { ConfigValidationWarning } from './config';

export type ConfigSettingsCategory =
  | 'appearance'
  | 'behavior'
  | 'mining-anki'
  | 'input'
  | 'integrations'
  | 'tracking-app'
  | 'advanced';

export type ConfigSettingsControl =
  | 'boolean'
  | 'number'
  | 'text'
  | 'textarea'
  | 'select'
  | 'color'
  | 'string-list'
  | 'json'
  | 'secret'
  | 'keyboard-shortcut'
  | 'key-code'
  | 'mpv-key'
  | 'known-words-decks'
  | 'anki-deck'
  | 'anki-note-type'
  | 'anki-field'
  | 'mpv-keybindings'
  | 'color-list'
  | 'css-declarations';

export type ConfigSettingsRestartBehavior = 'hot-reload' | 'restart';

export interface ConfigSettingsField {
  id: string;
  label: string;
  description: string;
  configPath: string;
  category: ConfigSettingsCategory;
  section: string;
  subsection?: string;
  control: ConfigSettingsControl;
  defaultValue: unknown;
  enumValues?: readonly string[];
  enumLabels?: Record<string, string>;
  restartBehavior: ConfigSettingsRestartBehavior;
  advanced?: boolean;
  secret?: boolean;
  legacyHidden?: boolean;
  settingsHidden?: boolean;
}

export type ConfigSettingsSnapshotValue = unknown;

export interface ConfigSettingsSnapshot {
  configPath: string;
  fields: ConfigSettingsField[];
  values: Record<string, ConfigSettingsSnapshotValue>;
  warnings: ConfigValidationWarning[];
}

export type ConfigSettingsPatchOperation =
  | {
      op: 'set';
      path: string;
      value: unknown;
    }
  | {
      op: 'reset';
      path: string;
    };

export interface ConfigSettingsPatch {
  operations: ConfigSettingsPatchOperation[];
}

export interface ConfigSettingsSaveResult {
  ok: boolean;
  snapshot?: ConfigSettingsSnapshot;
  warnings?: ConfigValidationWarning[];
  error?: string;
  hotReloadFields: string[];
  restartRequiredFields: string[];
  restartRequiredSections?: string[];
}

export interface ConfigSettingsAPI {
  getSnapshot(): Promise<ConfigSettingsSnapshot>;
  savePatch(patch: ConfigSettingsPatch): Promise<ConfigSettingsSaveResult>;
  openSettingsFile(): Promise<boolean>;
  openSettingsWindow(): Promise<boolean>;
  getAnkiDeckNames(draftUrl?: string): Promise<ConfigSettingsAnkiListResult>;
  getAnkiDeckFieldNames(deckName: string, draftUrl?: string): Promise<ConfigSettingsAnkiListResult>;
  getAnkiDeckModelNames(deckName: string, draftUrl?: string): Promise<ConfigSettingsAnkiListResult>;
  getAnkiModelNames(draftUrl?: string): Promise<ConfigSettingsAnkiListResult>;
  getAnkiModelFieldNames(
    modelName: string,
    draftUrl?: string,
  ): Promise<ConfigSettingsAnkiListResult>;
  getYomitanAnkiDeckName(): Promise<ConfigSettingsAnkiDeckResult>;
}

export interface ConfigSettingsAnkiListResult {
  ok: boolean;
  values: string[];
  error?: string;
}

export interface ConfigSettingsAnkiDeckResult {
  ok: boolean;
  value: string;
  error?: string;
}
