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

export const firmataDomain = 'firmata';
export const firmataDefaultName = 'Firmata';

export type TFirmataRawData = TSimpleLocalRawData;
export type TFirmataPinMode = 'ANALOG' | 'OUTPUT' | 'PWM' | 'INPUT' | 'PULLUP' | string;
export type TFirmataPin = number | string;

export interface IFirmataPinConfig {
  name: string;
  pin: TFirmataPin;
  pinMode: TFirmataPinMode;
  initial?: boolean | number;
  negate?: boolean;
  minimum?: number;
  maximum?: number;
  differential?: number;
}

export interface IFirmataSnapshot extends ISimpleLocalSnapshot {}
export interface IFirmataConfig extends ISimpleLocalConfig {
  serialPort?: string;
  serialBaudRate?: number;
  arduinoInstanceId?: number;
  arduinoWait?: number;
  sleepTune?: number;
  samplingInterval?: number;
  switches?: IFirmataPinConfig[];
  lights?: IFirmataPinConfig[];
  binarySensors?: IFirmataPinConfig[];
  sensors?: IFirmataPinConfig[];
}
export interface IHomeAssistantFirmataConfig extends IFirmataConfig {}

export const firmataControlServices = [
  'turn_on',
  'turn_off',
  'toggle',
  'set_level',
];

export const firmataProfile: ISimpleLocalIntegrationProfile = {
  domain: 'firmata',
  displayName: 'Firmata',
  manufacturer: 'Firmata',
  model: 'Firmata board',
  defaultName: firmataDefaultName,
  defaultProtocol: 'local',
  status: 'control-runtime',
  platforms: [
    'binary_sensor',
    'light',
    'sensor',
    'switch',
  ],
  serviceDomains: [
    'light',
    'switch',
  ],
  controlServices: firmataControlServices,
  discoverySources: [
    'manual',
    'usb',
    'custom',
  ],
  discoveryKeywords: [
    'firmata',
    'arduino',
    'pymata',
    'serial',
    'microcontroller',
  ],
  metadata: {
    source: 'home-assistant/core',
    upstreamPath: 'homeassistant/components/firmata',
    upstreamDomain: 'firmata',
    iotClass: 'local_push',
    qualityScale: 'legacy',
    requirements: [
      'pymata-express==1.19',
    ],
    dependencies: [],
    afterDependencies: [],
    codeowners: [
      '@DaAwesomeP',
    ],
    configFlow: false,
    documentation: 'https://www.home-assistant.io/integrations/firmata',
    loggers: [
      'pymata_express',
    ],
    homeAssistantPlatforms: [
      'binary_sensor',
      'light',
      'sensor',
      'switch',
    ],
    runtime: {
      type: 'control-runtime',
      services: [
        'snapshot',
        'status',
        'refresh',
        ...firmataControlServices,
      ],
      platforms: [
        'binary_sensor',
        'light',
        'sensor',
        'switch',
      ],
      controls: true,
    },
    localApi: {
      implemented: [
        'manual serial board setup data, snapshots, raw data, snapshotProvider, and injected native clients',
        'executor-gated Firmata switch and light service dispatch',
      ],
      explicitUnsupported: [
        'claiming pin output success without injected client.execute or commandExecutor',
        'opening pymata-express serial sessions without an injected native client',
        'pin reservation, schema validation, and callback reporting beyond provided snapshot/rawData/client inputs',
      ],
    },
  },
};
