import { expect, tap } from '@git.zone/tstest/tapbundle';
import { EmulatedRokuClient, EmulatedRokuConfigFlow, EmulatedRokuIntegration, EmulatedRokuMapper, HomeAssistantEmulatedRokuIntegration, createEmulatedRokuDiscoveryDescriptor, emulatedRokuProfile, type IEmulatedRokuSnapshot } from '../../ts/integrations/emulated_roku/index.js';

const rawData = {
  servers: [
    {
      name: 'Living Room Remote',
      host_ip: '192.168.1.20',
      listen_port: 8060,
      advertise_ip: '192.168.1.20',
      advertise_port: 8060,
      upnp_bind_multicast: true,
      state: 'listening',
    },
  ],
};

tap.test('matches manual Emulated Roku candidates and creates config flow output', async () => {
  const descriptor = createEmulatedRokuDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'emulated_roku-manual-match');
  const result = await matcher!.matches({ host: 'roku-emulator.local', name: 'Emulated Roku', metadata: { rawData } }, {});

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

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

  const done = await (await new EmulatedRokuConfigFlow().start(result.candidate!, {})).submit!({ name: 'Living Room Remote' });
  expect(done.kind).toEqual('done');
  expect(done.config?.host).toEqual('roku-emulator.local');
  expect(done.config?.port).toEqual(8060);
});

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

  expect(snapshot.online).toBeTrue();
  expect(devices[0].integrationDomain).toEqual('emulated_roku');
  expect(devices[0].manufacturer).toEqual('Home Assistant');
  expect(entities.length).toEqual(1);
  expect(entities[0].platform).toEqual('sensor');
  expect(entities[0].state).toEqual('listening');
});

tap.test('exposes Emulated Roku read-only runtime, HA alias, and unsupported control', async () => {
  const integration = new EmulatedRokuIntegration();
  const alias = new HomeAssistantEmulatedRokuIntegration();
  expect(alias.domain).toEqual('emulated_roku');
  expect(integration.status).toEqual('read-only-runtime');
  expect(emulatedRokuProfile.metadata.configFlow).toEqual(true);
  expect(emulatedRokuProfile.metadata.requirements).toEqual(['emulated-roku==0.3.0']);
  expect(emulatedRokuProfile.metadata.dependencies).toEqual(['network']);

  const runtime = await integration.setup({ name: 'Roku Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'emulated_roku', service: 'status', target: {} });
  const snapshot = status.data as IEmulatedRokuSnapshot;

  expect(status.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual('Roku Runtime');

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

export default tap.start();
