export const froniusDomain = 'fronius';
export const froniusDisplayName = 'Fronius';
export const froniusManufacturer = 'Fronius';
export const froniusDefaultHttpPort = 80;
export const froniusDefaultHttpsPort = 443;
export const froniusDefaultTimeoutMs = 5000;
export const froniusDhcpMacPrefix = '0003AC';

export type TFroniusProtocol = 'http' | 'https';
export type TFroniusApiVersion = 'auto' | 'v0' | 'v1';
export type TFroniusSnapshotSource = 'http' | 'client' | 'manual' | 'snapshot' | 'runtime';
export type TFroniusDeviceKind = 'system' | 'inverter' | 'meter' | 'ohmpilot' | 'storage';

export interface IFroniusCommandRequest {
  action: string;
  service?: string;
  target?: Record<string, unknown>;
  data?: Record<string, unknown>;
}

export interface IFroniusCommandExecutor {
  execute(commandArg: IFroniusCommandRequest): Promise<unknown> | unknown;
}

export interface IFroniusClientLike {
  getSnapshot?(): Promise<IFroniusSnapshot | Partial<IFroniusRawData> | unknown> | IFroniusSnapshot | Partial<IFroniusRawData> | unknown;
  getRawData?(): Promise<Partial<IFroniusRawData>> | Partial<IFroniusRawData>;
  currentLoggerInfo?(): Promise<unknown> | unknown;
  current_logger_info?(): Promise<unknown> | unknown;
  inverterInfo?(): Promise<unknown> | unknown;
  inverter_info?(): Promise<unknown> | unknown;
  currentActiveDeviceInfo?(): Promise<unknown> | unknown;
  current_active_device_info?(): Promise<unknown> | unknown;
  currentPowerFlow?(): Promise<unknown> | unknown;
  current_power_flow?(): Promise<unknown> | unknown;
  currentSystemMeterData?(): Promise<unknown> | unknown;
  current_system_meter_data?(): Promise<unknown> | unknown;
  currentSystemOhmpilotData?(): Promise<unknown> | unknown;
  current_system_ohmpilot_data?(): Promise<unknown> | unknown;
  currentSystemStorageData?(): Promise<unknown> | unknown;
  current_system_storage_data?(): Promise<unknown> | unknown;
  currentInverterData?(deviceIdArg: string): Promise<unknown> | unknown;
  current_inverter_data?(deviceIdArg: string): Promise<unknown> | unknown;
  execute?(commandArg: IFroniusCommandRequest): Promise<unknown> | unknown;
  destroy?(): Promise<void> | void;
}

export interface IFroniusConfig {
  host?: string;
  url?: string;
  port?: number;
  protocol?: TFroniusProtocol;
  apiVersion?: TFroniusApiVersion;
  timeoutMs?: number;
  verifyTls?: boolean;
  name?: string;
  manufacturer?: string;
  model?: string;
  uniqueId?: string;
  serialNumber?: string;
  isLogger?: boolean;
  online?: boolean;
  snapshot?: IFroniusSnapshot;
  rawData?: Partial<IFroniusRawData>;
  client?: IFroniusClientLike;
  commandExecutor?: IFroniusCommandExecutor;
}

export interface IHomeAssistantFroniusConfig extends IFroniusConfig {}

export interface IFroniusRawData {
  apiVersion?: Record<string, unknown>;
  loggerInfo?: unknown;
  inverterInfo?: unknown;
  activeDeviceInfo?: unknown;
  powerFlow?: unknown;
  systemMeter?: unknown;
  systemOhmpilot?: unknown;
  systemStorage?: unknown;
  inverterDeviceData?: Record<string, unknown>;
  errors?: Record<string, string>;
  fetchedAt?: string;
}

export interface IFroniusDeviceSnapshot {
  id: string;
  kind: TFroniusDeviceKind;
  name: string;
  manufacturer: string;
  model?: string;
  serialNumber?: string;
  uniqueId?: string;
  solarNetId?: string;
  viaDeviceId?: string;
  metadata?: Record<string, unknown>;
}

export interface IFroniusSensorSnapshot {
  id: string;
  deviceId: string;
  kind: string;
  name: string;
  value: unknown;
  unit?: string;
  deviceClass?: string;
  stateClass?: string;
  entityCategory?: string;
  available: boolean;
  attributes?: Record<string, unknown>;
}

export interface IFroniusLoggerSnapshot {
  uniqueIdentifier?: string;
  hardwareVersion?: string;
  softwareVersion?: string;
  productType?: string;
  timeZone?: string;
  raw?: unknown;
}

export interface IFroniusInverterSnapshot extends IFroniusDeviceSnapshot {
  kind: 'inverter';
  pvPower?: number;
  statusCode?: number;
  errorCode?: number;
}

export interface IFroniusCapabilitiesSnapshot {
  localRead: boolean;
  localControl: boolean;
  readOnlySolarApi: boolean;
}

export interface IFroniusSnapshot {
  system: IFroniusDeviceSnapshot;
  devices: IFroniusDeviceSnapshot[];
  inverters: IFroniusInverterSnapshot[];
  sensors: IFroniusSensorSnapshot[];
  loggerInfo?: IFroniusLoggerSnapshot;
  host?: string;
  port?: number;
  protocol?: TFroniusProtocol;
  apiVersion?: TFroniusApiVersion;
  rawData?: Partial<IFroniusRawData>;
  capabilities: IFroniusCapabilitiesSnapshot;
  online: boolean;
  updatedAt: string;
  source: TFroniusSnapshotSource;
  error?: string;
}

export interface IFroniusRefreshResult {
  success: boolean;
  snapshot: IFroniusSnapshot;
  error?: string;
  data?: unknown;
}

export interface IFroniusManualEntry {
  id?: string;
  uniqueId?: string;
  host?: string;
  url?: string;
  port?: number;
  protocol?: TFroniusProtocol;
  name?: string;
  manufacturer?: string;
  model?: string;
  serialNumber?: string;
  isLogger?: boolean;
  snapshot?: IFroniusSnapshot;
  rawData?: Partial<IFroniusRawData>;
  client?: IFroniusClientLike;
  commandExecutor?: IFroniusCommandExecutor;
  metadata?: Record<string, unknown>;
}

export interface IFroniusDhcpRecord {
  id?: string;
  ip?: string;
  address?: string;
  hostname?: string;
  hostName?: string;
  macaddress?: string;
  macAddress?: string;
  metadata?: Record<string, unknown>;
}
