import { expect, tap } from '@git.zone/tstest/tapbundle';
import { FileClient, FileConfigFlow, FileIntegration, FileMapper, HomeAssistantFileIntegration, createFileDiscoveryDescriptor, fileProfile, type IFileSnapshot } from '../../ts/integrations/file/index.js';

const rawData = {
  filePath: 'test/file/sample.log',
  latestEntry: '42.5',
  contentBytes: 24,
};

tap.test('matches manual File candidates and creates config flow output', async () => {
  const descriptor = createFileDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'file-manual-match');
  const result = await matcher!.matches({ name: 'Local file sensor', metadata: { rawData, filePath: 'test/file/sample.log', platform: 'sensor' } }, {});

  expect(result.matched).toBeTrue();
  expect(result.candidate?.integrationDomain).toEqual('file');

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

  const done = await (await new FileConfigFlow().start(result.candidate!, {})).submit!({});
  expect(done.kind).toEqual('done');
  expect(done.config?.rawData).toEqual(rawData);
  expect(done.config?.metadata?.filePath).toEqual('test/file/sample.log');
});

tap.test('maps File raw snapshots to devices and entities', async () => {
  const client = new FileClient({ name: 'Temperature Log', rawData, unitOfMeasurement: 'C' });
  const snapshot = await client.getSnapshot();
  const devices = FileMapper.toDevices(snapshot);
  const entities = FileMapper.toEntities(snapshot);

  expect(snapshot.online).toBeTrue();
  expect(devices[0].integrationDomain).toEqual('file');
  expect(devices[0].model).toEqual('Local File');
  expect(entities.length).toEqual(1);
  expect(entities[0].state).toEqual('42.5');
  expect(entities[0].attributes?.unit).toEqual('C');
});

tap.test('reads local File snapshots and exposes read-only runtime with unsupported control', async () => {
  const integration = new FileIntegration();
  const alias = new HomeAssistantFileIntegration();
  expect(alias.domain).toEqual('file');
  expect(integration.status).toEqual('read-only-runtime');
  expect(fileProfile.metadata.configFlow).toEqual(true);
  expect(fileProfile.metadata.requirements).toEqual(['file-read-backwards==2.0.0']);
  expect(Object.prototype.hasOwnProperty.call(fileProfile.metadata, 'qualityScale')).toBeTrue();
  expect(fileProfile.metadata.qualityScale).toBeUndefined();

  const runtime = await integration.setup({ name: 'File Runtime', filePath: 'test/file/sample.log' }, {});
  const status = await runtime.callService!({ domain: 'file', service: 'status', target: {} });
  const snapshot = status.data as IFileSnapshot;

  expect(status.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect(snapshot.entities[0].state).toEqual('latest value');
  expect((await runtime.devices())[0].name).toEqual('File Runtime');

  const command = await runtime.callService!({ domain: 'file', service: 'turn_on', target: {} });
  expect(command.success).toBeFalse();
  expect(command.error?.includes('requires an injected client.execute() or commandExecutor')).toBeTrue();
  await runtime.destroy();
});

export default tap.start();
