import { test, expect, handleElementDialogs } from "../fixtures/persistent-context";
import {
  goToRoom,
  sendMessage,
  waitForBotReply,
  assertNoDecryptionErrors,
} from "../fixtures/matrix-room";

// Keywords that indicate pirate-space personality.
// The LLM is non-deterministic, so we check for presence of ANY keyword from the set.
const PIRATE_SPACE_KEYWORDS = [
  "arrr",
  "cosmonauta",
  "estelar",
  "marea",
  "nave",
  "galaxia",
  "estrella",
  "pirata",
  "capitan",
  "nebulosa",
  "intergal",
  "asteroide",
  "meteorito",
  "agujero negro",
  "tripulacion",
  "🏴‍☠️",
  "🚀",
  "⭐",
  "🌌",
  "☄️",
  "💀",
];

function containsPirateKeyword(text: string): boolean {
  const lower = text.toLowerCase();
  return PIRATE_SPACE_KEYWORDS.some((kw) => lower.includes(kw.toLowerCase()));
}

test.describe("test-personality (pirata espacial)", () => {
  test.beforeEach(async ({ page }) => {
    await page.goto("/");
    await handleElementDialogs(page);
    await goToRoom(page, "Test Personality");
  });

  test("responde a saludo con personalidad pirata espacial", async ({ page }) => {
    await sendMessage(page, "Hola, como estas?");

    const reply = await waitForBotReply(page, {
      timeout: 60_000,
      sender: "Test Personality",
    });
    expect(reply).toBeTruthy();
    expect(reply.length).toBeGreaterThan(20);
    expect(containsPirateKeyword(reply)).toBe(true);
  });

  test("personalidad consistente en respuestas serias", async ({ page }) => {
    await sendMessage(page, "Que es la fotosintesis? Responde en una frase.");

    const reply = await waitForBotReply(page, {
      timeout: 60_000,
      sender: "Test Personality",
    });
    expect(reply).toBeTruthy();
    expect(reply.length).toBeGreaterThan(20);
    // Should contain real content about photosynthesis
    expect(reply.toLowerCase()).toMatch(/luz|sol|planta|energia|clorofila|carbon/i);
    // And still maintain pirate personality
    expect(containsPirateKeyword(reply)).toBe(true);
  });

  test("!help muestra lista de comandos", async ({ page }) => {
    await sendMessage(page, "!help");

    const reply = await waitForBotReply(page, {
      timeout: 10_000,
      sender: "Test Personality",
    });
    expect(reply).toBeTruthy();
    expect(reply.toLowerCase()).toContain("help");
    expect(reply.toLowerCase()).toContain("ping");
  });

  test("!ping responde", async ({ page }) => {
    await sendMessage(page, "!ping");

    const reply = await waitForBotReply(page, {
      timeout: 10_000,
      sender: "Test Personality",
    });
    expect(reply).toBeTruthy();
  });

  test("no hay errores de E2EE en el timeline", async ({ page }) => {
    await assertNoDecryptionErrors(page);
  });
});
