import { describe, it, expect, beforeEach } from 'vitest';

import { _initTestDatabase, getAllChats, storeChatMetadata } from './db.js';
import { getAvailableGroups, _setRegisteredGroups } from './index.js';

beforeEach(() => {
  _initTestDatabase();
  _setRegisteredGroups({});
});

// --- getAvailableGroups ---

describe('getAvailableGroups', () => {
  it('returns only groups, excludes DMs', () => {
    storeChatMetadata(
      'tg:-1001111111111',
      '2024-01-01T00:00:01.000Z',
      'Group 1',
      'telegram',
      true,
    );
    storeChatMetadata(
      'tg:123456789',
      '2024-01-01T00:00:02.000Z',
      'User DM',
      'telegram',
      false,
    );
    storeChatMetadata(
      'tg:-1002222222222',
      '2024-01-01T00:00:03.000Z',
      'Group 2',
      'telegram',
      true,
    );

    const groups = getAvailableGroups();
    expect(groups).toHaveLength(2);
    expect(groups.map((g) => g.jid)).toContain('tg:-1001111111111');
    expect(groups.map((g) => g.jid)).toContain('tg:-1002222222222');
    expect(groups.map((g) => g.jid)).not.toContain('tg:123456789');
  });

  it('excludes __group_sync__ sentinel', () => {
    storeChatMetadata('__group_sync__', '2024-01-01T00:00:00.000Z');
    storeChatMetadata(
      'tg:-1001111111111',
      '2024-01-01T00:00:01.000Z',
      'Group',
      'telegram',
      true,
    );

    const groups = getAvailableGroups();
    expect(groups).toHaveLength(1);
    expect(groups[0].jid).toBe('tg:-1001111111111');
  });

  it('marks registered groups correctly', () => {
    storeChatMetadata(
      'tg:-1001111111111',
      '2024-01-01T00:00:01.000Z',
      'Registered',
      'telegram',
      true,
    );
    storeChatMetadata(
      'tg:-1002222222222',
      '2024-01-01T00:00:02.000Z',
      'Unregistered',
      'telegram',
      true,
    );

    _setRegisteredGroups({
      'tg:-1001111111111': {
        name: 'Registered',
        folder: 'registered',
        trigger: '@Andy',
        added_at: '2024-01-01T00:00:00.000Z',
      },
    });

    const groups = getAvailableGroups();
    const reg = groups.find((g) => g.jid === 'tg:-1001111111111');
    const unreg = groups.find((g) => g.jid === 'tg:-1002222222222');

    expect(reg?.isRegistered).toBe(true);
    expect(unreg?.isRegistered).toBe(false);
  });

  it('returns groups ordered by most recent activity', () => {
    storeChatMetadata(
      'tg:-1001000000001',
      '2024-01-01T00:00:01.000Z',
      'Old',
      'telegram',
      true,
    );
    storeChatMetadata(
      'tg:-1001000000002',
      '2024-01-01T00:00:05.000Z',
      'New',
      'telegram',
      true,
    );
    storeChatMetadata(
      'tg:-1001000000003',
      '2024-01-01T00:00:03.000Z',
      'Mid',
      'telegram',
      true,
    );

    const groups = getAvailableGroups();
    expect(groups[0].jid).toBe('tg:-1001000000002');
    expect(groups[1].jid).toBe('tg:-1001000000003');
    expect(groups[2].jid).toBe('tg:-1001000000001');
  });

  it('excludes non-group chats regardless of JID format', () => {
    storeChatMetadata(
      'unknown-format-123',
      '2024-01-01T00:00:01.000Z',
      'Unknown',
    );
    storeChatMetadata(
      'tg:123456789',
      '2024-01-01T00:00:02.000Z',
      'DM',
      'telegram',
      false,
    );
    storeChatMetadata(
      'tg:-1001111111111',
      '2024-01-01T00:00:03.000Z',
      'Group',
      'telegram',
      true,
    );

    const groups = getAvailableGroups();
    expect(groups).toHaveLength(1);
    expect(groups[0].jid).toBe('tg:-1001111111111');
  });

  it('returns empty array when no chats exist', () => {
    const groups = getAvailableGroups();
    expect(groups).toHaveLength(0);
  });
});
