import { expect, tap } from '@git.zone/tstest/tapbundle';
import { FamilyhubClient, FamilyhubConfigFlow, FamilyhubIntegration, FamilyhubMapper, HomeAssistantFamilyhubIntegration, createFamilyhubDiscoveryDescriptor, familyhubProfile, type IFamilyhubSnapshot } from '../../ts/integrations/familyhub/index.js';

const rawData = {
  device: {
    id: 'familyhub-kitchen',
    name: 'Kitchen Family Hub',
    manufacturer: 'Samsung',
    model: 'Family Hub refrigerator',
    host: '192.0.2.44',
    port: 17654,
  },
  entities: [
    {
      id: 'camera_info',
      name: 'Kitchen FamilyHub Camera',
      platform: 'sensor',
      state: 'available',
      attributes: {
        glazeUrls: ['/camera1.jpg', '/camera2.jpg'],
        stillImagePath: '/.krate/owner/share/scloud/glazeCameraInfo.txt',
      },
    },
  ],
};

tap.test('matches manual Family Hub candidates and creates config flow output', async () => {
  const descriptor = createFamilyhubDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'familyhub-manual-match');
  const result = await matcher!.matches({ host: 'familyhub.local', name: 'Samsung Family Hub', metadata: { rawData } }, {});

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

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

  const done = await (await new FamilyhubConfigFlow().start(result.candidate!, {})).submit!({});
  expect(done.kind).toEqual('done');
  expect(done.config?.host).toEqual('familyhub.local');
  expect(done.config?.port).toEqual(17654);
  expect(done.config?.path).toEqual('/.krate/owner/share/scloud/glazeCameraInfo.txt');
});

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

  expect(snapshot.online).toBeTrue();
  expect(snapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual('familyhub');
  expect(devices[0].manufacturer).toEqual('Samsung');
  expect(entities[0].state).toEqual('available');
});

tap.test('exposes Family Hub read-only runtime, HA alias, and unsupported control', async () => {
  const integration = new FamilyhubIntegration();
  const alias = new HomeAssistantFamilyhubIntegration();
  expect(alias.domain).toEqual('familyhub');
  expect(integration.status).toEqual('read-only-runtime');
  expect(familyhubProfile.metadata.configFlow).toEqual(false);
  expect(familyhubProfile.metadata.qualityScale).toEqual('legacy');
  expect(familyhubProfile.metadata.requirements).toEqual(['python-family-hub-local==0.0.2']);

  const runtime = await integration.setup({ name: 'Family Hub Runtime', rawData }, {});
  const status = await runtime.callService!({ domain: 'camera', service: 'status', target: {} });
  const snapshot = status.data as IFamilyhubSnapshot;

  expect(status.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual('Kitchen Family Hub');

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

export default tap.start();
