import { expect, tap } from '@git.zone/tstest/tapbundle';
import { Lw12wifiClient, Lw12wifiConfigFlow, Lw12wifiIntegration, Lw12wifiMapper, createLw12wifiDiscoveryDescriptor, lw12wifiProfile, type ILw12wifiSnapshot, type TLw12wifiRawData } from '../../ts/integrations/lw12wifi/index.js';

const rawData: TLw12wifiRawData = {
  device: {
    id: 'lw12wifi-device-1',
    name: "LAGUTE LW-12 Device",
    manufacturer: "LAGUTE LW-12",
    model: "LAGUTE LW-12 local integration",
    serialNumber: 'lw12wifi-serial-1',
  },
  entities: [
    { id: 'status', name: 'Status', platform: "light", state: true, attributes: { domain: "lw12wifi" } },
  ],
  online: true,
  updatedAt: '2026-01-01T00:00:00.000Z',
  source: 'manual',
};

tap.test('matches manual LAGUTE LW-12 candidates and creates config flow output', async () => {
  const descriptor = createLw12wifiDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'lw12wifi-manual-match');
  const result = await matcher!.matches({ source: 'manual', id: 'lw12wifi-device-1', name: "LAGUTE LW-12 Device", metadata: { rawData } }, {});

  expect(result.matched).toBeTrue();
  expect(result.candidate?.integrationDomain).toEqual("lw12wifi");

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

  const done = await (await new Lw12wifiConfigFlow().start(result.candidate!, {})).submit!({});
  expect(done.kind).toEqual('done');
  expect(done.config?.uniqueId).toEqual('lw12wifi-device-1');
  expect(done.config?.rawData).toEqual(rawData);
});

tap.test('maps LAGUTE LW-12 raw snapshots to runtime devices and entities', async () => {
  const client = new Lw12wifiClient({ name: "LAGUTE LW-12 Runtime", rawData });
  const snapshot = await client.getSnapshot();
  const mappedSnapshot = Lw12wifiMapper.toSnapshotFromRaw({ name: "LAGUTE LW-12 Runtime" }, rawData);
  const devices = Lw12wifiMapper.toDevices(mappedSnapshot);
  const entities = Lw12wifiMapper.toEntities(mappedSnapshot);

  expect(snapshot.online).toBeTrue();
  expect(mappedSnapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual("lw12wifi");
  expect(devices[0].manufacturer).toEqual("LAGUTE LW-12");
  expect(entities.some((entityArg) => entityArg.integrationDomain === "lw12wifi" && entityArg.platform === "light")).toBeTrue();
});

tap.test('exposes LAGUTE LW-12 runtime and unsupported control without executor', async () => {
  const integration = new Lw12wifiIntegration();
  expect(integration.status).toEqual("read-only-runtime");
  expect(lw12wifiProfile.metadata.configFlow).toEqual(false);
  expect(lw12wifiProfile.metadata.requirements).toEqual([
    "lw12==0.9.2",
  ]);

  const runtime = await integration.setup({ name: "LAGUTE LW-12 Runtime", rawData }, {});
  const statusResult = await runtime.callService!({ domain: "lw12wifi", service: 'status', target: {} });
  const refresh = await runtime.callService!({ domain: "lw12wifi", service: 'refresh', target: {} });
  const snapshot = statusResult.data as ILw12wifiSnapshot;

  expect(statusResult.success).toBeTrue();
  expect(refresh.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual("LAGUTE LW-12 Device");

  const command = await runtime.callService!({ domain: "lw12wifi", service: lw12wifiProfile.controlServices?.[0] || 'turn_on', target: {} });
  expect(command.success).toBeFalse();
  expect(command.error!).toContain('requires an injected client.execute() or commandExecutor');
  await runtime.destroy();
});

export default tap.start();
