import * as plugins from '../../plugins.js';
import type { IIntegrationEntity } from '../../core/types.js';
import type { IWolfSmartsetSystem } from './wolf_smartset.types.js';

export class WolfSmartsetMapper {
  public static toDevices(systemsArg: IWolfSmartsetSystem[]): plugins.shxInterfaces.data.IDeviceDefinition[] {
    return systemsArg.map((systemArg) => ({
      id: `wolf_smartset.system.${systemArg.id}`,
      integrationDomain: 'wolf_smartset',
      name: systemArg.name,
      protocol: 'cloud',
      manufacturer: 'Wolf',
      model: systemArg.model || 'Smartset system',
      serialNumber: systemArg.serialNumber,
      online: systemArg.available,
      features: [
        { id: 'current_temperature', capability: 'sensor', name: 'Current temperature', readable: true, writable: false, unit: 'C' },
        { id: 'target_temperature', capability: 'climate', name: 'Target temperature', readable: true, writable: true, unit: 'C' },
      ],
      state: [
        { featureId: 'current_temperature', value: systemArg.currentTemperature ?? null, updatedAt: new Date().toISOString() },
        { featureId: 'target_temperature', value: systemArg.targetTemperature ?? null, updatedAt: new Date().toISOString() },
      ],
    }));
  }

  public static toEntities(systemsArg: IWolfSmartsetSystem[]): IIntegrationEntity[] {
    return systemsArg.map((systemArg) => ({
      id: `climate.${this.slug(systemArg.name)}`,
      uniqueId: `wolf_smartset_${systemArg.id}`,
      integrationDomain: 'wolf_smartset',
      deviceId: `wolf_smartset.system.${systemArg.id}`,
      platform: 'climate',
      name: systemArg.name,
      state: systemArg.targetTemperature ?? null,
      attributes: {
        currentTemperature: systemArg.currentTemperature,
      },
      available: systemArg.available,
    }));
  }

  private static slug(valueArg: string) {
    return valueArg.toLowerCase().replace(/[^a-z0-9]+/g, '_').replace(/^_+|_+$/g, '') || 'wolf_system';
  }
}
