import { expect, tap } from '@git.zone/tstest/tapbundle';
import { Gogogate2Client, Gogogate2ConfigFlow, Gogogate2Integration, Gogogate2Mapper, HomeAssistantGogogate2Integration, createGogogate2DiscoveryDescriptor, gogogate2Profile, type IGogogate2Snapshot, type TGogogate2RawData } from '../../ts/integrations/gogogate2/index.js';

const rawData: TGogogate2RawData = {
  model: 'iSmartGate',
  firmwareversion: '2.1.0',
  remoteaccessenabled: false,
  remoteaccess: 'garage.example.com',
  doors: [
    { door_id: 1, name: 'Garage Door', gate: false, status: 'closed', sensorid: 'ABC123', voltage: 92, temperature: 18.5 },
  ],
};

tap.test('matches manual Gogogate2 candidates and creates config flow output', async () => {
  const descriptor = createGogogate2DiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'gogogate2-manual-match');
  const result = await matcher!.matches({ source: 'manual', id: 'garage-hub', name: 'Garage Controller', metadata: { rawData } }, {});

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

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

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

tap.test('maps Gogogate2 raw snapshots to runtime devices and entities', async () => {
  const client = new Gogogate2Client({ name: 'Garage Controller', rawData });
  const snapshot = await client.getSnapshot();
  const mappedSnapshot = Gogogate2Mapper.toSnapshotFromRaw({ name: 'Garage Controller' }, rawData);
  const devices = Gogogate2Mapper.toDevices(mappedSnapshot);
  const entities = Gogogate2Mapper.toEntities(mappedSnapshot);

  expect(snapshot.online).toBeTrue();
  expect(mappedSnapshot.device.model).toEqual('iSmartGate');
  expect(devices[0].manufacturer).toEqual('Remsol');
  expect(entities.some((entityArg) => entityArg.id === 'cover.garage_controller_door_1')).toBeTrue();
  expect(entities.some((entityArg) => entityArg.id === 'sensor.garage_controller_door_1_battery')).toBeTrue();
  expect(entities.some((entityArg) => entityArg.id === 'sensor.garage_controller_door_1_temperature')).toBeTrue();
});

tap.test('exposes Gogogate2 read-only runtime, HA alias, and unsupported control', async () => {
  const integration = new Gogogate2Integration();
  const alias = new HomeAssistantGogogate2Integration();
  expect(alias instanceof Gogogate2Integration).toBeTrue();
  expect(alias.domain).toEqual('gogogate2');
  expect(integration.status).toEqual('read-only-runtime');
  expect(gogogate2Profile.metadata.configFlow).toEqual(true);
  expect(gogogate2Profile.metadata.requirements).toEqual(['ismartgate==5.0.2']);
  expect(gogogate2Profile.metadata.codeowners).toEqual(['@vangorra']);

  const runtime = await integration.setup({ name: 'Garage Controller', rawData }, {});
  const status = await runtime.callService!({ domain: 'gogogate2', service: 'status', target: {} });
  const refresh = await runtime.callService!({ domain: 'gogogate2', service: 'refresh', target: {} });
  const snapshot = status.data as IGogogate2Snapshot;

  expect(status.success).toBeTrue();
  expect(refresh.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual('Garage Controller');

  const command = await runtime.callService!({ domain: 'cover', service: 'open_cover', target: { entityId: 'cover.garage_controller_door_1' } });
  expect(command.success).toBeFalse();
  expect(command.error!).toContain('requires an injected client.execute() or commandExecutor');
  await runtime.destroy();
});

export default tap.start();
