import { expect, tap } from '@git.zone/tstest/tapbundle';
import * as http from 'node:http';
import type { AddressInfo } from 'node:net';
import { HomeAssistantItunesIntegration, ItunesClient, ItunesConfigFlow, ItunesIntegration, ItunesMapper, createItunesDiscoveryDescriptor, itunesProfile, type IItunesSnapshot, type TItunesRawData } from '../../ts/integrations/itunes/index.js';

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

const listen = async (serverArg: http.Server): Promise<number> => await new Promise((resolveArg) => {
  serverArg.listen(0, '127.0.0.1', () => resolveArg((serverArg.address() as AddressInfo).port));
});

const close = async (serverArg: http.Server): Promise<void> => await new Promise((resolveArg, rejectArg) => {
  serverArg.close((errorArg) => errorArg ? rejectArg(errorArg) : resolveArg());
});

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

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

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

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

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

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

tap.test('polls itunes-api HTTP endpoints and sends native playback command', async () => {
  const requests: Array<{ method?: string; url?: string }> = [];
  const server = http.createServer((requestArg, responseArg) => {
    requests.push({ method: requestArg.method, url: requestArg.url });
    responseArg.setHeader('content-type', 'application/json');
    if (requestArg.method === 'GET' && requestArg.url === '/now_playing') {
      responseArg.end(JSON.stringify({ player_state: 'playing', volume: 40, muted: false, name: 'Test Track', album: 'Album', artist: 'Artist', playlist: 'Library', id: 'track-1', shuffle: 'off' }));
      return;
    }
    if (requestArg.method === 'GET' && requestArg.url === '/airplay_devices') {
      responseArg.end(JSON.stringify({ airplay_devices: [{ id: 'speaker-1', name: 'Kitchen', selected: true, sound_volume: 55, supports_audio: true, supports_video: false }] }));
      return;
    }
    if (requestArg.method === 'PUT' && requestArg.url === '/pause') {
      responseArg.end(JSON.stringify({ player_state: 'paused' }));
      return;
    }
    responseArg.statusCode = 404;
    responseArg.end(JSON.stringify({ error: 'not found' }));
  });
  const port = await listen(server);
  try {
    const client = new ItunesClient({ host: '127.0.0.1', port });
    const snapshot = await client.getSnapshot(true);
    const command = await client.execute({ domain: 'itunes', service: 'media_pause', target: {} });

    expect(snapshot.source).toEqual('http');
    expect(snapshot.entities.some((entityArg) => entityArg.id === 'player' && entityArg.state === 'playing')).toBeTrue();
    expect(snapshot.entities.some((entityArg) => entityArg.id === 'airplay_speaker_1' && entityArg.state === 'on')).toBeTrue();
    expect(command.success).toBeTrue();
    expect(requests.some((requestArg) => requestArg.method === 'GET' && requestArg.url === '/now_playing')).toBeTrue();
    expect(requests.some((requestArg) => requestArg.method === 'GET' && requestArg.url === '/airplay_devices')).toBeTrue();
    expect(requests.some((requestArg) => requestArg.method === 'PUT' && requestArg.url === '/pause')).toBeTrue();
  } finally {
    await close(server);
  }
});

tap.test('exposes Apple iTunes runtime, HA alias, and unsupported control without executor', async () => {
  const integration = new ItunesIntegration();
  const alias = new HomeAssistantItunesIntegration();
  expect(alias instanceof ItunesIntegration).toBeTrue();
  expect(alias.domain).toEqual("itunes");
  expect(integration.status).toEqual("control-runtime");
  expect(itunesProfile.metadata.configFlow).toEqual(false);
  expect(itunesProfile.metadata.requirements).toEqual([]);

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

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

  const command = await runtime.callService!({ domain: "itunes", service: itunesProfile.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();
