import type { IDsmrSensorDescription, TDsmrProtocol, TDsmrVersion } from './dsmr.types.js';

export const dsmrProtocol: TDsmrProtocol = 'dsmr_protocol';
export const dsmrRfxtrxProtocol: TDsmrProtocol = 'rfxtrx_dsmr_protocol';
export const dsmrVersions: TDsmrVersion[] = ['2.2', '4', '5', '5B', '5L', '5S', 'Q3D', '5EONHU'];

export const dsmrObisReferenceByCode: Record<string, string> = {
  '0-0:1.0.0': 'P1_MESSAGE_TIMESTAMP',
  '0-0:96.1.0': 'EQUIPMENT_IDENTIFIER',
  '0-0:96.1.1': 'EQUIPMENT_IDENTIFIER',
  '0-0:96.1.4': 'BELGIUM_EQUIPMENT_IDENTIFIER',
  '1-3:0.2.8': 'DSMR_VERSION',
  '1-0:1.7.0': 'CURRENT_ELECTRICITY_USAGE',
  '1-0:2.7.0': 'CURRENT_ELECTRICITY_DELIVERY',
  '0-0:96.14.0': 'ELECTRICITY_ACTIVE_TARIFF',
  '1-0:1.8.1': 'ELECTRICITY_USED_TARIFF_1',
  '1-0:1.8.2': 'ELECTRICITY_USED_TARIFF_2',
  '1-0:1.8.3': 'ELECTRICITY_USED_TARIFF_3',
  '1-0:1.8.4': 'ELECTRICITY_USED_TARIFF_4',
  '1-0:2.8.1': 'ELECTRICITY_DELIVERED_TARIFF_1',
  '1-0:2.8.2': 'ELECTRICITY_DELIVERED_TARIFF_2',
  '1-0:2.8.3': 'ELECTRICITY_DELIVERED_TARIFF_3',
  '1-0:2.8.4': 'ELECTRICITY_DELIVERED_TARIFF_4',
  '1-0:15.8.0': 'ELECTRICITY_IMPORTED_TOTAL',
  '1-0:16.8.0': 'ELECTRICITY_EXPORTED_TOTAL',
  '1-0:21.7.0': 'INSTANTANEOUS_ACTIVE_POWER_L1_POSITIVE',
  '1-0:41.7.0': 'INSTANTANEOUS_ACTIVE_POWER_L2_POSITIVE',
  '1-0:61.7.0': 'INSTANTANEOUS_ACTIVE_POWER_L3_POSITIVE',
  '1-0:22.7.0': 'INSTANTANEOUS_ACTIVE_POWER_L1_NEGATIVE',
  '1-0:42.7.0': 'INSTANTANEOUS_ACTIVE_POWER_L2_NEGATIVE',
  '1-0:62.7.0': 'INSTANTANEOUS_ACTIVE_POWER_L3_NEGATIVE',
  '1-0:32.7.0': 'INSTANTANEOUS_VOLTAGE_L1',
  '1-0:52.7.0': 'INSTANTANEOUS_VOLTAGE_L2',
  '1-0:72.7.0': 'INSTANTANEOUS_VOLTAGE_L3',
  '1-0:31.7.0': 'INSTANTANEOUS_CURRENT_L1',
  '1-0:51.7.0': 'INSTANTANEOUS_CURRENT_L2',
  '1-0:71.7.0': 'INSTANTANEOUS_CURRENT_L3',
  '0-0:96.7.21': 'SHORT_POWER_FAILURE_COUNT',
  '0-0:96.7.9': 'LONG_POWER_FAILURE_COUNT',
  '0-0:96.13.0': 'TEXT_MESSAGE',
  '0-1:96.1.0': 'EQUIPMENT_IDENTIFIER_GAS',
  '0-1:96.1.1': 'EQUIPMENT_IDENTIFIER_GAS',
  '0-1:24.1.0': 'MBUS_DEVICE_TYPE',
  '0-1:24.2.1': 'HOURLY_GAS_METER_READING',
  '0-1:24.3.0': 'GAS_METER_READING',
};

