import { expect, tap } from '@git.zone/tstest/tapbundle';
import { HdmiCecClient, HdmiCecConfigFlow, HdmiCecIntegration, HdmiCecMapper, HomeAssistantHdmiCecIntegration, createHdmiCecDiscoveryDescriptor, hdmiCecProfile, type IHdmiCecSnapshot, type THdmiCecRawData } from '../../ts/integrations/hdmi_cec/index.js';

const rawData: THdmiCecRawData = {
  name: 'Living Room CEC Bus',
  devices: [
    {
      logicalAddress: 4,
      physicalAddress: '1.0.0.0',
      typeId: 4,
      typeName: 'Playback',
      vendor: 'Sony',
      vendorId: 43775,
      osdName: 'Blu-ray',
      powerStatus: 1,
      status: 'playing',
      platform: 'media_player',
    },
    {
      logicalAddress: 0,
      physicalAddress: '0.0.0.0',
      typeId: 0,
      typeName: 'TV',
      vendor: 'LG',
      osdName: 'TV',
      powerStatus: 0,
      platform: 'switch',
    },
  ],
};

tap.test('matches manual HDMI-CEC candidates and creates config flow output', async () => {
  const descriptor = createHdmiCecDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'hdmi_cec-manual-match');
  const result = await matcher!.matches({ source: 'manual', id: 'living-room-cec', name: 'HDMI-CEC', metadata: { rawData } }, {});

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

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

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

tap.test('maps HDMI-CEC raw snapshots to runtime devices and entities', async () => {
  const client = new HdmiCecClient({ name: 'Living Room CEC Bus', rawData });
  const snapshot = await client.getSnapshot();
  const mappedSnapshot = HdmiCecMapper.toSnapshotFromRaw({ name: 'Living Room CEC Bus' }, rawData);
  const devices = HdmiCecMapper.toDevices(mappedSnapshot);
  const entities = HdmiCecMapper.toEntities(mappedSnapshot);

  expect(snapshot.online).toBeTrue();
  expect(devices[0].integrationDomain).toEqual('hdmi_cec');
  expect(devices[0].manufacturer).toEqual('HDMI-CEC');
  expect(entities.some((entityArg) => entityArg.id === 'media_player.living_room_cec_bus_hdmi_4')).toBeTrue();
  expect(entities.some((entityArg) => entityArg.id === 'switch.living_room_cec_bus_hdmi_0')).toBeTrue();
  expect(entities.find((entityArg) => entityArg.id === 'media_player.living_room_cec_bus_hdmi_4')?.state).toEqual('playing');
});

tap.test('exposes HDMI-CEC read-only runtime, HA alias, and unsupported control', async () => {
  const integration = new HdmiCecIntegration();
  const alias = new HomeAssistantHdmiCecIntegration();
  expect(alias instanceof HdmiCecIntegration).toBeTrue();
  expect(alias.domain).toEqual('hdmi_cec');
  expect(integration.status).toEqual('read-only-runtime');
  expect(hdmiCecProfile.metadata.configFlow).toEqual(false);
  expect(hdmiCecProfile.metadata.qualityScale).toEqual('legacy');
  expect(hdmiCecProfile.metadata.requirements).toEqual(['pyCEC==0.5.2']);

  const runtime = await integration.setup({ name: 'Living Room CEC Bus', rawData }, {});
  const status = await runtime.callService!({ domain: 'hdmi_cec', service: 'status', target: {} });
  const refresh = await runtime.callService!({ domain: 'hdmi_cec', service: 'refresh', target: {} });
  const snapshot = status.data as IHdmiCecSnapshot;

  expect(status.success).toBeTrue();
  expect(refresh.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect((await runtime.entities()).length).toEqual(2);

  const command = await runtime.callService!({ domain: 'hdmi_cec', service: 'send_command', target: {}, data: { raw: '10:36' } });
  expect(command.success).toBeFalse();
  expect(command.error!).toContain('requires an injected client.execute() or commandExecutor');
  await runtime.destroy();
});

export default tap.start();
