import { expect, tap } from '@git.zone/tstest/tapbundle';
import { DenonRs232Client, DenonRs232ConfigFlow, DenonRs232Integration, DenonRs232Mapper, HomeAssistantDenonRs232Integration, createDenonRs232DiscoveryDescriptor, denonRs232Profile, type IDenonRs232Snapshot } from '../../ts/integrations/denon_rs232/index.js';

const rawData = {
  device: {
    name: 'Denon AVR-3803',
    manufacturer: 'Denon',
    model: 'AVR-3803',
    serialNumber: 'denon-rs232-1',
  },
  entities: [
    {
      id: 'main_receiver',
      name: 'Main receiver',
      platform: 'media_player' as const,
      state: 'on',
      attributes: {
        source: 'cd',
        volumeLevel: 0.42,
      },
    },
  ],
};

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

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

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

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

tap.test('maps Denon RS232 raw snapshots to runtime devices and entities', async () => {
  const client = new DenonRs232Client({ name: 'Denon RS232 Test', rawData });
  const snapshot = await client.getSnapshot();
  const devices = DenonRs232Mapper.toDevices(snapshot);
  const entities = DenonRs232Mapper.toEntities(snapshot);

  expect(snapshot.online).toBeTrue();
  expect(snapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual('denon_rs232');
  expect(devices[0].manufacturer).toEqual('Denon');
  expect(entities[0].platform).toEqual('media_player');
});

tap.test('exposes Denon RS232 runtime status and explicit unsupported control without executor', async () => {
  const alias = new HomeAssistantDenonRs232Integration();
  expect(alias instanceof DenonRs232Integration).toBeTrue();
  expect(alias.domain).toEqual('denon_rs232');
  expect(denonRs232Profile.status).toEqual('read-only-runtime');
  expect(denonRs232Profile.metadata.qualityScale).toEqual('bronze');
  expect(denonRs232Profile.metadata.requirements).toEqual(['denon-rs232==4.1.0']);

  const runtime = await new DenonRs232Integration().setup({ name: 'Denon RS232 Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'denon_rs232', service: 'status', target: {} });
  const snapshot = status.data as IDenonRs232Snapshot;

  expect(status.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect((await runtime.callService!({ domain: 'denon_rs232', service: 'refresh', target: {} })).success).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual('Denon AVR-3803');

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

export default tap.start();
