import { expect, tap } from '@git.zone/tstest/tapbundle';
import { GreeClient, GreeConfigFlow, GreeIntegration, GreeMapper, HomeAssistantGreeIntegration, createGreeDiscoveryDescriptor, greeProfile, type IGreeSnapshot, type TGreeRawData } from '../../ts/integrations/gree/index.js';

const rawData: TGreeRawData = {
  device_info: {
    name: 'Bedroom AC',
    ip: '192.168.1.70',
    port: 7000,
    mac: 'AA:BB:CC:DD:EE:70',
  },
  raw_properties: {
    Pow: 1,
    Mod: 1,
    SetTem: 23,
    TemSen: 22,
    WdSpd: 3,
    Lig: 1,
    Quiet: 0,
    Air: 1,
    Blo: 0,
    Health: 1,
    Tur: 0,
    SwhSlp: 0,
  },
};

tap.test('matches manual Gree candidates and creates config flow output', async () => {
  const descriptor = createGreeDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'gree-manual-match');
  const result = await matcher!.matches({ source: 'manual', host: '192.168.1.70', port: 7000, name: 'Bedroom AC', metadata: { rawData } }, {});

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

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

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

tap.test('maps Gree raw snapshots to runtime devices and entities', async () => {
  const client = new GreeClient({ host: '192.168.1.70', port: 7000, rawData });
  const snapshot = await client.getSnapshot();
  const mappedSnapshot = GreeMapper.toSnapshotFromRaw({ host: '192.168.1.70', port: 7000 }, rawData);
  const devices = GreeMapper.toDevices(mappedSnapshot);
  const entities = GreeMapper.toEntities(mappedSnapshot);

  expect(snapshot.online).toBeTrue();
  expect(mappedSnapshot.device.serialNumber).toEqual('AA:BB:CC:DD:EE:70');
  expect(devices[0].integrationDomain).toEqual('gree');
  expect(devices[0].manufacturer).toEqual('Gree');
  expect(entities.some((entityArg) => entityArg.platform === 'climate' && entityArg.state === 'cool')).toBeTrue();
  expect(entities.some((entityArg) => entityArg.platform === 'switch' && entityArg.name === 'Panel light' && entityArg.state === true)).toBeTrue();
});

tap.test('exposes Gree runtime, HA alias, and unsupported control without executor', async () => {
  const integration = new GreeIntegration();
  const alias = new HomeAssistantGreeIntegration();
  expect(alias instanceof GreeIntegration).toBeTrue();
  expect(alias.domain).toEqual('gree');
  expect(integration.status).toEqual('control-runtime');
  expect(greeProfile.metadata.configFlow).toEqual(true);
  expect(greeProfile.metadata.requirements).toEqual(['greeclimate==2.1.1']);

  const runtime = await integration.setup({ host: '192.168.1.70', port: 7000, rawData }, {});
  const status = await runtime.callService!({ domain: 'gree', service: 'status', target: {} });
  const refresh = await runtime.callService!({ domain: 'gree', service: 'refresh', target: {} });
  const snapshot = status.data as IGreeSnapshot;

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

  const command = await runtime.callService!({ domain: 'climate', service: 'set_temperature', target: {}, data: { temperature: 24 } });
  expect(command.success).toBeFalse();
  expect(command.error!).toContain('requires an injected client.execute() or commandExecutor');
  await runtime.destroy();
});

export default tap.start();
