export const unifiDefaultPort = 443;
export const unifiDefaultSite = 'default';

export type TUnifiProtocol = 'http' | 'https';
export type TUnifiCommandType = 'blockClient' | 'reconnectClient' | 'setWlanEnabled' | 'setPoePortEnabled';
export type TUnifiDiscoveryService = 'network' | 'protect' | 'access' | 'unknown';

export interface IUnifiConfig {
  host?: string;
  port?: number;
  protocol?: TUnifiProtocol;
  site?: string;
  username?: string;
  password?: string;
  verifySsl?: boolean;
  controller?: IUnifiController;
  snapshot?: IUnifiSnapshot;
  manualEntries?: IUnifiManualEntry[];
  sites?: IUnifiSite[];
  clients?: IUnifiClient[];
  devices?: IUnifiDevice[];
  wlans?: IUnifiWlan[];
  ports?: IUnifiPort[];
  events?: IUnifiEvent[];
  trackClients?: boolean;
  trackDevices?: boolean;
  trackWiredClients?: boolean;
  allowBandwidthSensors?: boolean;
  allowUptimeSensors?: boolean;
  detectionTimeSeconds?: number;
}

export interface IUnifiController {
  id?: string;
  name?: string;
  host?: string;
  port?: number;
  site?: string;
  version?: string;
  deviceType?: string;
  isUnifiOs?: boolean;
  connected?: boolean;
}

export interface IUnifiSite {
  id?: string;
  siteId?: string;
  _id?: string;
  name?: string;
  description?: string;
  desc?: string;
  role?: string;
}

export interface IUnifiClient {
  id?: string;
  _id?: string;
  mac: string;
  name?: string;
  hostname?: string;
  deviceName?: string;
  device_name?: string;
  ip?: string;
  oui?: string;
  essid?: string;
  network?: string;
  isWired?: boolean;
  is_wired?: boolean;
  isGuest?: boolean;
  is_guest?: boolean;
  blocked?: boolean;
  authorized?: boolean;
  lastSeen?: number;
  last_seen?: number;
  firstSeen?: number;
  first_seen?: number;
  uptime?: number;
  apMac?: string;
  ap_mac?: string;
  switchMac?: string;
  sw_mac?: string;
  switchPort?: number;
  sw_port?: number;
  vlan?: number;
  radio?: string;
  radioName?: string;
  radio_name?: string;
  radioProto?: string;
  radio_proto?: string;
  rssi?: number;
  rxBytesR?: number;
  'rx_bytes-r'?: number;
  txBytesR?: number;
  'tx_bytes-r'?: number;
  wiredRxBytesR?: number;
  'wired-rx_bytes-r'?: number;
  wiredTxBytesR?: number;
  'wired-tx_bytes-r'?: number;
  wiredRateMbps?: number;
  wired_rate_mbps?: number;
  fixedIp?: string;
  fixed_ip?: string;
  note?: string;
  raw?: Record<string, unknown>;
}

export interface IUnifiDevice {
  id?: string;
  _id?: string;
  deviceId?: string;
  device_id?: string;
  mac: string;
  name?: string;
  model?: string;
  version?: string;
  boardRevision?: number;
  board_rev?: number;
  ip?: string;
  type?: string;
  state?: number | string;
  disabled?: boolean;
  uptime?: number;
  numSta?: number;
  num_sta?: number;
  'user-num_sta'?: number;
  'guest-num_sta'?: number;
  generalTemperature?: number;
  general_temperature?: number;
  hasTemperature?: boolean;
  has_temperature?: boolean;
  outletAcPowerBudget?: string | number;
  outlet_ac_power_budget?: string | number;
  outletAcPowerConsumption?: string | number;
  outlet_ac_power_consumption?: string | number;
  portTable?: IUnifiPort[];
  port_table?: IUnifiPort[];
  portOverrides?: IUnifiPortOverride[];
  port_overrides?: IUnifiPortOverride[];
  wlanOverrides?: IUnifiWlanOverride[];
  wlan_overrides?: IUnifiWlanOverride[];
  temperatures?: IUnifiTemperature[];
  systemStats?: IUnifiSystemStats;
  'system-stats'?: IUnifiSystemStats;
  raw?: Record<string, unknown>;
}

export interface IUnifiSystemStats {
  cpu?: string | number;
  mem?: string | number;
  uptime?: string | number;
}

export interface IUnifiTemperature {
  name?: string;
  type?: string;
  value?: number;
}

