import { expect, tap } from '@git.zone/tstest/tapbundle';
import { Ccm15Client, Ccm15ConfigFlow, Ccm15Integration, Ccm15Mapper, HomeAssistantCcm15Integration, ccm15Profile, createCcm15DiscoveryDescriptor, type ICcm15Snapshot } from '../../ts/integrations/ccm15/index.js';

const rawData = {
  zones: 3,
  temperature: 23,
  mode: 'cool',
};

tap.test('matches manual Midea ccm15 AC Controller candidates and exposes native profile metadata', async () => {
  const integration = new Ccm15Integration();
  const alias = new HomeAssistantCcm15Integration();
  const descriptor = createCcm15DiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'ccm15-manual-match');
  const result = await matcher!.matches({ host: 'ccm15.local', name: 'Midea CCM15 AC Controller', metadata: { rawData } }, {});

  expect(alias.domain).toEqual('ccm15');
  expect(integration.displayName).toEqual('Midea ccm15 AC Controller');
  expect(ccm15Profile.manufacturer).toEqual('Midea');
  expect(ccm15Profile.model).toEqual('CCM15 AC controller');
  expect(ccm15Profile.metadata.requirements).toEqual(['py_ccm15==0.1.2']);
  expect(ccm15Profile.metadata.integrationType).toEqual('hub');
  expect(ccm15Profile.serviceDomains).toEqual(['climate', 'fan']);
  expect(ccm15Profile.controlServices).toEqual(['set_temperature', 'set_hvac_mode', 'set_percentage', 'turn_on', 'turn_off']);
  expect(result.matched).toBeTrue();
  expect(result.candidate?.integrationDomain).toEqual('ccm15');

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

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

tap.test('maps Midea ccm15 AC Controller raw snapshots to runtime devices and entities', async () => {
  const client = new Ccm15Client({ name: 'Midea ccm15 AC Controller Test', rawData });
  const snapshot = await client.getSnapshot();
  const devices = Ccm15Mapper.toDevices(snapshot);
  const entities = Ccm15Mapper.toEntities(snapshot);

  expect(snapshot.online).toBeTrue();
  expect(snapshot.source).toEqual('manual');
  expect(snapshot.rawData).toEqual(rawData);
  expect(devices[0].integrationDomain).toEqual('ccm15');
  expect(devices[0].manufacturer).toEqual('Midea');
  expect(devices[0].model).toEqual('CCM15 AC controller');
  expect(entities.some((entityArg) => entityArg.name === 'Mode' && entityArg.state === 'cool')).toBeTrue();
});

tap.test('exposes Midea ccm15 climate and fan services without fake live control', async () => {
  const runtime = await new Ccm15Integration().setup({ name: 'Midea ccm15 AC Controller Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'climate', service: 'status', target: {} });
  const snapshot = status.data as ICcm15Snapshot;

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

  const liveCommand = await runtime.callService!({ domain: 'fan', service: 'set_percentage', target: {}, data: { percentage: 50 } });
  expect(liveCommand.success).toBeFalse();
  await runtime.destroy();

  const executorRuntime = await new Ccm15Integration().setup({
    name: 'Midea ccm15 AC Controller Executor',
    rawData,
    commandExecutor: {
      execute: async (requestArg) => ({ success: true, data: { service: requestArg.service } }),
    },
  }, {});
  const executed = await executorRuntime.callService!({ domain: 'fan', service: 'set_percentage', target: {}, data: { percentage: 50 } });
  expect(executed.success).toBeTrue();
  expect((executed.data as { service: string }).service).toEqual('set_percentage');
  await executorRuntime.destroy();
});

export default tap.start();
