export const huaweiLteDomain = 'huawei_lte';
export const huaweiLteDisplayName = 'Huawei LTE';
export const huaweiLteDefaultDeviceName = 'LTE';
export const huaweiLteDefaultManufacturer = 'Huawei Technologies Co., Ltd.';
export const huaweiLteDefaultUrl = 'http://192.168.8.1/';
export const huaweiLteDefaultHttpPort = 80;
export const huaweiLteDefaultHttpsPort = 443;
export const huaweiLteDefaultTimeoutMs = 10000;
export const huaweiLteSsdpDeviceType = 'urn:schemas-upnp-org:device:InternetGatewayDevice:1';
export const huaweiLteSsdpManufacturers = [
  'Huawei',
  'Huawei Technologies Co., Ltd.',
  'SOYEA TECHNOLOGY CO., LTD.',
] as const;

export type THuaweiLteSnapshotSource = 'http' | 'client' | 'manual' | 'snapshot' | 'runtime' | 'executor';
export type THuaweiLteCommandAction =
  | 'refresh'
  | 'set_mobile_dataswitch'
  | 'set_wifi_guest_network'
  | 'reboot'
  | 'clear_traffic'
  | 'send_sms'
  | 'set_net_mode'
  | 'suspend_integration'
  | 'resume_integration';

export type THuaweiLteRawDataKey =
  | 'deviceInformation'
  | 'deviceBasicInformation'
  | 'deviceSignal'
  | 'dialupMobileDataswitch'
  | 'lanHostInfo'
  | 'monitoringCheckNotifications'
  | 'monitoringMonthStatistics'
  | 'monitoringStatus'
  | 'monitoringTrafficStatistics'
  | 'netCurrentPlmn'
  | 'netNetMode'
  | 'smsSmsCount'
  | 'wlanHostList'
  | 'wlanWifiFeatureSwitch'
  | 'wlanWifiGuestNetworkSwitch'
  | 'wlanMultiBasicSettings';

export interface IHuaweiLteValueMap {
  [key: string]: unknown;
}

export interface IHuaweiLteRawData {
  deviceInformation?: IHuaweiLteValueMap;
  deviceBasicInformation?: IHuaweiLteValueMap;
  deviceSignal?: IHuaweiLteValueMap;
  dialupMobileDataswitch?: IHuaweiLteValueMap;
  lanHostInfo?: IHuaweiLteValueMap;
  monitoringCheckNotifications?: IHuaweiLteValueMap;
  monitoringMonthStatistics?: IHuaweiLteValueMap;
  monitoringStatus?: IHuaweiLteValueMap;
  monitoringTrafficStatistics?: IHuaweiLteValueMap;
  netCurrentPlmn?: IHuaweiLteValueMap;
  netNetMode?: IHuaweiLteValueMap;
  smsSmsCount?: IHuaweiLteValueMap;
  wlanHostList?: IHuaweiLteValueMap;
  wlanWifiFeatureSwitch?: IHuaweiLteValueMap;
  wlanWifiGuestNetworkSwitch?: IHuaweiLteValueMap;
  wlanMultiBasicSettings?: IHuaweiLteValueMap;
  resources?: Partial<Record<THuaweiLteRawDataKey, IHuaweiLteValueMap>>;
  errors?: Partial<Record<THuaweiLteRawDataKey, string>>;
  fetchedAt?: string;
}

export interface IHuaweiLteDeviceSnapshotInfo {
  id: string;
  name: string;
  manufacturer: string;
  model?: string;
  serialNumber?: string;
  hardwareVersion?: string;
  softwareVersion?: string;
  host?: string;
  port?: number;
  url?: string;
  ssl?: boolean;
  verifySsl?: boolean;
  macAddresses: string[];
  upnpUdn?: string;
}

export interface IHuaweiLteConnectionSnapshot {
  mobileConnected?: boolean;
  connectionStatus?: string;
  mobileDataEnabled?: boolean;
  wifiEnabled?: boolean;
  wifi24ghzEnabled?: boolean;
  wifi5ghzEnabled?: boolean;
  guestWifiEnabled?: boolean;
  smsStorageFull?: boolean;
}

export interface IHuaweiLteSignalSnapshot {
  values: IHuaweiLteValueMap;
  rssi?: number;
  rsrp?: number;
  rsrq?: number;
  sinr?: number;
  nrrsrp?: number;
  nrrsrq?: number;
  nrsinr?: number;
}

export interface IHuaweiLteTrafficSnapshot {
  currentDownload?: number;
  currentUpload?: number;
  currentDownloadRate?: number;
  currentUploadRate?: number;
  totalDownload?: number;
  totalUpload?: number;
  currentConnectTime?: number;
  totalConnectTime?: number;
  currentMonthDownload?: number;
  currentMonthUpload?: number;
  currentDayUsed?: number;
}

