import type { TEntityPlatform } from '../../core/types.js';

export interface IMqttConfig {
  host?: string;
  port?: number;
  protocol?: 'mqtt' | 'mqtts';
  username?: string;
  password?: string;
  clientId?: string;
  keepalive?: number;
  clean?: boolean;
  rejectUnauthorized?: boolean;
  discovery?: boolean;
  discoveryPrefix?: string;
  birthMessage?: IMqttPublishOptions | false;
  willMessage?: IMqttPublishOptions | false;
  subscriptions?: string[];
  discoveryMessages?: IMqttMessage[];
  retainedMessages?: IMqttMessage[];
  entities?: IMqttEntityConfig[];
}

export interface IMqttPublishOptions {
  topic: string;
  payload?: IMqttPayload;
  qos?: 0 | 1 | 2;
  retain?: boolean;
}

export type IMqttPayload = string | number | boolean | null | Record<string, unknown> | unknown[];

export interface IMqttMessage {
  topic: string;
  payload: IMqttPayload;
  qos?: 0 | 1 | 2;
  retain?: boolean;
  timestamp?: number;
}

export interface IMqttBrokerInfo {
  host?: string;
  port: number;
  protocol: 'mqtt' | 'mqtts';
  clientId?: string;
  connected: boolean;
  discoveryPrefix: string;
}

export interface IMqttSnapshot {
  brokerInfo: IMqttBrokerInfo;
  entities: IMqttEntityConfig[];
  messages: IMqttMessage[];
}

export interface IMqttDeviceInfo {
  identifiers?: string | string[];
  connections?: string[][];
  name?: string;
  manufacturer?: string;
  model?: string;
  modelId?: string;
  swVersion?: string;
  hwVersion?: string;
  serialNumber?: string;
  configurationUrl?: string;
  suggestedArea?: string;
}

export interface IMqttOriginInfo {
  name?: string;
  swVersion?: string;
  supportUrl?: string;
}

export interface IMqttEntityConfig {
  platform: TEntityPlatform | string;
  objectId?: string;
  nodeId?: string;
  uniqueId?: string;
  defaultEntityId?: string;
  name?: string | null;
  device?: IMqttDeviceInfo;
  origin?: IMqttOriginInfo;
  stateTopic?: string;
  commandTopic?: string;
  availabilityTopic?: string;
  jsonAttributesTopic?: string;
  valueTemplate?: string;
  stateValueTemplate?: string;
  deviceClass?: string;
  unitOfMeasurement?: string;
  payloadOn?: string;
  payloadOff?: string;
  payloadAvailable?: string;
  payloadNotAvailable?: string;
  qos?: 0 | 1 | 2;
  retain?: boolean;
  state?: IMqttPayload;
  attributes?: Record<string, unknown>;
  discoveryTopic?: string;
  rawConfig?: Record<string, unknown>;
}

export interface IMqttManualEntry {
  host?: string;
  port?: number;
  protocol?: 'mqtt' | 'mqtts';
  username?: string;
  id?: string;
  name?: string;
  model?: string;
  metadata?: Record<string, unknown>;
}

export interface IMqttDiscoveryMessage {
  topic?: string;
  payload?: IMqttPayload;
}
