import { expect, tap } from '@git.zone/tstest/tapbundle';
import { EsceaClient, EsceaConfigFlow, EsceaIntegration, EsceaMapper, HomeAssistantEsceaIntegration, createEsceaDiscoveryDescriptor, esceaProfile, type IEsceaSnapshot } from '../../ts/integrations/escea/index.js';

const rawData = {
  device: {
    id: 'escea-controller-1',
    name: 'Living Room Fireplace',
    manufacturer: 'Escea',
    model: 'Escea Fireplace',
    serialNumber: 'ESCEA-001',
  },
  entities: [
    { id: 'fireplace', name: 'Fireplace', platform: 'climate', state: { hvacMode: 'heat', currentTemperature: 19, targetTemperature: 22, fanMode: 'auto' }, writable: true, unit: 'C' },
  ],
};

tap.test('matches manual Escea candidates and creates config flow output', async () => {
  const descriptor = createEsceaDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'escea-manual-match');
  const result = await matcher!.matches({ name: 'Escea Fireplace', metadata: { rawData } }, {});

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

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

  const done = await (await new EsceaConfigFlow().start(result.candidate!, {})).submit!({});
  expect(done.kind).toEqual('done');
  expect(done.config?.name).toEqual('Escea Fireplace');
  expect(done.config?.rawData).toEqual(rawData);
});

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

  expect(snapshot.online).toBeTrue();
  expect(snapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual('escea');
  expect(devices[0].manufacturer).toEqual('Escea');
  expect(entities.length).toEqual(1);
  expect(entities[0].platform).toEqual('climate');
});

tap.test('exposes Escea runtime services, HA alias, and unsupported control without executor', async () => {
  const alias = new HomeAssistantEsceaIntegration();
  expect(alias instanceof EsceaIntegration).toBeTrue();
  expect(alias.domain).toEqual('escea');
  expect(esceaProfile.metadata.iotClass).toEqual('local_push');
  expect(esceaProfile.metadata.requirements).toEqual(['pescea==1.0.12']);
  expect(esceaProfile.metadata.configFlow).toBeTrue();

  const runtime = await new EsceaIntegration().setup({ name: 'Escea Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'escea', service: 'status', target: {} });
  const snapshot = status.data as IEsceaSnapshot;

  expect(status.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect((await runtime.callService!({ domain: 'escea', service: 'snapshot', target: {} })).success).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual('Living Room Fireplace');

  const controlCommand = await runtime.callService!({ domain: 'climate', service: 'set_fan_mode', target: {}, data: { fanMode: 'auto' } });
  expect(controlCommand.success).toBeFalse();
  expect(controlCommand.error?.includes('requires an injected client.execute() or commandExecutor')).toBeTrue();
  await runtime.destroy();
});

export default tap.start();
