import { join } from "node:path";

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

import {
  RULESYNC_OVERVIEW_FILE_NAME,
  RULESYNC_RELATIVE_DIR_PATH,
  RULESYNC_RULES_RELATIVE_DIR_PATH,
} from "../../constants/rulesync-paths.js";
import { setupTestDirectory } from "../../test-utils/test-directories.js";
import { ensureDir, writeFileContent } from "../../utils/file.js";
import { GeminiCliRule } from "./geminicli-rule.js";
import { RulesyncRule } from "./rulesync-rule.js";

describe("GeminiCliRule", () => {
  let testDir: string;
  let cleanup: () => Promise<void>;

  beforeEach(async () => {
    ({ testDir, cleanup } = await setupTestDirectory());
    vi.spyOn(process, "cwd").mockReturnValue(testDir);
  });

  afterEach(async () => {
    await cleanup();
    vi.restoreAllMocks();
  });

  describe("constructor", () => {
    it("should create instance with default parameters", () => {
      const geminiCliRule = new GeminiCliRule({
        relativeDirPath: ".gemini/memories",
        relativeFilePath: "test-rule.md",
        fileContent: "# Test Rule\n\nThis is a test rule.",
      });

      expect(geminiCliRule).toBeInstanceOf(GeminiCliRule);
      expect(geminiCliRule.getRelativeDirPath()).toBe(".gemini/memories");
      expect(geminiCliRule.getRelativeFilePath()).toBe("test-rule.md");
      expect(geminiCliRule.getFileContent()).toBe("# Test Rule\n\nThis is a test rule.");
    });

    it("should create instance with custom outputRoot", () => {
      const geminiCliRule = new GeminiCliRule({
        outputRoot: "/custom/path",
        relativeDirPath: ".gemini/memories",
        relativeFilePath: "test-rule.md",
        fileContent: "# Custom Rule",
      });

      expect(geminiCliRule.getFilePath()).toBe("/custom/path/.gemini/memories/test-rule.md");
    });

    it("should validate content by default", () => {
      expect(() => {
        const _instance = new GeminiCliRule({
          relativeDirPath: ".gemini/memories",
          relativeFilePath: "test-rule.md",
          fileContent: "", // empty content should be valid since validate always returns success
        });
      }).not.toThrow();
    });

    it("should skip validation when requested", () => {
      expect(() => {
        const _instance = new GeminiCliRule({
          relativeDirPath: ".gemini/memories",
          relativeFilePath: "test-rule.md",
          fileContent: "",
          validate: false,
        });
      }).not.toThrow();
    });

    it("should handle root rule parameter", () => {
      const geminiCliRule = new GeminiCliRule({
        relativeDirPath: ".",
        relativeFilePath: "GEMINI.md",
        fileContent: "# Root Rule",
        root: true,
      });

      expect(geminiCliRule.getFileContent()).toBe("# Root Rule");
      expect(geminiCliRule.getFilePath()).toBe(join(testDir, "GEMINI.md"));
    });
  });

  describe("fromFile", () => {
    it("should create instance from existing non-root file", async () => {
      // Setup test file
      const memoriesDir = join(testDir, ".gemini/memories");
      await ensureDir(memoriesDir);
      const testContent = "# Test Rule from File\n\nContent from file.";
      await writeFileContent(join(memoriesDir, "test.md"), testContent);

      const geminiCliRule = await GeminiCliRule.fromFile({
        outputRoot: testDir,
        relativeFilePath: "test.md",
      });

      expect(geminiCliRule.getRelativeDirPath()).toBe(".gemini/memories");
      expect(geminiCliRule.getRelativeFilePath()).toBe("test.md");
      expect(geminiCliRule.getFileContent()).toBe(testContent);
      expect(geminiCliRule.getFilePath()).toBe(join(testDir, ".gemini/memories/test.md"));
    });

    it("should create instance from root GEMINI.md file", async () => {
      // Setup root test file
      const testContent = "# Root Gemini CLI Rule\n\nThis is the root configuration.";
      await writeFileContent(join(testDir, "GEMINI.md"), testContent);

      const geminiCliRule = await GeminiCliRule.fromFile({
        outputRoot: testDir,
        relativeFilePath: "GEMINI.md",
      });

      expect(geminiCliRule.getRelativeDirPath()).toBe(".");
      expect(geminiCliRule.getRelativeFilePath()).toBe("GEMINI.md");
      expect(geminiCliRule.getFileContent()).toBe(testContent);
      expect(geminiCliRule.getFilePath()).toBe(join(testDir, "GEMINI.md"));
    });

    it("should use default outputRoot when not provided", async () => {
      // Since process.cwd() is mocked to return testDir, default outputRoot will use testDir
      const memoriesDir = join(testDir, ".gemini/memories");
      await ensureDir(memoriesDir);
      const testContent = "# Default OutputRoot Test";
      const testFilePath = join(memoriesDir, "default-test.md");
      await writeFileContent(testFilePath, testContent);

      const geminiCliRule = await GeminiCliRule.fromFile({
        relativeFilePath: "default-test.md",
      });

      expect(geminiCliRule.getRelativeDirPath()).toBe(".gemini/memories");
      expect(geminiCliRule.getRelativeFilePath()).toBe("default-test.md");
      expect(geminiCliRule.getFileContent()).toBe(testContent);
      // Verify that the file path uses testDir (which is what process.cwd() returns when mocked)
      expect(geminiCliRule.getFilePath()).toBe(join(testDir, ".gemini/memories/default-test.md"));
    });

    it("should handle validation parameter", async () => {
      const memoriesDir = join(testDir, ".gemini/memories");
      await ensureDir(memoriesDir);
      const testContent = "# Validation Test";
      await writeFileContent(join(memoriesDir, "validation-test.md"), testContent);

      const geminiCliRuleWithValidation = await GeminiCliRule.fromFile({
        outputRoot: testDir,
        relativeFilePath: "validation-test.md",
        validate: true,
      });

      const geminiCliRuleWithoutValidation = await GeminiCliRule.fromFile({
        outputRoot: testDir,
        relativeFilePath: "validation-test.md",
        validate: false,
      });

      expect(geminiCliRuleWithValidation.getFileContent()).toBe(testContent);
      expect(geminiCliRuleWithoutValidation.getFileContent()).toBe(testContent);
    });

    it("should throw error when file does not exist", async () => {
      await expect(
        GeminiCliRule.fromFile({
          outputRoot: testDir,
          relativeFilePath: "nonexistent.md",
        }),
      ).rejects.toThrow();
    });
  });

  describe("fromRulesyncRule", () => {
    it("should create instance from RulesyncRule", () => {
      const rulesyncRule = new RulesyncRule({
        relativeDirPath: RULESYNC_RELATIVE_DIR_PATH,
        relativeFilePath: "test-rule.md",
        frontmatter: {
          root: false,
          targets: ["*"],
          description: "Test rule",
          globs: [],
        },
        body: "# Test RulesyncRule\n\nContent from rulesync.",
      });

      const geminiCliRule = GeminiCliRule.fromRulesyncRule({
        rulesyncRule,
      });

      expect(geminiCliRule).toBeInstanceOf(GeminiCliRule);
      expect(geminiCliRule.getRelativeDirPath()).toBe(".gemini/memories");
      expect(geminiCliRule.getRelativeFilePath()).toBe("test-rule.md");
      expect(geminiCliRule.getFileContent()).toContain(
        "# Test RulesyncRule\n\nContent from rulesync.",
      );
    });

    it("should create root rule from root RulesyncRule", () => {
      const rulesyncRule = new RulesyncRule({
        relativeDirPath: RULESYNC_RELATIVE_DIR_PATH,
        relativeFilePath: "root.md",
        frontmatter: {
          root: true,
          targets: ["*"],
          description: "Root rule",
          globs: [],
        },
        body: "# Root RulesyncRule\n\nRoot content from rulesync.",
      });

      const geminiCliRule = GeminiCliRule.fromRulesyncRule({
        rulesyncRule,
      });

      expect(geminiCliRule).toBeInstanceOf(GeminiCliRule);
      expect(geminiCliRule.getRelativeDirPath()).toBe(".");
      expect(geminiCliRule.getRelativeFilePath()).toBe("GEMINI.md");
      expect(geminiCliRule.getFileContent()).toContain(
        "# Root RulesyncRule\n\nRoot content from rulesync.",
      );
    });

    it("should use custom outputRoot", () => {
      const rulesyncRule = new RulesyncRule({
        relativeDirPath: RULESYNC_RELATIVE_DIR_PATH,
        relativeFilePath: "custom-base.md",
        frontmatter: {
          root: false,
          targets: ["*"],
          description: "",
          globs: [],
        },
        body: "# Custom Base Directory",
      });

      const geminiCliRule = GeminiCliRule.fromRulesyncRule({
        outputRoot: "/custom/base",
        rulesyncRule,
      });

      expect(geminiCliRule.getFilePath()).toBe("/custom/base/.gemini/memories/custom-base.md");
    });

    it("should handle validation parameter", () => {
      const rulesyncRule = new RulesyncRule({
        relativeDirPath: RULESYNC_RELATIVE_DIR_PATH,
        relativeFilePath: "validation.md",
        frontmatter: {
          root: false,
          targets: ["*"],
          description: "",
          globs: [],
        },
        body: "# Validation Test",
      });

      const geminiCliRuleWithValidation = GeminiCliRule.fromRulesyncRule({
        rulesyncRule,
        validate: true,
      });

      const geminiCliRuleWithoutValidation = GeminiCliRule.fromRulesyncRule({
        rulesyncRule,
        validate: false,
      });

      expect(geminiCliRuleWithValidation.getFileContent()).toContain("# Validation Test");
      expect(geminiCliRuleWithoutValidation.getFileContent()).toContain("# Validation Test");
    });
  });

  describe("toRulesyncRule", () => {
    it("should convert GeminiCliRule to RulesyncRule", () => {
      const geminiCliRule = new GeminiCliRule({
        outputRoot: testDir,
        relativeDirPath: ".gemini/memories",
        relativeFilePath: "convert-test.md",
        fileContent: "# Convert Test\n\nThis will be converted.",
      });

      const rulesyncRule = geminiCliRule.toRulesyncRule();

      expect(rulesyncRule).toBeInstanceOf(RulesyncRule);
      expect(rulesyncRule.getRelativeDirPath()).toBe(RULESYNC_RULES_RELATIVE_DIR_PATH);
      expect(rulesyncRule.getRelativeFilePath()).toBe("convert-test.md");
      expect(rulesyncRule.getFileContent()).toContain("# Convert Test\n\nThis will be converted.");
    });

    it("should preserve metadata in conversion", () => {
      const geminiCliRule = new GeminiCliRule({
        outputRoot: "/test/path",
        relativeDirPath: ".",
        relativeFilePath: "GEMINI.md",
        fileContent: "# Root Gemini Rule\n\nRoot content.",
        root: true,
      });

      const rulesyncRule = geminiCliRule.toRulesyncRule();

      expect(rulesyncRule.getFilePath()).toBe(
        join(testDir, RULESYNC_RULES_RELATIVE_DIR_PATH, RULESYNC_OVERVIEW_FILE_NAME),
      );
      expect(rulesyncRule.getFileContent()).toContain("# Root Gemini Rule\n\nRoot content.");
    });
  });

  describe("validate", () => {
    it("should always return success", () => {
      const geminiCliRule = new GeminiCliRule({
        relativeDirPath: ".gemini/memories",
        relativeFilePath: "validation-test.md",
        fileContent: "# Any content is valid",
      });

      const result = geminiCliRule.validate();

      expect(result.success).toBe(true);
      expect(result.error).toBeNull();
    });

    it("should return success for empty content", () => {
      const geminiCliRule = new GeminiCliRule({
        relativeDirPath: ".gemini/memories",
        relativeFilePath: "empty.md",
        fileContent: "",
      });

      const result = geminiCliRule.validate();

      expect(result.success).toBe(true);
      expect(result.error).toBeNull();
    });

    it("should return success for any content format", () => {
      const contents = [
        "# Markdown content",
        "Plain text content",
        "---\nfrontmatter: true\n---\nContent with frontmatter",
        "/* Code comments */",
        "Invalid markdown ### ###",
        "Unicode characters: 🚀 📝 ✨",
      ];

      for (const content of contents) {
        const geminiCliRule = new GeminiCliRule({
          relativeDirPath: ".gemini/memories",
          relativeFilePath: "test.md",
          fileContent: content,
        });

        const result = geminiCliRule.validate();
        expect(result.success).toBe(true);
        expect(result.error).toBeNull();
      }
    });

    it("should return success for root rule", () => {
      const geminiCliRule = new GeminiCliRule({
        relativeDirPath: ".",
        relativeFilePath: "GEMINI.md",
        fileContent: "# Root rule content",
        root: true,
      });

      const result = geminiCliRule.validate();

      expect(result.success).toBe(true);
      expect(result.error).toBeNull();
    });
  });

  describe("getSettablePaths", () => {
    it("should return correct paths for root and nonRoot", () => {
      const paths = GeminiCliRule.getSettablePaths();

      expect(paths.root).toEqual({
        relativeDirPath: ".",
        relativeFilePath: "GEMINI.md",
      });

      expect(paths.nonRoot).toEqual({
        relativeDirPath: ".gemini/memories",
      });
    });

    it("should have consistent paths structure", () => {
      const paths = GeminiCliRule.getSettablePaths();

      expect(paths).toHaveProperty("root");
      expect(paths).toHaveProperty("nonRoot");
      expect(paths.root).toHaveProperty("relativeDirPath");
      expect(paths.root).toHaveProperty("relativeFilePath");
      expect(paths.nonRoot).toHaveProperty("relativeDirPath");
    });
  });

  describe("isTargetedByRulesyncRule", () => {
    it("should return true for rules targeting geminicli", () => {
      const rulesyncRule = new RulesyncRule({
        outputRoot: testDir,
        relativeDirPath: ".agents/memories",
        relativeFilePath: "test.md",
        frontmatter: {
          targets: ["geminicli"],
        },
        body: "Test content",
      });

      expect(GeminiCliRule.isTargetedByRulesyncRule(rulesyncRule)).toBe(true);
    });

    it("should return true for rules targeting all tools (*)", () => {
      const rulesyncRule = new RulesyncRule({
        outputRoot: testDir,
        relativeDirPath: ".agents/memories",
        relativeFilePath: "test.md",
        frontmatter: {
          targets: ["*"],
        },
        body: "Test content",
      });

      expect(GeminiCliRule.isTargetedByRulesyncRule(rulesyncRule)).toBe(true);
    });

    it("should return false for rules not targeting geminicli", () => {
      const rulesyncRule = new RulesyncRule({
        outputRoot: testDir,
        relativeDirPath: ".agents/memories",
        relativeFilePath: "test.md",
        frontmatter: {
          targets: ["cursor", "copilot"],
        },
        body: "Test content",
      });

      expect(GeminiCliRule.isTargetedByRulesyncRule(rulesyncRule)).toBe(false);
    });

    it("should return false for empty targets", () => {
      const rulesyncRule = new RulesyncRule({
        outputRoot: testDir,
        relativeDirPath: ".agents/memories",
        relativeFilePath: "test.md",
        frontmatter: {
          targets: [],
        },
        body: "Test content",
      });

      expect(GeminiCliRule.isTargetedByRulesyncRule(rulesyncRule)).toBe(false);
    });

    it("should handle mixed targets including geminicli", () => {
      const rulesyncRule = new RulesyncRule({
        outputRoot: testDir,
        relativeDirPath: ".agents/memories",
        relativeFilePath: "test.md",
        frontmatter: {
          targets: ["cursor", "geminicli", "copilot"],
        },
        body: "Test content",
      });

      expect(GeminiCliRule.isTargetedByRulesyncRule(rulesyncRule)).toBe(true);
    });

    it("should handle undefined targets in frontmatter", () => {
      const rulesyncRule = new RulesyncRule({
        outputRoot: testDir,
        relativeDirPath: ".agents/memories",
        relativeFilePath: "test.md",
        frontmatter: {},
        body: "Test content",
      });

      expect(GeminiCliRule.isTargetedByRulesyncRule(rulesyncRule)).toBe(true);
    });
  });

  describe("integration tests", () => {
    it("should handle complete workflow from file to rulesync rule", async () => {
      // Create original file
      const memoriesDir = join(testDir, ".gemini/memories");
      await ensureDir(memoriesDir);
      const originalContent = "# Integration Test\n\nComplete workflow test.";
      await writeFileContent(join(memoriesDir, "integration.md"), originalContent);

      // Load from file
      const geminiCliRule = await GeminiCliRule.fromFile({
        outputRoot: testDir,
        relativeFilePath: "integration.md",
      });

      // Convert to rulesync rule
      const rulesyncRule = geminiCliRule.toRulesyncRule();

      // Verify conversion
      expect(rulesyncRule.getFileContent()).toContain(originalContent);
      expect(rulesyncRule.getRelativeDirPath()).toBe(RULESYNC_RULES_RELATIVE_DIR_PATH);
      expect(rulesyncRule.getRelativeFilePath()).toBe("integration.md");
    });

    it("should handle root file workflow", async () => {
      // Create root file
      const rootContent = "# Root Gemini CLI Configuration\n\nThis is the main configuration.";
      await writeFileContent(join(testDir, "GEMINI.md"), rootContent);

      // Load from file
      const geminiCliRule = await GeminiCliRule.fromFile({
        outputRoot: testDir,
        relativeFilePath: "GEMINI.md",
      });

      // Verify root properties
      expect(geminiCliRule.getRelativeDirPath()).toBe(".");
      expect(geminiCliRule.getRelativeFilePath()).toBe("GEMINI.md");
      expect(geminiCliRule.getFileContent()).toBe(rootContent);

      // Convert to rulesync rule
      const rulesyncRule = geminiCliRule.toRulesyncRule();

      // Verify conversion
      expect(rulesyncRule.getFileContent()).toContain(rootContent);
      expect(rulesyncRule.getRelativeDirPath()).toBe(RULESYNC_RULES_RELATIVE_DIR_PATH);
      expect(rulesyncRule.getRelativeFilePath()).toBe(RULESYNC_OVERVIEW_FILE_NAME);
    });

    it("should handle roundtrip conversion rulesync -> geminicli -> rulesync", () => {
      const originalBody = "# Roundtrip Test\n\nContent should remain the same.";

      // Start with rulesync rule
      const originalRulesync = new RulesyncRule({
        outputRoot: testDir,
        relativeDirPath: RULESYNC_RELATIVE_DIR_PATH,
        relativeFilePath: "roundtrip.md",
        frontmatter: {
          root: false,
          targets: ["*"],
          description: "",
          globs: [],
        },
        body: originalBody,
      });

      // Convert to gemini cli rule
      const geminiCliRule = GeminiCliRule.fromRulesyncRule({
        outputRoot: testDir,
        rulesyncRule: originalRulesync,
      });

      // Convert back to rulesync rule
      const finalRulesync = geminiCliRule.toRulesyncRule();

      // Verify content preservation
      expect(finalRulesync.getFileContent()).toContain(originalBody);
      expect(finalRulesync.getRelativeFilePath()).toBe("roundtrip.md");
    });

    it("should handle roundtrip conversion with root rule", () => {
      const originalBody = "# Root Roundtrip Test\n\nRoot content should remain the same.";

      // Start with root rulesync rule
      const originalRulesync = new RulesyncRule({
        outputRoot: testDir,
        relativeDirPath: RULESYNC_RELATIVE_DIR_PATH,
        relativeFilePath: "root-roundtrip.md",
        frontmatter: {
          root: true,
          targets: ["*"],
          description: "",
          globs: [],
        },
        body: originalBody,
      });

      // Convert to gemini cli rule
      const geminiCliRule = GeminiCliRule.fromRulesyncRule({
        outputRoot: testDir,
        rulesyncRule: originalRulesync,
      });

      // Verify root conversion
      expect(geminiCliRule.getRelativeDirPath()).toBe(".");
      expect(geminiCliRule.getRelativeFilePath()).toBe("GEMINI.md");

      // Convert back to rulesync rule
      const finalRulesync = geminiCliRule.toRulesyncRule();

      // Verify content preservation
      expect(finalRulesync.getFileContent()).toContain(originalBody);
      expect(finalRulesync.getRelativeFilePath()).toBe(RULESYNC_OVERVIEW_FILE_NAME);
    });

    it("should handle mixed file types in directory", async () => {
      // Create multiple files
      const memoriesDir = join(testDir, ".gemini/memories");
      await ensureDir(memoriesDir);

      const files = [
        { name: "general.md", content: "# General Rules\n\nGeneral content." },
        { name: "specific.md", content: "# Specific Rules\n\nSpecific content." },
        { name: "guidelines.md", content: "# Guidelines\n\nGuideline content." },
      ];

      // Write all files
      for (const file of files) {
        await writeFileContent(join(memoriesDir, file.name), file.content);
      }

      // Load each file and verify
      for (const file of files) {
        const geminiCliRule = await GeminiCliRule.fromFile({
          outputRoot: testDir,
          relativeFilePath: file.name,
        });

        expect(geminiCliRule.getFileContent()).toBe(file.content);
        expect(geminiCliRule.getRelativeDirPath()).toBe(".gemini/memories");
        expect(geminiCliRule.getRelativeFilePath()).toBe(file.name);

        // Verify conversion works
        const rulesyncRule = geminiCliRule.toRulesyncRule();
        expect(rulesyncRule.getFileContent()).toContain(file.content);
      }
    });

    it("should handle large content files", () => {
      const largeContent = "# Large Content Test\n\n" + "Lorem ipsum dolor sit amet. ".repeat(1000);

      const geminiCliRule = new GeminiCliRule({
        outputRoot: testDir,
        relativeDirPath: ".gemini/memories",
        relativeFilePath: "large.md",
        fileContent: largeContent,
      });

      // Verify validation still works
      const result = geminiCliRule.validate();
      expect(result.success).toBe(true);

      // Verify conversion works
      const rulesyncRule = geminiCliRule.toRulesyncRule();
      expect(rulesyncRule.getFileContent()).toContain(largeContent);
    });
  });
});
