import { expect, tap } from '@git.zone/tstest/tapbundle';
import { HolidayClient, HolidayConfigFlow, HolidayIntegration, HolidayMapper, HomeAssistantHolidayIntegration, createHolidayDiscoveryDescriptor, holidayProfile, type IHolidaySnapshot, type THolidayRawData } from '../../ts/integrations/holiday/index.js';

const rawData: THolidayRawData = {
  country: 'US',
  holidays: [
    { date: '2099-01-01', name: "New Year's Day" },
    { date: '2099-12-25', name: 'Christmas Day' },
  ],
};

tap.test('matches manual Holiday candidates and creates config flow output', async () => {
  const descriptor = createHolidayDiscoveryDescriptor();
  const matcher = descriptor.getMatchers().find((matcherArg) => matcherArg.id === 'holiday-manual-match');
  const result = await matcher!.matches({ source: 'manual', id: 'us-holidays', name: 'US Holidays', metadata: { rawData } }, {});

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

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

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

tap.test('maps Holiday raw snapshots to runtime devices and entities', async () => {
  const client = new HolidayClient({ name: 'US Holidays', country: 'US', rawData });
  const snapshot = await client.getSnapshot();
  const mappedSnapshot = HolidayMapper.toSnapshotFromRaw({ name: 'US Holidays', country: 'US' }, rawData);
  const devices = HolidayMapper.toDevices(mappedSnapshot);
  const entities = HolidayMapper.toEntities(mappedSnapshot);

  expect(snapshot.online).toBeTrue();
  expect(mappedSnapshot.source).toEqual('manual');
  expect(devices[0].integrationDomain).toEqual('holiday');
  expect(devices[0].manufacturer).toEqual('Home Assistant');
  expect(entities.some((entityArg) => entityArg.id === 'sensor.us_holidays_next_holiday')).toBeTrue();
  expect(entities.some((entityArg) => entityArg.id === 'binary_sensor.us_holidays_holiday_today')).toBeTrue();
});

tap.test('exposes Holiday read-only runtime, HA alias, and unsupported control', async () => {
  const integration = new HolidayIntegration();
  const alias = new HomeAssistantHolidayIntegration();
  expect(alias instanceof HolidayIntegration).toBeTrue();
  expect(alias.domain).toEqual('holiday');
  expect(integration.status).toEqual('read-only-runtime');
  expect(holidayProfile.metadata.configFlow).toEqual(true);
  expect(holidayProfile.metadata.requirements).toEqual(['holidays==0.95', 'babel==2.15.0']);
  expect(holidayProfile.metadata.qualityScale).toBeUndefined();

  const runtime = await integration.setup({ name: 'US Holidays', country: 'US', rawData }, {});
  const status = await runtime.callService!({ domain: 'holiday', service: 'status', target: {} });
  const refresh = await runtime.callService!({ domain: 'holiday', service: 'refresh', target: {} });
  const snapshot = status.data as IHolidaySnapshot;

  expect(status.success).toBeTrue();
  expect(refresh.success).toBeTrue();
  expect(snapshot.online).toBeTrue();
  expect((await runtime.devices())[0].name).toEqual('US Holidays');

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