import { expect, tap } from '@git.zone/tstest/tapbundle';
import { FlicClient, FlicConfigFlow, FlicIntegration, FlicMapper, HomeAssistantFlicIntegration, createFlicDiscoveryDescriptor, flicProfile, type IFlicSnapshot, type TFlicRawData } from '../../ts/integrations/flic/index.js';

const rawData: TFlicRawData = {
  host: 'localhost',
  port: 5551,
  buttons: [
    { address: '80:E4:DA:70:01:02', name: 'Kitchen Flic', is_on: true, click_type: 'single', queued_time: 0 },
    { address: '80:E4:DA:70:03:04', name: 'Hall Flic', is_on: false, click_type: 'hold', queued_time: 1 },
  ],
};

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

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

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

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

tap.test('maps Flic raw snapshots to devices and entities', async () => {
  const client = new FlicClient({ rawData, ignoredClickTypes: ['double'] });
  const snapshot = await client.getSnapshot();
  const devices = FlicMapper.toDevices(snapshot);
  const entities = FlicMapper.toEntities(snapshot);

  expect(snapshot.online).toBeTrue();
  expect(devices[0].integrationDomain).toEqual('flic');
  expect(devices[0].model).toEqual('Flic Button');
  expect(entities.length).toEqual(2);
  expect(entities[0].id).toEqual('binary_sensor.flic_button_80_e4_da_70_01_02');
  expect(entities[0].attributes?.eventName).toEqual('flic_click');
});

tap.test('exposes Flic read-only runtime, HA alias, and unsupported control', async () => {
  const integration = new FlicIntegration();
  const alias = new HomeAssistantFlicIntegration();
  expect(alias instanceof FlicIntegration).toBeTrue();
  expect(alias.domain).toEqual('flic');
  expect(integration.status).toEqual('read-only-runtime');
  expect(flicProfile.metadata.configFlow).toEqual(false);
  expect(flicProfile.metadata.qualityScale).toEqual('legacy');
  expect(flicProfile.metadata.requirements).toEqual(['pyflic==2.0.4']);

  const runtime = await integration.setup({ rawData }, {});
  const status = await runtime.callService!({ domain: 'flic', service: 'status', target: {} });
  const refresh = await runtime.callService!({ domain: 'flic', service: 'refresh', target: {} });
  const snapshot = status.data as IFlicSnapshot;

  expect(status.success).toBeTrue();
  expect(refresh.success).toBeTrue();
  expect(snapshot.entities.some((entityArg) => entityArg.id === 'button_80_e4_da_70_01_02')).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual('Flic');

  const command = await runtime.callService!({ domain: 'flic', service: '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();
