import type { ISimpleLocalClientLike, ISimpleLocalConfig, ISimpleLocalIntegrationProfile, ISimpleLocalSnapshot, TSimpleLocalRawData } from '../../core/index.js';

export const maryttsDomain = "marytts";
export const maryttsDefaultName = "MaryTTS";
export const maryttsDefaultHost = 'localhost';
export const maryttsDefaultPort = 59125;
export const maryttsDefaultLanguage = 'en_US';
export const maryttsDefaultVoice = 'cmu-slt-hsmm';
export const maryttsDefaultCodec = 'WAVE_FILE';
export const maryttsDefaultInputType = 'TEXT';
export const maryttsDefaultTimeoutMs = 10000;

export type TMaryttsCodec = 'WAVE_FILE' | 'AIFF_FILE' | 'AU_FILE' | string;
export type TMaryttsAudioFormat = 'wav' | 'aiff' | 'au' | string;

export interface IMaryttsSpeakOptions {
  language?: string;
  locale?: string;
  voice?: string;
  codec?: TMaryttsCodec;
  inputType?: string;
  effect?: Record<string, string>;
  effects?: Record<string, string>;
}

export interface IMaryttsSpeakRequest extends IMaryttsSpeakOptions {
  message: string;
}

export interface IMaryttsAudioResult {
  format: TMaryttsAudioFormat;
  codec: string;
  contentType?: string;
  language: string;
  voice: string;
  effects: Record<string, string>;
  audio: Buffer;
}

export interface IMaryttsClientLike extends ISimpleLocalClientLike {
  speak?(messageArg: string, optionsArg?: IMaryttsSpeakOptions): Promise<IMaryttsAudioResult> | IMaryttsAudioResult;
  synthesize?(requestArg: IMaryttsSpeakRequest): Promise<IMaryttsAudioResult> | IMaryttsAudioResult;
}

export type TMaryttsRawData = TSimpleLocalRawData;
export interface IMaryttsSnapshot extends ISimpleLocalSnapshot {}
export interface IMaryttsConfig extends ISimpleLocalConfig {
  url?: string;
  macAddress?: string;
  deviceId?: string;
  language?: string;
  locale?: string;
  voice?: string;
  codec?: TMaryttsCodec;
  inputType?: string;
  effect?: Record<string, string>;
  effects?: Record<string, string>;
  ssl?: boolean;
  client?: IMaryttsClientLike;
}

export const maryttsProfile: ISimpleLocalIntegrationProfile = {
  domain: maryttsDomain,
  displayName: "MaryTTS",
  manufacturer: "MaryTTS",
  model: "MaryTTS local integration",
  defaultName: maryttsDefaultName,
  defaultPort: maryttsDefaultPort,
  defaultProtocol: 'http',
  status: "control-runtime",
  platforms: [
    "sensor",
  ],
  serviceDomains: [
    "tts",
  ],
  controlServices: [
    "get_tts_audio",
    "say",
    "speak",
  ],
  discoverySources: [
    'manual',
    'custom',
  ],
  discoveryKeywords: [
    "marytts",
    "MaryTTS",
    "Marytts",
  ],
  metadata: {
    "source": "home-assistant/core",
    "upstreamPath": "homeassistant/components/marytts",
    "upstreamDomain": "marytts",
    "iotClass": "local_push",
    "qualityScale": "legacy",
    "requirements": [
      "speak2mary==1.4.0",
    ],
    "configFlow": false,
    "documentation": "https://www.home-assistant.io/integrations/marytts",
    "loggers": [
      "speak2mary",
    ],
    "runtime": {
      "type": "control-runtime",
      "services": [
        "snapshot",
        "status",
        "refresh",
        "get_tts_audio",
        "say",
        "speak",
      ],
      "platforms": [
        "sensor",
      ],
      "controls": "native MaryTTS HTTP POST /process synthesis",
    },
    "localApi": {
      "implemented": [
        "native MaryTTS HTTP POST /process synthesis with application/x-www-form-urlencoded body via global fetch",
        "manual local setup for MaryTTS endpoints, snapshots, rawData, snapshotProvider, and injected native clients",
        "runtime mapping for MaryTTS device snapshots and Home Assistant platform metadata",
        "codec mapping for WAVE_FILE, AIFF_FILE, and AU_FILE audio responses",
      ],
      "explicitUnsupported": [
        "discovering voices, effects, or locales from a MaryTTS server; Home Assistant and speak2mary expose static/default values here",
        "non-HTTP MaryTTS transports or servers that do not implement the documented /process endpoint",
        "Home Assistant entity registry migrations and repair flows",
        "claiming synthesis success when the MaryTTS /process request fails or returns non-2xx",
      ],
    },
  },
};
