import { expect, tap } from '@git.zone/tstest/tapbundle';
import { FilesizeClient, FilesizeConfigFlow, FilesizeIntegration, FilesizeMapper, HomeAssistantFilesizeIntegration, createFilesizeDiscoveryDescriptor, filesizeProfile, type IFilesizeSnapshot } from '../../ts/integrations/filesize/index.js';

const rawData = {
  filePath: 'test/filesize/sample.txt',
  file: 1.54,
  bytes: 1536000,
  lastUpdated: '2026-01-02T03:04:05.000Z',
  created: '2026-01-01T03:04:05.000Z',
};

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

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

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

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

tap.test('maps File Size raw snapshots to devices and entities', async () => {
  const client = new FilesizeClient({ rawData });
  const snapshot = await client.getSnapshot();
  const devices = FilesizeMapper.toDevices(snapshot);
  const entities = FilesizeMapper.toEntities(snapshot);

  expect(snapshot.online).toBeTrue();
  expect(devices[0].integrationDomain).toEqual('filesize');
  expect(devices[0].model).toEqual('Local File Statistics');
  expect(entities.length).toEqual(4);
  expect(entities[0].state).toEqual(1.54);
  expect(entities[1].attributes?.unit).toEqual('B');
  expect(entities[2].attributes?.deviceClass).toEqual('timestamp');
});

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

  const runtime = await integration.setup({ filePath: 'test/filesize/sample.txt' }, {});
  const status = await runtime.callService!({ domain: 'filesize', service: 'status', target: {} });
  const snapshot = status.data as IFilesizeSnapshot;

  expect(status.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect(Number(snapshot.entities.find((entityArg) => entityArg.id === 'bytes')?.state) > 0).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual('sample.txt');

  const command = await runtime.callService!({ domain: 'filesize', 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();
