import { expect, tap } from '@git.zone/tstest/tapbundle';
import { createServer, type IncomingMessage, type ServerResponse } from 'node:http';
import { HomeAssistantOverkizIntegration, OverkizClient, OverkizConfigFlow, OverkizIntegration, OverkizMapper, createOverkizDiscoveryDescriptor, overkizProfile, type IOverkizSnapshot, type TOverkizRawData } from '../../ts/integrations/overkiz/index.js';

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

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

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

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

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

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

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

tap.test('polls Somfy Developer Mode setup and executes commands through local HTTP API', async () => {
  const requests: Array<{ method?: string; url?: string; authorization?: string; body?: string }> = [];
  const server = createServer((requestArg: IncomingMessage, responseArg: ServerResponse) => {
    void (async () => {
      const body = requestArg.method === 'POST' ? await readBody(requestArg) : undefined;
      requests.push({ method: requestArg.method, url: requestArg.url, authorization: requestArg.headers.authorization, body });
      const url = new URL(requestArg.url || '/', 'http://127.0.0.1');
      if (requestArg.headers.authorization !== 'Bearer local-token') {
        json(responseArg, { errorCode: 'RESOURCE_ACCESS_DENIED', error: 'Missing authorization token.' }, 403);
        return;
      }
      if (url.pathname === '/enduser-mobile-web/1/enduserAPI/setup') {
        json(responseArg, {
          gateways: [{ id: '1234-5678-9012', type: 'TAHOMA_SWITCH', connectivity: { protocolVersion: '2026.1' } }],
          devices: [{
            deviceURL: 'io://1234-5678-9012/1',
            label: 'Kitchen light',
            widget: 'OnOff',
            uiClass: 'OnOff',
            protocol: 'io',
            available: true,
            enabled: true,
            states: [
              { name: 'core:OnOffState', value: 'on' },
              { name: 'core:BatteryState', value: 78 },
            ],
          }],
        });
        return;
      }
      if (url.pathname === '/enduser-mobile-web/1/enduserAPI/exec/apply' && requestArg.method === 'POST') {
        expect(body).toContain('io://1234-5678-9012/1');
        expect(body).toContain('off');
        json(responseArg, { execId: 'exec-1' });
        return;
      }
      responseArg.statusCode = 404;
      responseArg.end('{}');
    })().catch((errorArg) => {
      responseArg.statusCode = 500;
      responseArg.end(String(errorArg));
    });
  });

  await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));
  try {
    const address = server.address();
    const port = typeof address === 'object' && address ? address.port : 0;
    const url = `http://127.0.0.1:${port}/enduser-mobile-web/1/enduserAPI`;
    const client = new OverkizClient({ url, token: 'local-token', timeoutMs: 1000 });
    const snapshot = await client.getSnapshot(true);
    const command = await client.execute({ domain: 'overkiz', service: 'execute_command', target: {}, data: { deviceUrl: 'io://1234-5678-9012/1', command: 'off' } });
    const runtime = await new OverkizIntegration().setup({ url, token: 'local-token', timeoutMs: 1000 }, {});
    const status = await runtime.callService!({ domain: 'overkiz', service: 'status', target: {} });

    expect(snapshot.online).toBeTrue();
    expect(snapshot.source).toEqual('http');
    expect(snapshot.device.serialNumber).toEqual('1234-5678-9012');
    expect(snapshot.entities.find((entityArg) => entityArg.attributes?.stateName === 'core:OnOffState')?.platform).toEqual('switch');
    expect(snapshot.entities.find((entityArg) => entityArg.attributes?.stateName === 'core:BatteryState')?.state).toEqual(78);
    expect(command.success).toBeTrue();
    expect((command.data as { execId?: string }).execId).toEqual('exec-1');
    expect((status.data as IOverkizSnapshot).online).toBeTrue();
    expect(requests.every((requestArg) => requestArg.authorization === 'Bearer local-token')).toBeTrue();
    expect(requests.some((requestArg) => requestArg.method === 'POST' && requestArg.url === '/enduser-mobile-web/1/enduserAPI/exec/apply')).toBeTrue();
    await runtime.destroy();
  } finally {
    await new Promise<void>((resolve, reject) => server.close((errorArg) => errorArg ? reject(errorArg) : resolve()));
  }
});

tap.test('exposes Overkiz runtime, HA alias, and unsupported control without executor', async () => {
  const integration = new OverkizIntegration();
  const alias = new HomeAssistantOverkizIntegration();
  expect(alias instanceof OverkizIntegration).toBeTrue();
  expect(alias.domain).toEqual("overkiz");
  expect(integration.status).toEqual("control-runtime");
  expect(overkizProfile.metadata.configFlow).toEqual(true);
  expect(overkizProfile.metadata.requirements).toEqual([
    "pyoverkiz==1.20.0",
  ]);

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

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

  const command = await runtime.callService!({ domain: "overkiz", service: overkizProfile.controlServices?.[0] || 'turn_on', target: {} });
  expect(command.success).toBeFalse();
  expect(command.error!).toContain('Overkiz live commands require config.host/config.url plus a local Developer Mode config.token');
  await runtime.destroy();
});

const readBody = async (requestArg: IncomingMessage): Promise<string> => {
  const chunks: Buffer[] = [];
  for await (const chunk of requestArg) {
    chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
  }
  return Buffer.concat(chunks).toString('utf8');
};

const json = (responseArg: ServerResponse, valueArg: unknown, statusArg = 200): void => {
  responseArg.statusCode = statusArg;
  responseArg.setHeader('content-type', 'application/json');
  responseArg.end(JSON.stringify(valueArg));
};

export default tap.start();