export interface IHuaweiLteSmsSnapshot {
  unread?: number;
  localUnread?: number;
  localInbox?: number;
  localOutbox?: number;
  localDraft?: number;
  localMax?: number;
  simUnread?: number;
  simInbox?: number;
  simOutbox?: number;
  simDraft?: number;
  simMax?: number;
}

export interface IHuaweiLteNetworkSnapshot {
  operatorName?: string;
  operatorCode?: string;
  operatorSearchMode?: string;
  networkMode?: string;
}

export interface IHuaweiLteHostSnapshot {
  id: string;
  macAddress: string;
  hostname?: string;
  ipAddress?: string;
  active: boolean;
  wireless: boolean;
  isLocalDevice?: boolean;
  attributes?: Record<string, unknown>;
}

export interface IHuaweiLteCapabilities {
  localControl: boolean;
  mobileDataSwitch: boolean;
  guestWifiSwitch: boolean;
  reboot: boolean;
  clearTraffic: boolean;
  sendSms: boolean;
  networkModeSelect: boolean;
}

export interface IHuaweiLteSnapshot {
  device: IHuaweiLteDeviceSnapshotInfo;
  connection: IHuaweiLteConnectionSnapshot;
  signal: IHuaweiLteSignalSnapshot;
  traffic: IHuaweiLteTrafficSnapshot;
  sms: IHuaweiLteSmsSnapshot;
  network: IHuaweiLteNetworkSnapshot;
  hosts: IHuaweiLteHostSnapshot[];
  capabilities: IHuaweiLteCapabilities;
  rawData?: IHuaweiLteRawData;
  online: boolean;
  updatedAt: string;
  source: THuaweiLteSnapshotSource;
  suspended?: boolean;
  error?: string;
}

export interface IHuaweiLteCommandRequest {
  action: THuaweiLteCommandAction;
  enabled?: boolean;
  phoneNumbers?: string[];
  message?: string;
  smsIndex?: number;
  sca?: string;
  networkMode?: string;
  networkBand?: string;
  lteBand?: string;
  service?: string;
  target?: Record<string, unknown>;
  data?: Record<string, unknown>;
}

export interface IHuaweiLteCommandExecutor {
  execute(requestArg: IHuaweiLteCommandRequest): Promise<unknown>;
}

export interface IHuaweiLteClientLike {
  getSnapshot?: () => Promise<IHuaweiLteSnapshot | Partial<IHuaweiLteRawData>>;
  getRawData?: () => Promise<Partial<IHuaweiLteRawData>>;
  getResource?: (endpointArg: string) => Promise<IHuaweiLteValueMap>;
  execute?: (requestArg: IHuaweiLteCommandRequest) => Promise<unknown>;
  destroy?: () => Promise<void>;
}

export interface IHuaweiLteConfig {
  url?: string;
  host?: string;
  port?: number;
  ssl?: boolean;
  verifySsl?: boolean;
  username?: string;
  password?: string;
  unauthenticatedMode?: boolean;
  trackWiredClients?: boolean;
  recipients?: string[];
  timeoutMs?: number;
  name?: string;
  manufacturer?: string;
  model?: string;
  uniqueId?: string;
  serialNumber?: string;
  macAddress?: string;
  macAddresses?: string[];
  upnpUdn?: string;
  snapshot?: IHuaweiLteSnapshot;
  rawData?: Partial<IHuaweiLteRawData>;
  client?: IHuaweiLteClientLike;
  commandExecutor?: IHuaweiLteCommandExecutor;
  online?: boolean;
}

export interface IHomeAssistantHuaweiLteConfig extends IHuaweiLteConfig {}

export interface IHuaweiLteSsdpRecord {
  st?: string;
  nt?: string;
  usn?: string;
  location?: string;
  ssdpLocation?: string;
  deviceType?: string;
  manufacturer?: string;
  friendlyName?: string;
  modelName?: string;
  serialNumber?: string;
  presentationUrl?: string;
  udn?: string;
  host?: string;
  headers?: Record<string, string | undefined>;
  ssdpHeaders?: Record<string, string | undefined>;
  upnp?: {
    deviceType?: string;
    manufacturer?: string;
    friendlyName?: string;
    modelName?: string;
    serialNumber?: string;
    serial?: string;
    presentationURL?: string;
    presentationUrl?: string;
    udn?: string;
    UDN?: string;
    [key: string]: unknown;
  };
  metadata?: Record<string, unknown>;
  [key: string]: unknown;
}

export interface IHuaweiLteManualEntry extends Partial<IHuaweiLteConfig> {
  id?: string;
  metadata?: Record<string, unknown>;
}

export interface IHuaweiLteRefreshResult {
  success: boolean;
  snapshot: IHuaweiLteSnapshot;
  error?: string;
  data?: unknown;
}
