import { describe, it, expect, beforeAll, beforeEach } from 'bun:test';
import { Database } from 'bun:sqlite';
import { initializeDatabase } from '../../../src/db';
import { TaskService } from '../../../src/tasks/service';
import { CommandService } from '../../../src/services/command';
import { Metrics } from '../../../src/services/metrics';
import { setDb } from '../../../src/db/locator';

describe('CommandService - autoasignación con "yo" / "@yo"', () => {
  let memdb: Database;

  beforeAll(() => {
    memdb = new Database(':memory:');
    initializeDatabase(memdb);
    setDb(memdb);
  });

  beforeEach(() => {
    process.env.NODE_ENV = 'test';
    process.env.METRICS_ENABLED = 'true';
    Metrics.reset?.();

    memdb.exec(`
      DELETE FROM task_assignments;
      DELETE FROM tasks;
      DELETE FROM users;
      DELETE FROM user_preferences;
    `);
  });

  function getLastTask() {
    return memdb.prepare(`SELECT id, description FROM tasks ORDER BY id DESC LIMIT 1`).get() as any;
  }

  function getAssignees(taskId: number): string[] {
    const rows = memdb.prepare(`SELECT user_id FROM task_assignments WHERE task_id = ? ORDER BY assigned_at ASC`).all(taskId) as any[];
    return rows.map(r => String(r.user_id));
  }

  it('en grupo: "yo" autoasigna al remitente y no queda en la descripción', async () => {
    const sender = '600111222';
    await CommandService.handle({
      sender,
      groupId: '12345@g.us', // contexto grupo
      message: 't n Hacer algo yo',
      mentions: [],
    });

    const t = getLastTask();
    const assignees = getAssignees(Number(t.id));
    expect(assignees).toContain(sender);
    expect(String(t.description)).toBe('Hacer algo');
  });

  it('en grupo: "@yo" autoasigna y no incrementa métricas de fallo', async () => {
    const sender = '600222333';
    await CommandService.handle({
      sender,
      groupId: 'group@g.us',
      message: 't n Revisar docs @yo',
      mentions: [],
    });

    const t = getLastTask();
    const assignees = getAssignees(Number(t.id));
    expect(assignees).toContain(sender);
    expect(String(t.description)).toBe('Revisar docs');

    const prom = Metrics.render?.('prom') || '';
    expect(prom).not.toContain('onboarding_assign_failures_total');
  });

  it('no falsos positivos: "yoyo" y "hoyo" no autoasignan en grupo (queda sin dueño)', async () => {
    const sender = '600333444';
    // yoyo
    await CommandService.handle({
      sender,
      groupId: 'grp@g.us',
      message: 't n Caso yoyo',
      mentions: [],
    });
    let t = getLastTask();
    let assignees = getAssignees(Number(t.id));
    expect(assignees.length).toBe(0);

    // hoyo
    await CommandService.handle({
      sender,
      groupId: 'grp@g.us',
      message: 't n Voy a cavar un hoyo',
      mentions: [],
    });
    t = getLastTask();
    assignees = getAssignees(Number(t.id));
    expect(assignees.length).toBe(0);
  });

  it('combinado: "yo @34600123456" asigna al remitente y al otro usuario', async () => {
    const sender = '600444555';
    await CommandService.handle({
      sender,
      groupId: 'g@g.us',
      message: 't n Tarea combinada yo @34600123456',
      mentions: [],
    });

    const t = getLastTask();
    const assignees = getAssignees(Number(t.id));
    expect(new Set(assignees)).toEqual(new Set([sender, '34600123456']));
  });

  it('en DM: "yo" también se asigna al remitente y no queda en la descripción', async () => {
    const sender = '600555666';
    await CommandService.handle({
      sender,
      groupId: `${sender}@s.whatsapp.net`, // DM
      message: 't n Mi tarea yo',
      mentions: [],
    });

    const t = getLastTask();
    const assignees = getAssignees(Number(t.id));
    expect(assignees).toContain(sender);
    expect(String(t.description)).toBe('Mi tarea');
  });

  it('en grupo: "@yo," autoasigna y no incrementa métricas de fallo', async () => {
    const sender = '600666777';
    await CommandService.handle({
      sender,
      groupId: 'group2@g.us',
      message: 't n Revisar algo @yo,',
      mentions: [],
    });

    const t = getLastTask();
    const assignees = getAssignees(Number(t.id));
    expect(assignees).toContain(sender);
    expect(String(t.description)).toBe('Revisar algo');

    const prom = Metrics.render?.('prom') || '';
    expect(prom).not.toContain('onboarding_assign_failures_total');
  });

  it('en grupo: "(yo)" autoasigna y no queda en la descripción', async () => {
    const sender = '600777888';
    await CommandService.handle({
      sender,
      groupId: 'grp2@g.us',
      message: 't n Hacer (yo)',
      mentions: [],
    });

    const t = getLastTask();
    const assignees = getAssignees(Number(t.id));
    expect(assignees).toContain(sender);
    expect(String(t.description)).toBe('Hacer');
  });
});
