import { expect, tap } from '@git.zone/tstest/tapbundle';
import { FireflyIiiClient, FireflyIiiConfigFlow, FireflyIiiIntegration, FireflyIiiMapper, HomeAssistantFireflyIiiIntegration, createFireflyIiiDiscoveryDescriptor, fireflyIiiProfile, type IFireflyIiiSnapshot } from '../../ts/integrations/firefly_iii/index.js';

const rawData = {
  device: {
    id: 'firefly-local',
    name: 'Firefly III Local',
    manufacturer: 'Firefly III',
    model: 'Firefly III',
    configurationUrl: 'https://firefly.local',
  },
  entities: [
    { id: 'checking_balance', name: 'Checking Balance', platform: 'sensor', state: 1234.56, unit: 'EUR', deviceClass: 'monetary' },
    { id: 'groceries_budget', name: 'Groceries Budget', platform: 'sensor', state: 210.14, unit: 'EUR', deviceClass: 'monetary' },
  ],
};

tap.test('matches manual Firefly III candidates and creates config flow output', async () => {
  const descriptor = createFireflyIiiDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'firefly_iii-manual-match');
  const result = await matcher!.matches({ url: 'https://firefly.local', name: 'Firefly III', metadata: { rawData, apiKey: 'token' } }, {});

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

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

  const done = await (await new FireflyIiiConfigFlow().start(result.candidate!, {})).submit!({});
  expect(done.kind).toEqual('done');
  expect(done.config?.host).toEqual('firefly.local');
  expect(done.config?.apiKey).toEqual('token');
});

tap.test('maps Firefly III raw snapshots to runtime devices and entities', async () => {
  const client = new FireflyIiiClient({ rawData });
  const snapshot = await client.getSnapshot();
  const devices = FireflyIiiMapper.toDevices(snapshot);
  const entities = FireflyIiiMapper.toEntities(snapshot);

  expect(snapshot.online).toBeTrue();
  expect(snapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual('firefly_iii');
  expect(devices[0].manufacturer).toEqual('Firefly III');
  expect(entities.length).toEqual(2);
  expect(entities[0].attributes?.deviceClass).toEqual('monetary');
});

tap.test('exposes Firefly III read-only runtime, HA alias, and unsupported control', async () => {
  const integration = new FireflyIiiIntegration();
  const alias = new HomeAssistantFireflyIiiIntegration();
  expect(alias.domain).toEqual('firefly_iii');
  expect(integration.status).toEqual('read-only-runtime');
  expect(fireflyIiiProfile.metadata.configFlow).toEqual(true);
  expect(fireflyIiiProfile.metadata.requirements).toEqual(['pyfirefly==0.1.12']);

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

  expect(status.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual('Firefly III Local');

  const command = await runtime.callService!({ domain: 'firefly_iii', service: 'turn_on', target: {} });
  expect(command.success).toBeFalse();
  await runtime.destroy();
});

export default tap.start();
