import { readFile } from "node:fs/promises";
import { join } from "node:path";
import { existsSync } from "node:fs";
class DjangoDetector {
  name = "django";
  language = "python";
  filePattern = /urls\.py$/;
  projectFiles = [];
  async detect(projectRoot, files) {
    this.projectFiles = files;
    const hasManagePy = files.some((f) => f.endsWith("manage.py"));
    if (hasManagePy) return true;
    for (const file of ["requirements.txt", "Pipfile", "pyproject.toml"]) {
      const filePath = join(projectRoot, file);
      if (existsSync(filePath)) {
        try {
          const content = await readFile(filePath, "utf-8");
          if (content.toLowerCase().includes("django")) {
            return true;
          }
        } catch {
        }
      }
    }
    return false;
  }
  async extractRoutes(filePath, content, ctx) {
    const routes = [];
    const visitedFiles = /* @__PURE__ */ new Set();
    await this.parseUrlsFile(filePath, content, "", ctx, routes, visitedFiles);
    return routes;
  }
  async parseUrlsFile(filePath, content, prefix, ctx, routes, visitedFiles) {
    if (visitedFiles.has(filePath)) return;
    visitedFiles.add(filePath);
    const pathRegex = /\b(path|re_path|url)\s*\(\s*['"]([^'"]*)['"]\s*,\s*([^)]+)\)/g;
    let match;
    while ((match = pathRegex.exec(content)) !== null) {
      const routePath = match[2];
      const handlerStr = match[3].trim();
      const cleanPrefix = prefix.replace(/^\/|\/$/g, "");
      const cleanRoute = routePath.replace(/^\/|\/$/g, "");
      const fullPath = "/" + [cleanPrefix, cleanRoute].filter(Boolean).join("/");
      const includeMatch = handlerStr.match(/include\s*\(\s*['"]([^'"]+)['"]/);
      if (includeMatch) {
        const includedModule = includeMatch[1];
        const resolvedPath = this.resolveModulePath(includedModule);
        if (resolvedPath) {
          const absResolvedPath = join(ctx.workspaceRoot, resolvedPath);
          if (existsSync(absResolvedPath)) {
            try {
              const subContent = await readFile(absResolvedPath, "utf-8");
              await this.parseUrlsFile(resolvedPath, subContent, fullPath, ctx, routes, visitedFiles);
            } catch {
            }
          }
        }
      } else {
        let handlerSymbol = handlerStr.split(",")[0].trim();
        handlerSymbol = handlerSymbol.replace(/\.as_view\s*\([^)]*\)/, "");
        let resolvedFile = filePath;
        const resolvedPath = ctx.resolveSymbolToFile(handlerSymbol);
        if (resolvedPath) {
          resolvedFile = resolvedPath;
        }
        routes.push({
          framework: this.name,
          method: "GET",
          // Django defaults to GET/any unless method-check decorators are used
          path: fullPath,
          handlerFile: resolvedFile,
          handlerSymbol,
          metadata: {
            confidence: "inferred"
          }
        });
      }
    }
    const routerRegex = /router\.register\s*\(\s*r?['"]([^'"]+)['"]\s*,\s*([a-zA-Z0-9_]+)/g;
    while ((match = routerRegex.exec(content)) !== null) {
      const routerPath = match[1];
      const viewSetSymbol = match[2];
      const cleanPrefix = prefix.replace(/^\/|\/$/g, "");
      const cleanRoute = routerPath.replace(/^\/|\/$/g, "");
      const fullPath = "/" + [cleanPrefix, cleanRoute].filter(Boolean).join("/");
      let resolvedFile = filePath;
      const resolvedPath = ctx.resolveSymbolToFile(viewSetSymbol);
      if (resolvedPath) {
        resolvedFile = resolvedPath;
      }
      const verbs = [
        { method: "GET", suffix: "/", symbolSuffix: ".list" },
        { method: "POST", suffix: "/", symbolSuffix: ".create" },
        { method: "GET", suffix: "/{id}/", symbolSuffix: ".retrieve" },
        { method: "PUT", suffix: "/{id}/", symbolSuffix: ".update" },
        { method: "PATCH", suffix: "/{id}/", symbolSuffix: ".partial_update" },
        { method: "DELETE", suffix: "/{id}/", symbolSuffix: ".destroy" }
      ];
      for (const v of verbs) {
        const cleanSuffix = v.suffix.replace(/^\/|\/$/g, "");
        const finalPath = "/" + [fullPath.replace(/^\/|\/$/g, ""), cleanSuffix].filter(Boolean).join("/");
        routes.push({
          framework: this.name,
          method: v.method,
          path: finalPath,
          handlerFile: resolvedFile,
          handlerSymbol: `${viewSetSymbol}${v.symbolSuffix}`,
          metadata: {
            confidence: "inferred",
            resourceType: "drf_viewset"
          }
        });
      }
    }
  }
  resolveModulePath(moduleStr) {
    const targetRelPath = moduleStr.replace(/\./g, "/") + ".py";
    for (const f of this.projectFiles) {
      if (f.endsWith(targetRelPath)) {
        return f;
      }
    }
    const parts = moduleStr.split(".");
    const lastPart = parts[parts.length - 1] + ".py";
    for (const f of this.projectFiles) {
      if (f.endsWith(lastPart)) {
        return f;
      }
    }
    return null;
  }
}
export {
  DjangoDetector
};
