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

export const modernFormsDomain = "modern_forms";
export const modernFormsDefaultName = "Modern Forms";
export const modernFormsDefaultPort = 80;
export const modernFormsDefaultPath = '/mf';
export const modernFormsDefaultTimeoutMs = 5000;

export interface IModernFormsInfo extends Record<string, unknown> {
  clientId?: string;
  mac?: string;
  lightType?: string;
  fanType?: string;
  fanMotorType?: string;
  productSku?: string;
  deviceName?: string;
  firmwareVersion?: string;
  mainMcuFirmwareVersion?: string;
}

export interface IModernFormsState extends Record<string, unknown> {
  fanOn?: boolean;
  fanSpeed?: number;
  fanDirection?: string;
  fanSleepTimer?: number;
  lightOn?: boolean;
  lightBrightness?: number;
  lightSleepTimer?: number;
  awayModeEnabled?: boolean;
  adaptiveLearning?: boolean;
}

export interface IModernFormsRawApiData extends Record<string, unknown> {
  info?: IModernFormsInfo;
  state?: IModernFormsState;
  fetchedAt?: string;
}

export type TModernFormsRawData = TSimpleLocalRawData | IModernFormsRawApiData;
export interface IModernFormsSnapshot extends ISimpleLocalSnapshot {}
export interface IModernFormsConfig extends ISimpleLocalConfig {
  url?: string;
  macAddress?: string;
  deviceId?: string;
}
export interface IHomeAssistantModernFormsConfig extends IModernFormsConfig {}

export const modernFormsProfile: ISimpleLocalIntegrationProfile = {
  domain: modernFormsDomain,
  displayName: "Modern Forms",
  manufacturer: "Modern Forms",
  model: "Modern Forms local integration",
  defaultName: modernFormsDefaultName,
  defaultPort: modernFormsDefaultPort,
  defaultHttpPath: modernFormsDefaultPath,
  defaultProtocol: 'http',
  status: "control-runtime",
  platforms: [
    "binary_sensor",
    "fan",
    "light",
    "sensor",
    "switch",
  ],
  serviceDomains: [
    "fan",
    "light",
    "switch",
  ],
  controlServices: [
    "clear_fan_sleep_timer",
    "clear_light_sleep_timer",
    "set_fan_sleep_timer",
    "set_light_sleep_timer",
    "toggle",
    "turn_off",
    "turn_on",
  ],
  discoverySources: [
    'manual',
    'custom',
  ],
  discoveryKeywords: [
    "modern_forms",
    "Modern Forms",
    "Modern",
    "Forms",
  ],
  metadata: {
    "source": "home-assistant/core",
    "upstreamPath": "homeassistant/components/modern_forms",
    "upstreamDomain": "modern_forms",
    "iotClass": "local_polling",
    "requirements": [
      "aiomodernforms==0.1.8",
    ],
    "codeowners": [
      "@wonderslug",
    ],
    "configFlow": true,
    "documentation": "https://www.home-assistant.io/integrations/modern_forms",
    "loggers": [
      "aiomodernforms",
    ],
    "runtime": {
      "type": "control-runtime",
      "services": [
        "snapshot",
        "status",
        "refresh",
        "clear_fan_sleep_timer",
        "clear_light_sleep_timer",
        "set_fan_sleep_timer",
        "set_light_sleep_timer",
        "toggle",
        "turn_off",
        "turn_on",
      ],
      "platforms": [
        "binary_sensor",
        "fan",
        "light",
        "sensor",
        "switch",
      ],
      "controls": "native local HTTP /mf when config.host/config.url is configured; otherwise delegated to injected client.execute or commandExecutor",
    },
    "localApi": {
      "implemented": [
        "manual local setup for Modern Forms endpoints, snapshots, rawData, snapshotProvider, and injected native clients",
        "live local HTTP polling with POST /mf queryStaticShadowData and queryDynamicShadowData as used by aiomodernforms",
        "live local HTTP fan, light, away-mode, adaptive-learning, and sleep-timer writes through the documented /mf command keys",
        "runtime mapping for Modern Forms fan, light, switch, sensor, and binary_sensor metadata",
        "safe command delegation to injected client.execute or commandExecutor when supplied",
      ],
      "explicitUnsupported": [
        "pairing, cloud account linking, or subscription flows; HA only uses direct local HTTP once a host is known",
        "claiming live Modern Forms polling or write success without config.host/config.url, an injected native client, or snapshotProvider",
        "Home Assistant entity registry migrations and repair flows",
        "claiming command success without a real /mf response, injected client.execute, or commandExecutor",
      ],
    },
  },
};
