import { test, expect } from "@playwright/test";
import * as fs from "fs";
import * as path from "path";

const REPO_ROOT = path.resolve(__dirname, "../..");
const AGENT_DIR = path.join(REPO_ROOT, "agents/test-personality");
const LAUNCHER = path.join(REPO_ROOT, "cmd/launcher/main.go");

test.describe("create-agent pipeline (validacion estructural)", () => {
  test("agents/test-personality/agent.go existe y exporta Rules()", () => {
    const agentGo = path.join(AGENT_DIR, "agent.go");
    expect(fs.existsSync(agentGo)).toBe(true);

    const content = fs.readFileSync(agentGo, "utf-8");
    expect(content).toContain("func Rules()");
    expect(content).toContain('agents.Register("test-personality"');
    // Agent (not robot) should have actual rules, not nil
    expect(content).toContain("ActionKindLLM");
  });

  test("agents/test-personality/config.yaml tiene type: agent (default)", () => {
    const configYaml = path.join(AGENT_DIR, "config.yaml");
    expect(fs.existsSync(configYaml)).toBe(true);

    const content = fs.readFileSync(configYaml, "utf-8");
    expect(content).toMatch(/id:\s*test-personality/);
    expect(content).toMatch(/enabled:\s*true/);
    // Should NOT have type: robot
    expect(content).not.toMatch(/type:\s*robot/);
  });

  test("agents/test-personality/prompts/system.md existe con personalidad", () => {
    const systemPrompt = path.join(AGENT_DIR, "prompts/system.md");
    expect(fs.existsSync(systemPrompt)).toBe(true);

    const content = fs.readFileSync(systemPrompt, "utf-8");
    // Pirate space personality keywords
    expect(content.toLowerCase()).toContain("pirata");
    expect(content.toLowerCase()).toContain("cosmonauta");
    expect(content.toLowerCase()).toContain("estelar");
    // Security section
    expect(content.toLowerCase()).toContain("seguridad");
    expect(content).toContain("instrucciones obligatorias");
  });

  test("config.yaml tiene LLM configurado (openai/gpt-4o)", () => {
    const configYaml = path.join(AGENT_DIR, "config.yaml");
    const content = fs.readFileSync(configYaml, "utf-8");
    expect(content).toMatch(/provider:\s*openai/);
    expect(content).toMatch(/model:\s*"?gpt-4o"?/);
  });

  test("config.yaml tiene encryption habilitada", () => {
    const configYaml = path.join(AGENT_DIR, "config.yaml");
    const content = fs.readFileSync(configYaml, "utf-8");
    expect(content).toMatch(/encryption:[\s\S]*?enabled:\s*true/);
  });

  test("cmd/launcher/main.go tiene import de test-personality", () => {
    const content = fs.readFileSync(LAUNCHER, "utf-8");
    expect(content).toContain("agents/test-personality");
  });
});
