import { expect, tap } from '@git.zone/tstest/tapbundle';
import { FolderClient, FolderConfigFlow, FolderIntegration, FolderMapper, HomeAssistantFolderIntegration, createFolderDiscoveryDescriptor, folderProfile, type IFolderSnapshot, type TFolderRawData } from '../../ts/integrations/folder/index.js';

const fixtureFolder = 'test/folder';
const rawData: TFolderRawData = {
  folder: fixtureFolder,
  filter: '*.txt',
  number_of_files: 1,
  bytes: 12,
  file_list: [`${fixtureFolder}/alpha.txt`],
};

tap.test('matches manual Folder candidates and creates config flow output', async () => {
  const descriptor = createFolderDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'folder-manual-match');
  const result = await matcher!.matches({ source: 'manual', name: 'Folder sensor', metadata: { rawData, folder: fixtureFolder, folderPath: fixtureFolder, filter: '*.txt' } }, {});

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

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

  const done = await (await new FolderConfigFlow().start(result.candidate!, {})).submit!({});
  expect(done.kind).toEqual('done');
  expect(done.config?.rawData).toEqual(rawData);
  expect(done.config?.metadata?.folderPath).toEqual(fixtureFolder);
});

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

  expect(snapshot.online).toBeTrue();
  expect(devices[0].integrationDomain).toEqual('folder');
  expect(devices[0].model).toEqual('Local Folder Sensor');
  expect(entities.length).toEqual(1);
  expect(entities[0].attributes?.bytes).toEqual(12);
  expect(entities[0].attributes?.number_of_files).toEqual(1);
});

tap.test('reads local Folder snapshots and exposes read-only runtime with unsupported control', async () => {
  const integration = new FolderIntegration();
  const alias = new HomeAssistantFolderIntegration();
  expect(alias instanceof FolderIntegration).toBeTrue();
  expect(alias.domain).toEqual('folder');
  expect(integration.status).toEqual('read-only-runtime');
  expect(folderProfile.metadata.configFlow).toEqual(false);
  expect(folderProfile.metadata.qualityScale).toEqual('legacy');
  expect(folderProfile.metadata.requirements).toEqual([]);

  const runtime = await integration.setup({ folder: fixtureFolder, filter: '*.txt' }, {});
  const status = await runtime.callService!({ domain: 'folder', service: 'status', target: {} });
  const refresh = await runtime.callService!({ domain: 'folder', service: 'refresh', target: {} });
  const snapshot = status.data as IFolderSnapshot;

  expect(status.success).toBeTrue();
  expect(refresh.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect(snapshot.entities[0].attributes?.number_of_files).toEqual(1);
  expect(Number(snapshot.entities[0].attributes?.bytes) > 0).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual('folder');

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