import * as fs from 'node:fs/promises';
import * as os from 'node:os';
import * as path from 'node:path';
import { expect, tap } from '@git.zone/tstest/tapbundle';
import { LandisgyrHeatMeterClient, LandisgyrHeatMeterConfigFlow, LandisgyrHeatMeterIntegration, LandisgyrHeatMeterMapper, createLandisgyrHeatMeterDiscoveryDescriptor, landisgyrHeatMeterProfile, type ILandisgyrHeatMeterSnapshot, type TLandisgyrHeatMeterRawData } from '../../ts/integrations/landisgyr_heat_meter/index.js';

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

const heatMeterFile = `/LUGCUH50\n6.8(12.345*GJ)6.26(00123.456*m3)9.21(OWNER-1)6.26*01(100.100*m3)6.8*01(11.100*GJ)F(00000000)9.20(66153690)6.35(60*m)6.6(25.5*kW)6.6*01(20.5*kW)6.33(3.2*m3ph)9.4(70.1*C&40.2*C)6.31(1200*h)6.32(5*h)6.32*01(4*h)6.36(2026-01-01)6.33*01(2.2*m3ph)9.4*01(65.1*C&35.2*C)6.36*02(2026-02-01)9.36(2026-03-04&05:06:07)9.24(4.0*m3ph)9.1(SET&FW)9.31(1100*h)!\n`;

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

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

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

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

tap.test('maps Landis+Gyr Heat Meter raw snapshots to runtime devices and entities', async () => {
  const client = new LandisgyrHeatMeterClient({ name: "Landis+Gyr Heat Meter Runtime", rawData });
  const snapshot = await client.getSnapshot();
  const mappedSnapshot = LandisgyrHeatMeterMapper.toSnapshotFromRaw({ name: "Landis+Gyr Heat Meter Runtime" }, rawData);
  const devices = LandisgyrHeatMeterMapper.toDevices(mappedSnapshot);
  const entities = LandisgyrHeatMeterMapper.toEntities(mappedSnapshot);

  expect(snapshot.online).toBeTrue();
  expect(mappedSnapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual("landisgyr_heat_meter");
  expect(devices[0].manufacturer).toEqual("Landis+Gyr Heat Meter");
  expect(entities.some((entityArg) => entityArg.integrationDomain === "landisgyr_heat_meter" && entityArg.platform === "sensor")).toBeTrue();
});

tap.test('reads documented ultraheat-api file snapshots and parses meter sensors', async () => {
  const directory = await fs.mkdtemp(path.join(os.tmpdir(), 'landisgyr-'));
  const filePath = path.join(directory, 'LUGCUH50_dummy.txt');
  await fs.writeFile(filePath, heatMeterFile, 'utf8');

  try {
    const client = new LandisgyrHeatMeterClient({ filePath, name: 'Utility Heat Meter' });
    const snapshot = await client.getSnapshot(true);
    const runtime = await new LandisgyrHeatMeterIntegration().setup({ filePath, name: 'Utility Heat Meter' }, {});
    const entities = await runtime.entities();
    const refresh = await runtime.callService!({ domain: 'landisgyr_heat_meter', service: 'refresh', target: {} });

    expect(snapshot.online).toBeTrue();
    expect(snapshot.source).toEqual('file');
    expect(snapshot.device.model).toEqual('LUGCUH50');
    expect(snapshot.device.serialNumber).toEqual('66153690');
    expect(snapshot.entities.find((entityArg) => entityArg.id === 'heat_usage_gj')?.state).toEqual(12.345);
    expect(snapshot.entities.find((entityArg) => entityArg.id === 'volume_usage_m3')?.state).toEqual(123.456);
    expect(snapshot.entities.find((entityArg) => entityArg.id === 'meter_date_time')?.state).toEqual('2026-03-04T05:06:07.000Z');
    expect(entities.find((entityArg) => entityArg.attributes?.key === 'device_number')?.state).toEqual('66153690');
    expect(refresh.success).toBeTrue();
    await runtime.destroy();
  } finally {
    await fs.rm(directory, { recursive: true, force: true });
  }
});

tap.test('exposes Landis+Gyr Heat Meter runtime and unsupported control without executor', async () => {
  const integration = new LandisgyrHeatMeterIntegration();
  expect(integration.status).toEqual("read-only-runtime");
  expect(landisgyrHeatMeterProfile.metadata.configFlow).toEqual(true);
  expect(landisgyrHeatMeterProfile.metadata.requirements).toEqual([
    "ultraheat-api==0.5.7",
  ]);
  expect(JSON.stringify(landisgyrHeatMeterProfile.metadata.localApi)).toContain('pyserial');

  const runtime = await integration.setup({ name: "Landis+Gyr Heat Meter Runtime", rawData }, {});
  const statusResult = await runtime.callService!({ domain: "landisgyr_heat_meter", service: 'status', target: {} });
  const refresh = await runtime.callService!({ domain: "landisgyr_heat_meter", service: 'refresh', target: {} });
  const snapshot = statusResult.data as ILandisgyrHeatMeterSnapshot;

  expect(statusResult.success).toBeTrue();
  expect(refresh.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual("Landis+Gyr Heat Meter Device");

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