import * as http from 'node:http';
import { expect, tap } from '@git.zone/tstest/tapbundle';
import { HomeAssistantOpenhomeIntegration, OpenhomeClient, OpenhomeConfigFlow, OpenhomeIntegration, OpenhomeMapper, createOpenhomeDiscoveryDescriptor, openhomeProfile, type IOpenhomeSnapshot, type TOpenhomeRawData } from '../../ts/integrations/openhome/index.js';

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

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

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

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

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

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

  expect(snapshot.online).toBeTrue();
  expect(mappedSnapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual("openhome");
  expect(devices[0].manufacturer).toEqual("Linn / OpenHome");
  expect(entities.some((entityArg) => entityArg.integrationDomain === "openhome" && entityArg.platform === "media_player")).toBeTrue();
});

tap.test('polls and controls Linn / OpenHome over documented local HTTP/SOAP', async () => {
  const actions: string[] = [];
  const server = http.createServer((request, response) => {
    if (request.method === 'GET' && request.url === '/device.xml') {
      response.writeHead(200, { 'content-type': 'application/xml' });
      response.end(deviceDescriptionXml());
      return;
    }

    if (request.method === 'POST') {
      const action = String(request.headers.soapaction || '').match(/#([^"#]+)"?$/)?.[1] || '';
      actions.push(action);
      response.writeHead(200, { 'content-type': 'text/xml' });
      response.end(soapResponse(action));
      return;
    }

    response.writeHead(404);
    response.end('not found');
  });

  await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));
  try {
    const address = server.address();
    if (!address || typeof address === 'string') {
      throw new Error('Test server did not expose a TCP address.');
    }
    const url = `http://127.0.0.1:${address.port}/device.xml`;
    const client = new OpenhomeClient({ url, timeoutMs: 1000 });
    const snapshot = await client.getSnapshot(true);
    const mediaEntity = snapshot.entities.find((entityArg) => entityArg.id === 'media_player')!;

    expect(snapshot.online).toBeTrue();
    expect(snapshot.source).toEqual('http');
    expect(snapshot.device.id).toEqual('linn-uuid-1');
    expect(snapshot.device.protocol).toEqual('upnp');
    expect(mediaEntity.state).toEqual('playing');
    expect(mediaEntity.attributes?.source).toEqual('Spotify');
    expect((mediaEntity.attributes?.trackInfo as { title?: string }).title).toEqual('Test Track');

    const pause = await client.execute({ domain: 'media_player', service: 'media_pause', target: {} });
    const volume = await client.execute({ domain: 'media_player', service: 'volume_up', target: {} });
    expect(pause.success).toBeTrue();
    expect(volume.success).toBeTrue();
    expect(actions.includes('Pause')).toBeTrue();
    expect(actions.includes('VolumeInc')).toBeTrue();

    const runtime = await new OpenhomeIntegration().setup({ url, timeoutMs: 1000 }, {});
    const status = await runtime.callService!({ domain: 'media_player', service: 'status', target: {} });
    expect(status.success).toBeTrue();
    expect((status.data as IOpenhomeSnapshot).source).toEqual('http');
    await runtime.destroy();
  } finally {
    await new Promise<void>((resolve, reject) => server.close((errorArg) => errorArg ? reject(errorArg) : resolve()));
  }
});

tap.test('exposes Linn / OpenHome runtime, HA alias, and unsupported control without executor', async () => {
  const integration = new OpenhomeIntegration();
  const alias = new HomeAssistantOpenhomeIntegration();
  expect(alias instanceof OpenhomeIntegration).toBeTrue();
  expect(alias.domain).toEqual("openhome");
  expect(integration.status).toEqual("control-runtime");
  expect(openhomeProfile.metadata.configFlow).toEqual(true);
  expect(openhomeProfile.metadata.requirements).toEqual([
    "openhomedevice==2.2.0",
  ]);
  expect((openhomeProfile.metadata.localApi as { implemented: string[] }).implemented.some((itemArg) => itemArg.includes('HTTP/SOAP'))).toBeTrue();

  const runtime = await integration.setup({ name: "Linn / OpenHome Runtime", rawData }, {});
  const statusResult = await runtime.callService!({ domain: "openhome", service: 'status', target: {} });
  const refresh = await runtime.callService!({ domain: "openhome", service: 'refresh', target: {} });
  const snapshot = statusResult.data as IOpenhomeSnapshot;

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

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

const deviceDescriptionXml = () => `<?xml version="1.0"?>
<root>
  <device>
    <friendlyName>Linn DSM</friendlyName>
    <manufacturer>Linn Products Ltd</manufacturer>
    <modelName>Akurate DSM</modelName>
    <serialNumber>serial-1</serialNumber>
    <UDN>uuid:linn-uuid-1</UDN>
    <serviceList>
      ${serviceXml('Product')}
      ${serviceXml('Volume')}
      ${serviceXml('Transport')}
      ${serviceXml('Playlist')}
      ${serviceXml('Info')}
      ${serviceXml('Pins')}
      <service><serviceType>urn:linn-co-uk:service:Update:1</serviceType><serviceId>urn:linn-co-uk:serviceId:Update</serviceId><controlURL>/upnp/update</controlURL></service>
    </serviceList>
  </device>
</root>`;

const serviceXml = (nameArg: string) => `<service><serviceType>urn:av-openhome-org:service:${nameArg}:1</serviceType><serviceId>urn:av-openhome-org:serviceId:${nameArg}</serviceId><controlURL>/upnp/${nameArg.toLowerCase()}</controlURL></service>`;

const soapResponse = (actionArg: string): string => {
  const didl = escapeXml('<DIDL-Lite xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/" xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/"><item><dc:title>Test Track</dc:title><upnp:artist>Test Artist</upnp:artist><upnp:album>Test Album</upnp:album><res protocolInfo="http-get:*:audio/flac:*" duration="0:03:05">http://media.local/test.flac</res><upnp:class>object.item.audioItem.musicTrack</upnp:class></item></DIDL-Lite>');
  const sources = escapeXml('<SourceList><Source><Name>Playlist</Name><Type>Playlist</Type><Visible>true</Visible></Source><Source><Name>Radio</Name><Type>Radio</Type><Visible>true</Visible></Source><Source><Name>Spotify</Name><Type>Playlist</Type><Visible>true</Visible></Source></SourceList>');
  const softwareStatus = escapeXml(JSON.stringify({ status: 'on_latest', current_software: { version: '4.100.502' } }));
  const responses: Record<string, string> = {
    Product: '<Name>Linn DSM</Name><Room>Living Room</Room>',
    Standby: '<Value>false</Value>',
    SourceIndex: '<Value>2</Value>',
    Source: '<Type>Playlist</Type><Name>Spotify</Name>',
    SourceXml: `<Value>${sources}</Value>`,
    TransportState: '<State>Playing</State>',
    Track: `<Uri>http://media.local/test.flac</Uri><Metadata>${didl}</Metadata>`,
    Volume: '<Value>35</Value>',
    Mute: '<Value>false</Value>',
    GetSoftwareStatus: `<SoftwareStatus>${softwareStatus}</SoftwareStatus>`,
  };
  return `<?xml version="1.0"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><u:${actionArg}Response>${responses[actionArg] || ''}</u:${actionArg}Response></s:Body></s:Envelope>`;
};

const escapeXml = (valueArg: string): string => valueArg.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');

export default tap.start();