export interface IUnifiPortOverride {
  portIdx?: number;
  port_idx?: number;
  poeMode?: string;
  poe_mode?: string;
  portSecurityEnabled?: boolean;
  port_security_enabled?: boolean;
  portconfId?: string;
  portconf_id?: string;
}

export interface IUnifiPort {
  id?: string;
  deviceMac?: string;
  device_mac?: string;
  portIdx?: number | string;
  port_idx?: number;
  ifname?: string;
  name?: string;
  media?: string;
  enabled?: boolean;
  enable?: boolean;
  up?: boolean;
  portPoe?: boolean;
  port_poe?: boolean;
  poeEnable?: boolean;
  poe_enable?: boolean;
  poeMode?: string;
  poe_mode?: string;
  poeCaps?: number;
  poe_caps?: number;
  poePower?: string | number;
  poe_power?: string | number;
  speed?: number;
  rxBytesR?: number;
  'rx_bytes-r'?: number;
  txBytesR?: number;
  'tx_bytes-r'?: number;
  portconfId?: string;
  portconf_id?: string;
  raw?: Record<string, unknown>;
}

export interface IUnifiWlan {
  id?: string;
  _id?: string;
  name: string;
  enabled?: boolean;
  siteId?: string;
  site_id?: string;
  security?: string;
  isGuest?: boolean;
  is_guest?: boolean;
  hideSsid?: boolean;
  hide_ssid?: boolean;
  nameCombineEnabled?: boolean;
  name_combine_enabled?: boolean;
  nameCombineSuffix?: string;
  name_combine_suffix?: string;
  raw?: Record<string, unknown>;
}

export interface IUnifiWlanOverride {
  name?: string;
  radio?: string;
  radioName?: string;
  radio_name?: string;
  wlanId?: string;
  wlan_id?: string;
}

export interface IUnifiEvent {
  type?: string;
  key?: string;
  mac?: string;
  deviceId?: string;
  entityId?: string;
  data?: unknown;
  timestamp?: number;
}

export interface IUnifiSnapshot {
  connected: boolean;
  host?: string;
  port?: number;
  site?: string;
  controller?: IUnifiController;
  sites: IUnifiSite[];
  clients: IUnifiClient[];
  devices: IUnifiDevice[];
  wlans: IUnifiWlan[];
  ports: IUnifiPort[];
  events: IUnifiEvent[];
}

export interface IUnifiManualEntry {
  id?: string;
  host?: string;
  port?: number;
  site?: string;
  name?: string;
  controller?: IUnifiController;
  snapshot?: IUnifiSnapshot;
  sites?: IUnifiSite[];
  clients?: IUnifiClient[];
  devices?: IUnifiDevice[];
  wlans?: IUnifiWlan[];
  ports?: IUnifiPort[];
  metadata?: Record<string, unknown>;
}

export interface IUnifiCommand {
  type: TUnifiCommandType;
  service: string;
  target: {
    entityId?: string;
    deviceId?: string;
  };
  mac?: string;
  wlanId?: string;
  deviceMac?: string;
  portIdx?: number | string;
  enabled?: boolean;
  block?: boolean;
}

export interface IUnifiCommandResult {
  success: boolean;
  error?: string;
  data?: unknown;
}

export interface IUnifiMdnsRecord {
  type?: string;
  name?: string;
  host?: string;
  port?: number;
  txt?: {
    mac?: string;
    hw_addr?: string;
    model?: string;
    modelid?: string;
    name?: string;
    controller_uuid?: string;
    uuid?: string;
    [key: string]: string | undefined;
  };
}

export interface IUnifiSsdpRecord {
  location?: string;
  host?: string;
  port?: number;
  manufacturer?: string;
  modelName?: string;
  modelDescription?: string;
  server?: string;
  usn?: string;
  st?: string;
  nt?: string;
  udn?: string;
  headers?: Record<string, string | undefined>;
}

export interface IUnifiManualDiscoveryEntry {
  id?: string;
  host?: string;
  port?: number;
  site?: string;
  name?: string;
  macAddress?: string;
  manufacturer?: string;
  model?: string;
  services?: Partial<Record<TUnifiDiscoveryService, boolean>>;
  metadata?: Record<string, unknown>;
}

export interface IUnifiDiscoveryDeviceRecord {
  source_ip?: string;
  hw_addr?: string;
  direct_connect_domain?: string;
  services?: Partial<Record<TUnifiDiscoveryService, boolean>> | Record<string, boolean | undefined>;
  name?: string;
  model?: string;
}

export type IHomeAssistantUnifiConfig = IUnifiConfig;
