import { expect, tap } from '@git.zone/tstest/tapbundle';
import { EpsonClient, EpsonConfigFlow, EpsonIntegration, EpsonMapper, HomeAssistantEpsonIntegration, createEpsonDiscoveryDescriptor, epsonProfile, type IEpsonSnapshot } from '../../ts/integrations/epson/index.js';

const rawData = {
  device: {
    id: 'epson-projector-1',
    name: 'Theater Projector',
    manufacturer: 'Epson',
    model: 'Home Cinema',
    serialNumber: 'EPSON12345',
  },
  entities: [
    { id: 'projector', name: 'Projector', platform: 'media_player', state: 'on', writable: true, attributes: { source: 'HDMI1', cmode: 'cinema', volumeLevel: 0.35 } },
  ],
};

tap.test('matches manual Epson candidates and creates config flow output', async () => {
  const descriptor = createEpsonDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'epson-manual-match');
  const result = await matcher!.matches({ host: 'projector.local', name: 'Epson Projector', metadata: { rawData, connectionType: 'http' } }, {});

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

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

  const done = await (await new EpsonConfigFlow().start(result.candidate!, {})).submit!({ name: 'Theater Projector' });
  expect(done.kind).toEqual('done');
  expect(done.config?.host).toEqual('projector.local');
  expect(done.config?.name).toEqual('Theater Projector');
  expect(done.config?.metadata?.connectionType).toEqual('http');
});

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

  expect(snapshot.online).toBeTrue();
  expect(snapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual('epson');
  expect(devices[0].manufacturer).toEqual('Epson');
  expect(entities.length).toEqual(1);
  expect(entities[0].platform).toEqual('media_player');
});

tap.test('exposes Epson runtime services, HA alias, and unsupported control without executor', async () => {
  const integration = new EpsonIntegration();
  const alias = new HomeAssistantEpsonIntegration();
  expect(alias instanceof EpsonIntegration).toBeTrue();
  expect(alias.domain).toEqual('epson');
  expect(integration.status).toEqual('control-runtime');
  expect(epsonProfile.metadata.requirements).toEqual(['epson-projector==0.6.0']);
  expect(epsonProfile.metadata.configFlow).toEqual(true);

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

  expect(status.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual('Theater Projector');
  expect((await runtime.callService!({ domain: 'epson', service: 'refresh', target: {} })).success).toBeTrue();

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

export default tap.start();
