import { expect, tap } from '@git.zone/tstest/tapbundle';
import { KmtronicClient, KmtronicConfigFlow, KmtronicIntegration, KmtronicMapper, createKmtronicDiscoveryDescriptor, kmtronicProfile, type IKmtronicSnapshot, type TKmtronicRawData } from '../../ts/integrations/kmtronic/index.js';

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

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

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

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

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

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

  expect(snapshot.online).toBeTrue();
  expect(mappedSnapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual("kmtronic");
  expect(devices[0].manufacturer).toEqual("KMtronic");
  expect(entities.some((entityArg) => entityArg.integrationDomain === "kmtronic" && entityArg.platform === "switch")).toBeTrue();
});

tap.test('polls and controls KMtronic relays through the pykmtronic HTTP endpoints', async () => {
  const originalFetch = globalThis.fetch;
  const calls: Array<{ url: string; init?: RequestInit }> = [];
  globalThis.fetch = (async (inputArg: string | URL | Request, initArg?: RequestInit) => {
    const url = String(inputArg);
    calls.push({ url, init: initArg });
    if (url.endsWith('/status.xml')) {
      return new Response('<response><status>ok</status><relay1>1</relay1><relay2>0</relay2></response>', { status: 200 });
    }
    return new Response('ok', { status: 200 });
  }) as typeof fetch;

  try {
    const client = new KmtronicClient({ host: 'relay.local', username: 'admin', password: 'secret', name: 'Relay Controller' });
    const snapshot = await client.getSnapshot(true);
    const command = await client.execute({ domain: 'switch', service: 'turn_off', target: {}, data: { relayId: 1 } });

    expect(snapshot.source).toEqual('http');
    expect(snapshot.entities.find((entityArg) => entityArg.id === 'relay_1')?.state).toBeTrue();
    expect(command.success).toBeTrue();
    expect(calls[0].url).toEqual('http://relay.local/status.xml');
    expect(String((calls[0].init?.headers as Record<string, string>).authorization)).toContain('Basic ');
    expect(calls[1].url).toEqual('http://relay.local/FF0100');
  } finally {
    globalThis.fetch = originalFetch;
  }
});

tap.test('exposes KMtronic runtime and unsupported control without executor', async () => {
  const integration = new KmtronicIntegration();
  expect(integration.status).toEqual("control-runtime");
  expect(kmtronicProfile.metadata.configFlow).toEqual(true);
  expect(kmtronicProfile.metadata.requirements).toEqual([
    "pykmtronic==0.3.0",
  ]);

  const runtime = await integration.setup({ name: "KMtronic Runtime", rawData }, {});
  const statusResult = await runtime.callService!({ domain: "kmtronic", service: 'status', target: {} });
  const refresh = await runtime.callService!({ domain: "kmtronic", service: 'refresh', target: {} });
  const snapshot = statusResult.data as IKmtronicSnapshot;

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

  const command = await runtime.callService!({ domain: "kmtronic", service: kmtronicProfile.controlServices?.[0] || 'turn_on', target: {} });
  expect(command.success).toBeFalse();
  expect(command.error!).toContain('requires config.host/config.url');
  await runtime.destroy();
});

export default tap.start();
