import * as plugins from '../../plugins.js';
import type { IIntegrationEntity, IIntegrationEvent } from '../../core/types.js';
import type { IHueEvent, IHueResourceResponse } from './hue.types.js';

export class HueMapper {
  public static toDevices(resourcesArg: IHueResourceResponse): plugins.shxInterfaces.data.IDeviceDefinition[] {
    const devices: plugins.shxInterfaces.data.IDeviceDefinition[] = [];

    if (resourcesArg.bridge) {
      devices.push({
        id: `hue.bridge.${resourcesArg.bridge.id}`,
        integrationDomain: 'hue',
        name: resourcesArg.bridge.metadata?.name || 'Hue Bridge',
        protocol: 'hue',
        manufacturer: resourcesArg.bridge.productData?.manufacturerName || 'Signify',
        model: resourcesArg.bridge.productData?.productName || resourcesArg.bridge.productData?.modelId,
        online: true,
        features: [
          { id: 'connectivity', capability: 'sensor', name: 'Connectivity', readable: true, writable: false },
        ],
        state: [
          { featureId: 'connectivity', value: 'online', updatedAt: new Date().toISOString() },
        ],
      });
    }

    for (const light of resourcesArg.lights) {
      devices.push({
        id: `hue.light.${light.id}`,
        integrationDomain: 'hue',
        name: light.metadata?.name || `Hue Light ${light.id}`,
        protocol: 'hue',
        manufacturer: 'Signify',
        model: light.metadata?.archetype,
        online: true,
        features: [
          { id: 'on', capability: 'light', name: 'Power', readable: true, writable: true },
          { id: 'brightness', capability: 'light', name: 'Brightness', readable: true, writable: true, unit: '%' },
        ],
        state: [
          { featureId: 'on', value: light.on?.on ?? false, updatedAt: new Date().toISOString() },
          { featureId: 'brightness', value: light.dimming?.brightness ?? null, updatedAt: new Date().toISOString() },
        ],
        metadata: {
          hueOwnerRid: light.owner?.rid,
        },
      });
    }

    return devices;
  }

  public static toEntities(resourcesArg: IHueResourceResponse): IIntegrationEntity[] {
    return resourcesArg.lights.map((lightArg) => ({
      id: `light.${this.slug(lightArg.metadata?.name || lightArg.id)}`,
      uniqueId: `hue_light_${lightArg.id}`,
      integrationDomain: 'hue',
      deviceId: `hue.light.${lightArg.id}`,
      platform: 'light',
      name: lightArg.metadata?.name || `Hue Light ${lightArg.id}`,
      state: lightArg.on?.on ? 'on' : 'off',
      attributes: {
        brightness: lightArg.dimming?.brightness,
      },
      available: true,
    }));
  }

  public static toIntegrationEvent(eventArg: IHueEvent): IIntegrationEvent {
    return {
      type: 'state_changed',
      integrationDomain: 'hue',
      data: eventArg,
      timestamp: Date.now(),
    };
  }

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