export const apcupsdDefaultPort = 3551;
export const apcupsdDefaultTimeoutMs = 10000;

export type TApcupsdTransport = 'tcp' | 'snapshot';
export type TApcupsdSnapshotSource = 'tcp' | 'manual' | 'snapshot' | 'runtime';

export interface IApcupsdConfig {
  host?: string;
  port?: number;
  timeoutMs?: number;
  name?: string;
  uniqueId?: string;
  transport?: TApcupsdTransport;
  snapshot?: IApcupsdSnapshot;
  rawStatus?: string | IApcupsdStatusRecord;
  online?: boolean;
}

export interface IHomeAssistantApcupsdConfig extends IApcupsdConfig {}

export interface IApcupsdStatusRecord {
  [key: string]: string;
}

export interface IApcupsdUpsInfo {
  id?: string;
  name?: string;
  host?: string;
  port?: number;
  model?: string;
  serialNumber?: string;
  firmware?: string;
  version?: string;
  hostname?: string;
  mode?: string;
  status?: string;
  statusFlag?: number;
  lineOnline?: boolean;
}

export interface IApcupsdBatteryInfo {
  chargePercent?: number;
  voltage?: number;
  nominalVoltage?: number;
  timeLeftMinutes?: number;
  timeOnBatterySeconds?: number;
  totalTimeOnBatterySeconds?: number;
  status?: string;
  replacementDate?: string;
  lastSelfTest?: string;
  selfTest?: string;
  badBatteries?: number;
  externalBatteries?: number;
}

export interface IApcupsdPowerInfo {
  lineVoltage?: number;
  outputVoltage?: number;
  loadPercent?: number;
  loadApparentPercent?: number;
  lineFrequency?: number;
  nominalInputVoltage?: number;
  nominalOutputVoltage?: number;
  nominalPowerWatts?: number;
  nominalApparentPowerVa?: number;
  outputCurrentAmps?: number;
  inputVoltageHigh?: number;
  inputVoltageLow?: number;
  transferHighVoltage?: number;
  transferLowVoltage?: number;
  transferCount?: number;
  lastTransfer?: string;
}

export interface IApcupsdSnapshot {
  ups: IApcupsdUpsInfo;
  battery: IApcupsdBatteryInfo;
  power: IApcupsdPowerInfo;
  status: IApcupsdStatusRecord;
  online: boolean;
  updatedAt?: string;
  source?: TApcupsdSnapshotSource;
  raw?: string;
  error?: string;
}

export interface IApcupsdManualEntry {
  host?: string;
  port?: number;
  id?: string;
  name?: string;
  manufacturer?: string;
  model?: string;
  snapshot?: IApcupsdSnapshot;
  rawStatus?: string | IApcupsdStatusRecord;
  metadata?: Record<string, unknown>;
}

export interface IApcupsdDiscoveryRecord {
  source: 'manual';
  host?: string;
  port?: number;
  name?: string;
  id?: string;
}