export const dsmrSensorDescriptions: IDsmrSensorDescription[] = [
  { key: 'timestamp', name: 'Timestamp', obisReference: 'P1_MESSAGE_TIMESTAMP', obisCodes: ['0-0:1.0.0'], deviceType: 'electricity', deviceClass: 'timestamp', entityCategory: 'diagnostic', enabledByDefault: false },
  { key: 'current_electricity_usage', name: 'Power consumption', obisReference: 'CURRENT_ELECTRICITY_USAGE', obisCodes: ['1-0:1.7.0'], deviceType: 'electricity', deviceClass: 'power', stateClass: 'measurement' },
  { key: 'current_electricity_delivery', name: 'Power production', obisReference: 'CURRENT_ELECTRICITY_DELIVERY', obisCodes: ['1-0:2.7.0'], deviceType: 'electricity', deviceClass: 'power', stateClass: 'measurement' },
  { key: 'electricity_active_tariff', name: 'Active tariff', obisReference: 'ELECTRICITY_ACTIVE_TARIFF', obisCodes: ['0-0:96.14.0'], dsmrVersions: ['2.2', '4', '5', '5B', '5L', '5EONHU'], deviceType: 'electricity', deviceClass: 'enum', translateTariff: true },
  { key: 'electricity_used_tariff_1', name: 'Energy consumption (tarif 1)', obisReference: 'ELECTRICITY_USED_TARIFF_1', obisCodes: ['1-0:1.8.1'], dsmrVersions: ['2.2', '4', '5', '5B', '5L', '5EONHU'], deviceType: 'electricity', deviceClass: 'energy', stateClass: 'total_increasing' },
  { key: 'electricity_used_tariff_2', name: 'Energy consumption (tarif 2)', obisReference: 'ELECTRICITY_USED_TARIFF_2', obisCodes: ['1-0:1.8.2'], dsmrVersions: ['2.2', '4', '5', '5B', '5L', '5EONHU'], deviceType: 'electricity', deviceClass: 'energy', stateClass: 'total_increasing' },
  { key: 'electricity_used_tariff_3', name: 'Energy consumption (tarif 3)', obisReference: 'ELECTRICITY_USED_TARIFF_3', obisCodes: ['1-0:1.8.3'], dsmrVersions: ['5EONHU'], deviceType: 'electricity', deviceClass: 'energy', stateClass: 'total_increasing' },
  { key: 'electricity_used_tariff_4', name: 'Energy consumption (tarif 4)', obisReference: 'ELECTRICITY_USED_TARIFF_4', obisCodes: ['1-0:1.8.4'], dsmrVersions: ['5EONHU'], deviceType: 'electricity', deviceClass: 'energy', stateClass: 'total_increasing' },
  { key: 'electricity_delivered_tariff_1', name: 'Energy production (tarif 1)', obisReference: 'ELECTRICITY_DELIVERED_TARIFF_1', obisCodes: ['1-0:2.8.1'], dsmrVersions: ['2.2', '4', '5', '5B', '5L', '5EONHU'], deviceType: 'electricity', deviceClass: 'energy', stateClass: 'total_increasing' },
  { key: 'electricity_delivered_tariff_2', name: 'Energy production (tarif 2)', obisReference: 'ELECTRICITY_DELIVERED_TARIFF_2', obisCodes: ['1-0:2.8.2'], dsmrVersions: ['2.2', '4', '5', '5B', '5L', '5EONHU'], deviceType: 'electricity', deviceClass: 'energy', stateClass: 'total_increasing' },
  { key: 'electricity_delivered_tariff_3', name: 'Energy production (tarif 3)', obisReference: 'ELECTRICITY_DELIVERED_TARIFF_3', obisCodes: ['1-0:2.8.3'], dsmrVersions: ['5EONHU'], deviceType: 'electricity', deviceClass: 'energy', stateClass: 'total_increasing' },
  { key: 'electricity_delivered_tariff_4', name: 'Energy production (tarif 4)', obisReference: 'ELECTRICITY_DELIVERED_TARIFF_4', obisCodes: ['1-0:2.8.4'], dsmrVersions: ['5EONHU'], deviceType: 'electricity', deviceClass: 'energy', stateClass: 'total_increasing' },
  { key: 'electricity_imported_total', name: 'Energy consumption (total)', obisReference: 'ELECTRICITY_IMPORTED_TOTAL', obisCodes: ['1-0:15.8.0'], dsmrVersions: ['5L', '5S', 'Q3D', '5EONHU'], deviceType: 'electricity', deviceClass: 'energy', stateClass: 'total_increasing' },
  { key: 'electricity_exported_total', name: 'Energy production (total)', obisReference: 'ELECTRICITY_EXPORTED_TOTAL', obisCodes: ['1-0:16.8.0'], dsmrVersions: ['5L', '5S', 'Q3D', '5EONHU'], deviceType: 'electricity', deviceClass: 'energy', stateClass: 'total_increasing' },
  { key: 'instantaneous_active_power_l1_positive', name: 'Power consumption phase L1', obisReference: 'INSTANTANEOUS_ACTIVE_POWER_L1_POSITIVE', obisCodes: ['1-0:21.7.0'], deviceType: 'electricity', deviceClass: 'power', stateClass: 'measurement', enabledByDefault: false },
  { key: 'instantaneous_active_power_l2_positive', name: 'Power consumption phase L2', obisReference: 'INSTANTANEOUS_ACTIVE_POWER_L2_POSITIVE', obisCodes: ['1-0:41.7.0'], deviceType: 'electricity', deviceClass: 'power', stateClass: 'measurement', enabledByDefault: false },
  { key: 'instantaneous_active_power_l3_positive', name: 'Power consumption phase L3', obisReference: 'INSTANTANEOUS_ACTIVE_POWER_L3_POSITIVE', obisCodes: ['1-0:61.7.0'], deviceType: 'electricity', deviceClass: 'power', stateClass: 'measurement', enabledByDefault: false },
  { key: 'instantaneous_active_power_l1_negative', name: 'Power production phase L1', obisReference: 'INSTANTANEOUS_ACTIVE_POWER_L1_NEGATIVE', obisCodes: ['1-0:22.7.0'], deviceType: 'electricity', deviceClass: 'power', stateClass: 'measurement', enabledByDefault: false },
  { key: 'instantaneous_active_power_l2_negative', name: 'Power production phase L2', obisReference: 'INSTANTANEOUS_ACTIVE_POWER_L2_NEGATIVE', obisCodes: ['1-0:42.7.0'], deviceType: 'electricity', deviceClass: 'power', stateClass: 'measurement', enabledByDefault: false },
  { key: 'instantaneous_active_power_l3_negative', name: 'Power production phase L3', obisReference: 'INSTANTANEOUS_ACTIVE_POWER_L3_NEGATIVE', obisCodes: ['1-0:62.7.0'], deviceType: 'electricity', deviceClass: 'power', stateClass: 'measurement', enabledByDefault: false },
  { key: 'instantaneous_voltage_l1', name: 'Voltage phase L1', obisReference: 'INSTANTANEOUS_VOLTAGE_L1', obisCodes: ['1-0:32.7.0'], deviceType: 'electricity', deviceClass: 'voltage', stateClass: 'measurement', entityCategory: 'diagnostic', enabledByDefault: false },
  { key: 'instantaneous_voltage_l2', name: 'Voltage phase L2', obisReference: 'INSTANTANEOUS_VOLTAGE_L2', obisCodes: ['1-0:52.7.0'], deviceType: 'electricity', deviceClass: 'voltage', stateClass: 'measurement', entityCategory: 'diagnostic', enabledByDefault: false },
  { key: 'instantaneous_voltage_l3', name: 'Voltage phase L3', obisReference: 'INSTANTANEOUS_VOLTAGE_L3', obisCodes: ['1-0:72.7.0'], deviceType: 'electricity', deviceClass: 'voltage', stateClass: 'measurement', entityCategory: 'diagnostic', enabledByDefault: false },
  { key: 'instantaneous_current_l1', name: 'Current phase L1', obisReference: 'INSTANTANEOUS_CURRENT_L1', obisCodes: ['1-0:31.7.0'], deviceType: 'electricity', deviceClass: 'current', stateClass: 'measurement', entityCategory: 'diagnostic', enabledByDefault: false },
  { key: 'instantaneous_current_l2', name: 'Current phase L2', obisReference: 'INSTANTANEOUS_CURRENT_L2', obisCodes: ['1-0:51.7.0'], deviceType: 'electricity', deviceClass: 'current', stateClass: 'measurement', entityCategory: 'diagnostic', enabledByDefault: false },
  { key: 'instantaneous_current_l3', name: 'Current phase L3', obisReference: 'INSTANTANEOUS_CURRENT_L3', obisCodes: ['1-0:71.7.0'], deviceType: 'electricity', deviceClass: 'current', stateClass: 'measurement', entityCategory: 'diagnostic', enabledByDefault: false },
  { key: 'short_power_failure_count', name: 'Short power failure count', obisReference: 'SHORT_POWER_FAILURE_COUNT', obisCodes: ['0-0:96.7.21'], dsmrVersions: ['2.2', '4', '5', '5L'], deviceType: 'electricity', stateClass: 'total_increasing', entityCategory: 'diagnostic', enabledByDefault: false },
  { key: 'long_power_failure_count', name: 'Long power failure count', obisReference: 'LONG_POWER_FAILURE_COUNT', obisCodes: ['0-0:96.7.9'], dsmrVersions: ['2.2', '4', '5', '5L'], deviceType: 'electricity', stateClass: 'total_increasing', entityCategory: 'diagnostic', enabledByDefault: false },
  { key: 'hourly_gas_meter_reading', name: 'Gas consumption', obisReference: 'HOURLY_GAS_METER_READING', obisCodes: ['0-1:24.2.1'], obisCodePatterns: [/^0-\d+:24\.2\.1$/], dsmrVersions: ['4', '5', '5B', '5L'], deviceType: 'gas', deviceClass: 'gas', stateClass: 'total_increasing' },
  { key: 'gas_meter_reading', name: 'Gas consumption', obisReference: 'GAS_METER_READING', obisCodes: ['0-1:24.3.0'], obisCodePatterns: [/^0-\d+:24\.3\.0$/], dsmrVersions: ['2.2'], deviceType: 'gas', deviceClass: 'gas', stateClass: 'total_increasing' },
];
