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 { setDb } from '../../../src/db/locator';

describe('CommandService - formato dd/MM en listados', () => {
  let memdb: Database;

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

  beforeEach(() => {
    process.env.NODE_ENV = 'test';
    process.env.TZ = 'Europe/Madrid';
    memdb.exec('DELETE FROM task_assignments; DELETE FROM tasks;');
  });

  it('"t ver mis" muestra fechas en dd/MM', async () => {
    const sender = '600111222';

    // Crear una tarea con due_date conocida y asignada al usuario (DM → asignada al creador)
    await CommandService.handle({
      sender,
      groupId: `${sender}@s.whatsapp.net`, // DM
      message: 't n tarea listada 2099-01-05',
      mentions: [],
    });

    // Listar "mis" y comprobar formato
    const responses = await CommandService.handle({
      sender,
      groupId: `${sender}@s.whatsapp.net`,
      message: 't ver mis',
      mentions: [],
    });

    expect(responses.length).toBe(1);
    expect(responses[0].recipient).toBe(sender);
    expect(responses[0].message).toContain('📅 05/01');
  });
});
