import { describe, it, expect, beforeEach, afterEach } from 'bun:test';
import { makeMemDb } from '../../helpers/db';
import { CommandService } from '../../../src/services/command';
import { AllowedGroups } from '../../../src/services/allowed-groups';

describe('CommandService - gating en modo enforce', () => {
  const envBackup = process.env;

  beforeEach(() => {
    process.env = { ...envBackup, NODE_ENV: 'test', GROUP_GATING_MODE: 'enforce' };
    const memdb = makeMemDb();
    (CommandService as any).dbInstance = memdb;
    (AllowedGroups as any).dbInstance = memdb;
  });

  afterEach(() => {
    process.env = envBackup;
  });

  it('bloquea comandos en grupo no permitido (desconocido)', async () => {
    const res = await CommandService.handle({
      sender: '34600123456',
      groupId: 'g1@g.us',
      message: '/t ayuda',
      mentions: []
    });
    expect(Array.isArray(res)).toBe(true);
    expect(res.length).toBe(0);
  });

  it('permite comandos en grupo permitido', async () => {
    AllowedGroups.setStatus('g2@g.us', 'allowed', 'G2');

    const res = await CommandService.handle({
      sender: '34600123456',
      groupId: 'g2@g.us',
      message: '/t ayuda',
      mentions: []
    });

    expect(res.length).toBeGreaterThan(0);
    expect(res[0].recipient).toBe('34600123456');
  });
});
