import { expect, tap } from '@git.zone/tstest/tapbundle';
import { HisenseAehw4a1Client, HisenseAehw4a1ConfigFlow, HisenseAehw4a1Integration, HisenseAehw4a1Mapper, HomeAssistantHisenseAehw4a1Integration, createHisenseAehw4a1DiscoveryDescriptor, hisenseAehw4a1Profile, type IHisenseAehw4a1Snapshot, type THisenseAehw4a1RawData } from '../../ts/integrations/hisense_aehw4a1/index.js';

const rawData: THisenseAehw4a1RawData = {
  status: {
    run_status: '1',
    mode_status: '0010',
    wind_status: '00000110',
    up_down: '1',
    left_right: '0',
    temperature_Fahrenheit: '0',
    indoor_temperature_status: '00010101',
    indoor_temperature_setting: '00010011',
    efficient: '0',
    low_electricity: '0',
    sleep_status: '0000000',
  },
};

tap.test('matches manual Hisense AEH-W4A1 candidates and creates config flow output', async () => {
  const descriptor = createHisenseAehw4a1DiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'hisense_aehw4a1-manual-match');
  const result = await matcher!.matches({ source: 'manual', id: '192.0.2.44', host: '192.0.2.44', name: 'Bedroom AC', metadata: { rawData } }, {});

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

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

  const done = await (await new HisenseAehw4a1ConfigFlow().start(result.candidate!, {})).submit!({});
  expect(done.kind).toEqual('done');
  expect(done.config?.host).toEqual('192.0.2.44');
  expect(done.config?.rawData).toEqual(rawData);
});

tap.test('maps Hisense AEH-W4A1 raw snapshots to runtime devices and entities', async () => {
  const client = new HisenseAehw4a1Client({ name: 'Bedroom AC', host: '192.0.2.44', rawData });
  const snapshot = await client.getSnapshot();
  const mappedSnapshot = HisenseAehw4a1Mapper.toSnapshotFromRaw({ name: 'Bedroom AC', host: '192.0.2.44' }, rawData);
  const devices = HisenseAehw4a1Mapper.toDevices(mappedSnapshot);
  const entities = HisenseAehw4a1Mapper.toEntities(mappedSnapshot);

  expect(snapshot.online).toBeTrue();
  expect(mappedSnapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual('hisense_aehw4a1');
  expect(devices[0].manufacturer).toEqual('Hisense');
  expect(entities.some((entityArg) => entityArg.id === 'climate.bedroom_ac_climate')).toBeTrue();
  expect(entities[0].attributes?.currentTemperature).toEqual(21);
});

tap.test('exposes Hisense AEH-W4A1 read-only runtime, HA alias, and unsupported control', async () => {
  const integration = new HisenseAehw4a1Integration();
  const alias = new HomeAssistantHisenseAehw4a1Integration();
  expect(alias instanceof HisenseAehw4a1Integration).toBeTrue();
  expect(alias.domain).toEqual('hisense_aehw4a1');
  expect(integration.status).toEqual('read-only-runtime');
  expect(hisenseAehw4a1Profile.metadata.configFlow).toEqual(true);
  expect(hisenseAehw4a1Profile.metadata.requirements).toEqual(['pyaehw4a1==0.3.9']);
  expect(hisenseAehw4a1Profile.metadata.qualityScale).toBeUndefined();

  const runtime = await integration.setup({ name: 'Bedroom AC', host: '192.0.2.44', rawData }, {});
  const status = await runtime.callService!({ domain: 'hisense_aehw4a1', service: 'status', target: {} });
  const refresh = await runtime.callService!({ domain: 'hisense_aehw4a1', service: 'refresh', target: {} });
  const snapshot = status.data as IHisenseAehw4a1Snapshot;

  expect(status.success).toBeTrue();
  expect(refresh.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual('Bedroom AC');

  const command = await runtime.callService!({ domain: 'climate', service: 'set_temperature', target: { entityId: 'climate.bedroom_ac_climate' }, data: { temperature: 20 } });
  expect(command.success).toBeFalse();
  expect(command.error!).toContain('requires an injected client.execute() or commandExecutor');
  await runtime.destroy();
});

export default tap.start();
