import { expect, tap } from '@git.zone/tstest/tapbundle';
import { Fail2banClient, Fail2banConfigFlow, Fail2banIntegration, Fail2banMapper, HomeAssistantFail2banIntegration, createFail2banDiscoveryDescriptor, fail2banProfile, type IFail2banSnapshot } from '../../ts/integrations/fail2ban/index.js';

const rawData = {
  device: {
    id: 'fail2ban-host',
    name: 'Fail2Ban Host',
  },
  entities: [
    {
      id: 'ssh_current_bans',
      name: 'fail2ban ssh',
      platform: 'sensor',
      state: '203.0.113.15',
      attributes: {
        current_bans: ['203.0.113.15'],
        total_bans: ['198.51.100.4', '203.0.113.15'],
        jail: 'ssh',
      },
    },
  ],
};

tap.test('matches manual Fail2Ban candidates and creates config flow output', async () => {
  const descriptor = createFail2banDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'fail2ban-manual-match');
  const result = await matcher!.matches({ name: 'Fail2Ban', metadata: { rawData, jails: ['ssh'], filePath: '/var/log/fail2ban.log' } }, {});

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

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

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

tap.test('maps Fail2Ban raw snapshots to runtime devices and entities', async () => {
  const client = new Fail2banClient({ name: 'Fail2Ban Runtime', rawData });
  const snapshot = await client.getSnapshot();
  const devices = Fail2banMapper.toDevices(snapshot);
  const entities = Fail2banMapper.toEntities(snapshot);

  expect(snapshot.online).toBeTrue();
  expect(snapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual('fail2ban');
  expect(devices[0].model).toEqual('Fail2Ban log parser');
  expect(entities[0].state).toEqual('203.0.113.15');
});

tap.test('exposes Fail2Ban read-only runtime, HA alias, and unsupported control', async () => {
  const integration = new Fail2banIntegration();
  const alias = new HomeAssistantFail2banIntegration();
  expect(alias.domain).toEqual('fail2ban');
  expect(integration.status).toEqual('read-only-runtime');
  expect(fail2banProfile.metadata.configFlow).toEqual(false);
  expect(fail2banProfile.metadata.qualityScale).toEqual('legacy');
  expect(fail2banProfile.metadata.requirements).toEqual([]);

  const runtime = await integration.setup({ name: 'Fail2Ban Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'fail2ban', service: 'status', target: {} });
  const snapshot = status.data as IFail2banSnapshot;

  expect(status.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual('Fail2Ban Host');

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

export default tap.start();
