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

export const influxdbDomain = "influxdb";
export const influxdbDefaultName = "InfluxDB";
export const influxdbDefaultPort = 8086;
export const influxdbDefaultTimeoutMs = 10000;

export type TInfluxdbApiVersion = '1' | '2';
export type TInfluxdbQueryLanguage = 'influxql' | 'flux';

export interface IInfluxdbQueryConfig {
  name?: string;
  uniqueId?: string;
  unique_id?: string;
  database?: string;
  dbName?: string;
  db_name?: string;
  bucket?: string;
  organization?: string;
  org?: string;
  query?: string;
  measurement?: string;
  field?: string;
  where?: string;
  groupFunction?: string;
  group_function?: string;
  rangeStart?: string;
  range_start?: string;
  rangeStop?: string;
  range_stop?: string;
  imports?: string[];
  unitOfMeasurement?: string;
  unit_of_measurement?: string;
}

export interface IInfluxdbPoint {
  measurement: string;
  tags?: Record<string, string | number | boolean>;
  fields: Record<string, string | number | boolean>;
  time?: string | number | Date;
}

export interface IInfluxdbRepositoryInfo {
  name: string;
  kind: 'database' | 'bucket';
  id?: string;
  organization?: string;
}

export interface IInfluxdbQuerySample {
  name: string;
  query: string;
  language: TInfluxdbQueryLanguage;
  value: unknown;
  repository?: string;
  unit?: string;
  raw?: Record<string, unknown>;
}

export interface IInfluxdbRawHttpData {
  device: {
    id?: string;
    name: string;
    manufacturer: string;
    model: string;
    host?: string;
    port?: number;
    protocol: 'http' | 'https';
  };
  apiVersion: TInfluxdbApiVersion;
  repositories: IInfluxdbRepositoryInfo[];
  queries: IInfluxdbQuerySample[];
  entities: ISimpleLocalEntitySnapshot[];
}

export interface IInfluxdbWriteResult {
  success: boolean;
  lineCount: number;
  apiVersion: TInfluxdbApiVersion;
  repository: string;
}

export interface IInfluxdbRefreshResult {
  success: boolean;
  snapshot: IInfluxdbSnapshot;
  error?: string;
  data?: Record<string, unknown>;
}

export type TInfluxdbRawData = TSimpleLocalRawData | IInfluxdbRawHttpData;
export interface IInfluxdbSnapshot extends ISimpleLocalSnapshot {
  rawData?: TInfluxdbRawData;
}
export interface IInfluxdbConfig extends ISimpleLocalConfig {
  url?: string;
  macAddress?: string;
  deviceId?: string;
  ssl?: boolean;
  apiVersion?: TInfluxdbApiVersion | 1 | 2;
  api_version?: TInfluxdbApiVersion | 1 | 2;
  database?: string;
  dbName?: string;
  db_name?: string;
  bucket?: string;
  organization?: string;
  org?: string;
  precision?: 'ns' | 'us' | 'ms' | 's';
  queries?: IInfluxdbQueryConfig[];
  queriesFlux?: IInfluxdbQueryConfig[];
  queries_flux?: IInfluxdbQueryConfig[];
}
export interface IHomeAssistantInfluxdbConfig extends IInfluxdbConfig {}

export const influxdbProfile: ISimpleLocalIntegrationProfile = {
  domain: influxdbDomain,
  displayName: "InfluxDB",
  manufacturer: "InfluxDB",
  model: "InfluxDB local integration",
  defaultName: influxdbDefaultName,
  defaultPort: influxdbDefaultPort,
  defaultProtocol: 'http',
  status: "read-only-runtime",
  platforms: [
    "sensor",
  ],
  serviceDomains: [],
  controlServices: [],
  discoverySources: [
    'manual',
    'custom',
  ],
  discoveryKeywords: [
    "influxdb",
    "InfluxDB",
    "Influxdb",
  ],
  metadata: {
    "source": "home-assistant/core",
    "upstreamPath": "homeassistant/components/influxdb",
    "upstreamDomain": "influxdb",
    "iotClass": "local_push",
    "requirements": [
      "influxdb==5.3.1",
      "influxdb-client==1.50.0",
    ],
    "dependencies": [
      "file_upload",
    ],
    "codeowners": [
      "@mdegat01",
      "@Robbie1221",
    ],
    "configFlow": true,
    "documentation": "https://www.home-assistant.io/integrations/influxdb",
    "loggers": [
      "influxdb",
      "influxdb_client",
    ],
    "runtime": {
      "type": "read-only-runtime",
      "polling": "local HTTP InfluxDB API v1/v2 using fetch",
      "services": [
        "snapshot",
        "status",
        "refresh",
      ],
      "platforms": [
        "sensor",
      ],
      "controls": false,
    },
    "localApi": {
      "implemented": [
        "native local HTTP fetch support for InfluxDB v1 /query and /write endpoints",
        "native local HTTP fetch support for InfluxDB v2 /api/v2/buckets, /api/v2/query, and /api/v2/write endpoints",
        "configured InfluxQL and Flux query sensors mapped into runtime snapshots",
        "manual snapshots, rawData, snapshotProvider, and injected native clients",
        "runtime mapping for InfluxDB device snapshots and Home Assistant platform metadata",
        "safe command delegation to injected client.execute or commandExecutor only",
      ],
      "explicitUnsupported": [
        "Home Assistant event-bus subscription/threaded state writer; this client exposes explicit write() calls but does not subscribe to HA state changes",
        "InfluxDB custom CA certificate and verify_ssl=false handling from HA config because global fetch does not expose per-request TLS trust overrides here",
        "HA InfluxDB v2 cloud default host is not contacted unless an explicit local url or host is configured",
        "Home Assistant entity registry migrations and repair flows",
        "claiming command success without injected client.execute or commandExecutor",
      ],
    },
  },
};
