import { expect, tap } from '@git.zone/tstest/tapbundle';
import { EurotronicCometblueClient, EurotronicCometblueConfigFlow, EurotronicCometblueIntegration, EurotronicCometblueMapper, HomeAssistantEurotronicCometblueIntegration, createEurotronicCometblueDiscoveryDescriptor, eurotronicCometblueProfile, type IEurotronicCometblueSnapshot } from '../../ts/integrations/eurotronic_cometblue/index.js';

const rawData = {
  device: {
    id: 'cometblue-aabbccddeeff',
    name: 'Comet Blue AA:BB',
    manufacturer: 'Eurotronic',
    model: 'Comet Blue',
    serialNumber: 'AA:BB:CC:DD:EE:FF',
    protocol: 'local',
  },
  entities: [
    { id: 'thermostat', name: 'Thermostat', platform: 'climate', state: 'auto', writable: true, attributes: { currentTemperature: 21.5, targetTemperature: 20, hvacModes: ['auto', 'heat', 'off'], presetMode: 'comfort' } },
    { id: 'battery', name: 'Battery', platform: 'sensor', state: 86, unit: '%', deviceClass: 'battery' },
    { id: 'sync_time', name: 'Sync time', platform: 'button', state: 'available', writable: true },
  ],
};

tap.test('matches manual Eurotronic Comet Blue candidates and creates config flow output', async () => {
  const descriptor = createEurotronicCometblueDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'eurotronic_cometblue-manual-match');
  const result = await matcher!.matches({ id: 'AA:BB:CC:DD:EE:FF', name: 'Comet Blue AA:BB', metadata: { address: 'AA:BB:CC:DD:EE:FF', pin: '000000', rawData } }, {});

  expect(result.matched).toBeTrue();
  expect(result.candidate?.integrationDomain).toEqual('eurotronic_cometblue');

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

  const done = await (await new EurotronicCometblueConfigFlow().start(result.candidate!, {})).submit!({});
  expect(done.kind).toEqual('done');
  expect(done.config?.uniqueId).toEqual('AA:BB:CC:DD:EE:FF');
  expect(done.config?.metadata?.pin).toEqual('000000');
});

tap.test('maps Eurotronic Comet Blue raw snapshots to runtime devices and entities', async () => {
  const client = new EurotronicCometblueClient({ name: 'Comet Blue Runtime', rawData });
  const snapshot = await client.getSnapshot();
  const devices = EurotronicCometblueMapper.toDevices(snapshot);
  const entities = EurotronicCometblueMapper.toEntities(snapshot);

  expect(snapshot.online).toBeTrue();
  expect(snapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual('eurotronic_cometblue');
  expect(devices[0].manufacturer).toEqual('Eurotronic');
  expect(entities.length).toEqual(3);
});

tap.test('exposes Eurotronic Comet Blue runtime, HA alias, and unsupported control', async () => {
  const integration = new EurotronicCometblueIntegration();
  const alias = new HomeAssistantEurotronicCometblueIntegration();
  expect(alias.domain).toEqual('eurotronic_cometblue');
  expect(integration.status).toEqual('control-runtime');
  expect(eurotronicCometblueProfile.metadata.qualityScale).toEqual('bronze');
  expect(eurotronicCometblueProfile.metadata.requirements).toEqual(['eurotronic-cometblue-ha==1.4.0']);

  const runtime = await integration.setup({ name: 'Comet Blue Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'eurotronic_cometblue', service: 'status', target: {} });
  const snapshot = status.data as IEurotronicCometblueSnapshot;

  expect(status.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual('Comet Blue AA:BB');

  const command = await runtime.callService!({ domain: 'climate', service: 'set_temperature', target: {}, data: { temperature: 20.5 } });
  expect(command.success).toBeFalse();
  expect(Boolean(command.error)).toBeTrue();
  await runtime.destroy();
});

export default tap.start();
