import { createServer, type IncomingMessage, type ServerResponse } from 'node:http';
import { expect, tap } from '@git.zone/tstest/tapbundle';
import { HomeAssistantOllamaIntegration, OllamaClient, OllamaConfigFlow, OllamaIntegration, OllamaMapper, createOllamaDiscoveryDescriptor, ollamaProfile, type IOllamaSnapshot, type TOllamaRawData } from '../../ts/integrations/ollama/index.js';

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

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

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

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

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

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

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

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

tap.test('exposes Ollama runtime, HA alias, and unsupported control without executor', async () => {
  const integration = new OllamaIntegration();
  const alias = new HomeAssistantOllamaIntegration();
  expect(alias instanceof OllamaIntegration).toBeTrue();
  expect(alias.domain).toEqual("ollama");
  expect(integration.status).toEqual("read-only-runtime");
  expect(ollamaProfile.metadata.configFlow).toEqual(true);
  expect(ollamaProfile.metadata.requirements).toEqual([
    "ollama==0.5.1",
  ]);

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

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

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

tap.test('reads Ollama model status over the native HTTP API', async () => {
  const requests: Array<{ method?: string; url?: string; authorization?: string }> = [];
  const server = createServer((requestArg: IncomingMessage, responseArg: ServerResponse) => {
    requests.push({ method: requestArg.method, url: requestArg.url, authorization: requestArg.headers.authorization });
    const url = new URL(requestArg.url || '/', 'http://127.0.0.1');

    if (requestArg.headers.authorization !== 'Bearer secret') {
      json(responseArg, { error: 'unauthorized' }, 401);
      return;
    }
    if (url.pathname === '/api/tags') {
      json(responseArg, { models: [{ name: 'llama3.2:latest', model: 'llama3.2:latest', size: 100 }, { name: 'qwen3:4b-instruct', model: 'qwen3:4b-instruct', size: 200 }] });
      return;
    }
    if (url.pathname === '/api/ps') {
      json(responseArg, { models: [{ name: 'llama3.2:latest', model: 'llama3.2:latest', size: 100 }] });
      return;
    }
    json(responseArg, { error: 'not found' }, 404);
  });

  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}`;
    const client = new OllamaClient({ url, apiKey: 'secret', timeoutMs: 1000 });
    const snapshot = await client.getSnapshot(true);
    const runtime = await new OllamaIntegration().setup({ url, apiKey: 'secret', timeoutMs: 1000 }, {});
    const status = await runtime.callService!({ domain: 'ollama', service: 'status', target: {} });

    expect(snapshot.source).toEqual('http');
    expect(snapshot.online).toBeTrue();
    expect(snapshot.entities.find((entityArg) => entityArg.id === 'models')?.state).toEqual(2);
    expect(snapshot.entities.find((entityArg) => entityArg.id === 'running_models')?.state).toEqual(1);
    expect(status.success).toBeTrue();
    expect(requests.some((requestArg) => requestArg.url === '/api/tags' && requestArg.authorization === 'Bearer secret')).toBeTrue();
    await runtime.destroy();
  } finally {
    await new Promise<void>((resolve, reject) => server.close((errorArg) => errorArg ? reject(errorArg) : resolve()));
  }
});

export default tap.start();
