import { expect, tap } from '@git.zone/tstest/tapbundle';
import { HomeAssistantMqttEventstreamIntegration, MqttEventstreamClient, MqttEventstreamConfigFlow, MqttEventstreamIntegration, MqttEventstreamMapper, createMqttEventstreamDiscoveryDescriptor, mqttEventstreamProfile, type IMqttEventstreamSnapshot, type TMqttEventstreamRawData } from '../../ts/integrations/mqtt_eventstream/index.js';

const rawData: TMqttEventstreamRawData = {
  device: {
    id: 'mqtt_eventstream-device-1',
    name: "MQTT Eventstream Device",
    manufacturer: "MQTT Eventstream",
    model: "MQTT Eventstream local integration",
    serialNumber: 'mqtt_eventstream-serial-1',
  },
  entities: [
    { id: 'status', name: 'Status', platform: "sensor", state: true, attributes: { domain: "mqtt_eventstream" } },
  ],
  online: true,
  updatedAt: '2026-01-01T00:00:00.000Z',
  source: 'manual',
};

tap.test('matches manual MQTT Eventstream candidates and creates config flow output', async () => {
  const descriptor = createMqttEventstreamDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'mqtt_eventstream-manual-match');
  const result = await matcher!.matches({ source: 'manual', id: 'mqtt_eventstream-device-1', name: "MQTT Eventstream Device", metadata: { rawData } }, {});

  expect(result.matched).toBeTrue();
  expect(result.candidate?.integrationDomain).toEqual("mqtt_eventstream");

  const validation = await descriptor.getValidators()[0].validate(result.candidate!, {});
  expect(validation.matched).toBeTrue();

  const done = await (await new MqttEventstreamConfigFlow().start(result.candidate!, {})).submit!({});
  expect(done.kind).toEqual('done');
  expect(done.config?.uniqueId).toEqual('mqtt_eventstream-device-1');
  expect(done.config?.rawData).toEqual(rawData);
});

tap.test('maps MQTT Eventstream raw snapshots to runtime devices and entities', async () => {
  const client = new MqttEventstreamClient({ name: "MQTT Eventstream Runtime", rawData });
  const snapshot = await client.getSnapshot();
  const mappedSnapshot = MqttEventstreamMapper.toSnapshotFromRaw({ name: "MQTT Eventstream Runtime" }, rawData);
  const devices = MqttEventstreamMapper.toDevices(mappedSnapshot);
  const entities = MqttEventstreamMapper.toEntities(mappedSnapshot);

  expect(snapshot.online).toBeTrue();
  expect(mappedSnapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual("mqtt_eventstream");
  expect(devices[0].manufacturer).toEqual("MQTT Eventstream");
  expect(entities.some((entityArg) => entityArg.integrationDomain === "mqtt_eventstream" && entityArg.platform === "sensor")).toBeTrue();
});

tap.test('documents MQTT Eventstream broker blocker and refuses generic HTTP fallback', async () => {
  const metadata = mqttEventstreamProfile.metadata as {
    runtime: { blocker: string; controls: boolean };
    localApi: { status: string; explicitUnsupported: string[] };
  };
  const originalFetch = globalThis.fetch;
  let fetchCalls = 0;
  globalThis.fetch = (async () => {
    fetchCalls += 1;
    throw new Error('HTTP fallback must not be used for mqtt_eventstream');
  }) as typeof fetch;

  try {
    const snapshot = await new MqttEventstreamClient({ name: 'MQTT Eventstream Live', host: '127.0.0.1', path: '/events', transport: 'http' }).getSnapshot(true);
    expect(snapshot.online).toBeFalse();
    expect(snapshot.error!).toContain('mqtt.async_publish/mqtt.async_subscribe');

    const runtime = await new MqttEventstreamIntegration().setup({ name: 'MQTT Eventstream Runtime', host: '127.0.0.1', path: '/events', transport: 'http' }, {});
    const status = await runtime.callService!({ domain: 'mqtt_eventstream', service: 'status', target: {} });
    const runtimeSnapshot = status.data as IMqttEventstreamSnapshot;
    expect(runtimeSnapshot.online).toBeFalse();
    expect(runtimeSnapshot.error!).toContain('mqtt.async_publish/mqtt.async_subscribe');
    await runtime.destroy();

    expect(fetchCalls).toEqual(0);
  } finally {
    globalThis.fetch = originalFetch;
  }

  expect(metadata.runtime.controls).toBeFalse();
  expect(metadata.runtime.blocker).toContain('MQTT Eventstream live operation requires');
  expect(metadata.localApi.status).toContain('MQTT integration');
  expect(metadata.localApi.explicitUnsupported.some((itemArg) => itemArg.includes('mqtt.async_publish') && itemArg.includes('mqtt.async_subscribe'))).toBeTrue();
});

tap.test('exposes MQTT Eventstream runtime, HA alias, and unsupported control without executor', async () => {
  const integration = new MqttEventstreamIntegration();
  const alias = new HomeAssistantMqttEventstreamIntegration();
  expect(alias instanceof MqttEventstreamIntegration).toBeTrue();
  expect(alias.domain).toEqual("mqtt_eventstream");
  expect(integration.status).toEqual("read-only-runtime");
  expect(mqttEventstreamProfile.metadata.configFlow).toEqual(false);
  expect(mqttEventstreamProfile.metadata.requirements).toEqual([]);

  const runtime = await integration.setup({ name: "MQTT Eventstream Runtime", rawData }, {});
  const statusResult = await runtime.callService!({ domain: "mqtt_eventstream", service: 'status', target: {} });
  const refresh = await runtime.callService!({ domain: "mqtt_eventstream", service: 'refresh', target: {} });
  const snapshot = statusResult.data as IMqttEventstreamSnapshot;

  expect(statusResult.success).toBeTrue();
  expect(refresh.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual("MQTT Eventstream Device");

  const command = await runtime.callService!({ domain: "mqtt_eventstream", service: mqttEventstreamProfile.controlServices?.[0] || 'turn_on', target: {} });
  expect(command.success).toBeFalse();
  expect(command.error!).toContain('requires an injected client.execute() or commandExecutor');
  await runtime.destroy();
});

export default tap.start();
