import { tool } from "@opencode-ai/plugin";
import fs from "fs/promises";
import path from "path";

export default tool({
  description:
    "Read memory files for persistent cross-session context. Returns current project state, learnings, and active tasks. Supports subdirectories (e.g., 'research/opencode-sessions').",
  args: {
    file: tool.schema
      .string()
      .optional()
      .describe(
        "Specific memory file to read (with optional subdirectory): memory, todo, development-guidelines, architecture-decisions, blockers, research/topic-name. Defaults to 'memory'.",
      ),
  },
  execute: async (args: { file?: string }) => {
    const fileName = args.file || "memory";

    // Normalize: strip .md extension if present
    const normalizedFile = fileName.replace(/\.md$/i, "");

    // Location priority: project > global > legacy
    const locations = [
      path.join(process.cwd(), ".opencode/memory", `${normalizedFile}.md`),
      path.join(
        process.env.HOME || "",
        ".config/opencode/memory",
        `${normalizedFile}.md`,
      ),
      path.join(
        process.cwd(),
        ".config/opencode/memory",
        `${normalizedFile}.md`,
      ),
    ];

    // Try each location in order
    for (const filePath of locations) {
      try {
        const content = await fs.readFile(filePath, "utf-8");
        const locationLabel = filePath.includes(".opencode/memory")
          ? "project"
          : filePath.includes(process.env.HOME || "")
            ? "global"
            : "legacy";
        return `[Read from ${locationLabel}: ${filePath}]\n\n${content}`;
      } catch (error) {
        // Continue to next location if file not found
        if (
          error instanceof Error &&
          "code" in error &&
          error.code === "ENOENT"
        ) {
          continue;
        }
        // Other errors should be reported
        if (error instanceof Error) {
          return `Error reading memory from ${filePath}: ${error.message}`;
        }
      }
    }

    // No file found in any location
    return `Memory file '${normalizedFile}.md' not found in any location.\nSearched:\n- ${locations.join("\n- ")}\n\nCommon files: memory, todo, development-guidelines, architecture-decisions, blockers\nOr use subdirectories: research/topic-name, decisions/choice-name, plans/plan-name`;
  },
});
