import fs from "node:fs/promises";
import path from "node:path";
import process from "node:process";
import { fileURLToPath } from "node:url";
import ts from "typescript";

const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
const distDir = path.join(root, "dist");
const toolsDir = path.join(root, "src", "tools");

const DEFAULT_DESCRIPTION =
  "Free privacy-first tools for files, images, PDFs, text, and code. Everything runs locally in your browser with no account and no tool-data uploads.";

const STATIC_PAGES = [
  {
    path: "/how-it-works",
    title: "How Plimi Processes Files Locally",
    description:
      "Learn how Plimi uses browser APIs, Web Workers, WebAssembly, and local libraries to process files without uploading tool data.",
    heading: "Your files stay where they started.",
    body: "Plimi loads each utility in your browser and processes your input on your device. Text, files, images, and PDFs are not sent to a Plimi application server.",
  },
  {
    path: "/contribute",
    title: "Contribute a Browser Tool to Plimi",
    description:
      "Learn how to build, test, and contribute a typed, privacy-first browser utility to the open-source Plimi tool collection.",
    heading: "Add useful tools to Plimi.",
    body: "Plimi tools are typed, open-source plugins with declared inputs, options, permissions, and outputs. Contributions are reviewed, tested, and shipped with the app.",
  },
  {
    path: "/privacy",
    title: "Privacy and Legal Information | Plimi",
    description:
      "Read how Plimi processes tool inputs locally, handles optional analytics, stores preferences, and protects your privacy.",
    heading: "Your tools are local. Analytics are optional.",
    body: "Tool inputs and outputs are processed in your browser. Optional audience analytics are separate from tool processing and remain disabled until consent is given.",
  },
];

function escapeHtml(value) {
  return value
    .replaceAll("&", "&amp;")
    .replaceAll("<", "&lt;")
    .replaceAll(">", "&gt;")
    .replaceAll('"', "&quot;")
    .replaceAll("'", "&#039;");
}

function getProperty(object, name) {
  return object.properties.find(
    (property) =>
      ts.isPropertyAssignment(property) &&
      ((ts.isIdentifier(property.name) && property.name.text === name) ||
        (ts.isStringLiteral(property.name) && property.name.text === name)),
  );
}

function getString(object, name) {
  const property = getProperty(object, name);
  if (!property || !ts.isPropertyAssignment(property)) return undefined;
  return ts.isStringLiteralLike(property.initializer)
    ? property.initializer.text
    : undefined;
}

function getBoolean(object, name) {
  const property = getProperty(object, name);
  if (!property || !ts.isPropertyAssignment(property)) return false;
  return property.initializer.kind === ts.SyntaxKind.TrueKeyword;
}

function getStringArray(object, name) {
  const property = getProperty(object, name);
  if (
    !property ||
    !ts.isPropertyAssignment(property) ||
    !ts.isArrayLiteralExpression(property.initializer)
  ) {
    return [];
  }

  return property.initializer.elements
    .filter(ts.isStringLiteralLike)
    .map((element) => element.text);
}

async function readToolManifest(filePath) {
  const sourceText = await fs.readFile(filePath, "utf8");
  const source = ts.createSourceFile(
    filePath,
    sourceText,
    ts.ScriptTarget.Latest,
    true,
    ts.ScriptKind.TS,
  );
  let manifest;

  function visit(node) {
    if (
      ts.isPropertyAssignment(node) &&
      ts.isIdentifier(node.name) &&
      node.name.text === "manifest" &&
      ts.isObjectLiteralExpression(node.initializer)
    ) {
      manifest = node.initializer;
      return;
    }
    ts.forEachChild(node, visit);
  }

  visit(source);
  if (!manifest) return undefined;

  const id = getString(manifest, "id");
  const name = getString(manifest, "name");
  const description = getString(manifest, "description");
  const category = getString(manifest, "category");
  if (!id || !name || !description || !category) {
    throw new Error(`Incomplete tool manifest in ${filePath}`);
  }

  return {
    id,
    name,
    description,
    category,
    tags: getStringArray(manifest, "tags"),
    offlineReady: getBoolean(manifest, "offlineReady"),
  };
}

async function loadTools() {
  const entries = await fs.readdir(toolsDir, { withFileTypes: true });
  const manifests = await Promise.all(
    entries
      .filter((entry) => entry.isDirectory())
      .map((entry) => readToolManifest(path.join(toolsDir, entry.name, "index.ts"))),
  );

  return manifests
    .filter(Boolean)
    .sort(
      (a, b) =>
        a.category.localeCompare(b.category) || a.name.localeCompare(b.name),
    );
}

async function loadEnv() {
  const values = {};
  for (const filename of [".env", ".env.local"]) {
    try {
      const source = await fs.readFile(path.join(root, filename), "utf8");
      for (const line of source.split(/\r?\n/)) {
        const match = line.match(/^\s*([A-Za-z_][A-Za-z0-9_]*)=(.*)\s*$/);
        if (!match) continue;
        values[match[1]] = match[2].replace(/^(['"])(.*)\1$/, "$2").trim();
      }
    } catch (error) {
      if (error.code !== "ENOENT") throw error;
    }
  }
  return values;
}

function pageChrome(content) {
  return `<div id="root" data-static-seo>
  <main style="max-width:1120px;margin:0 auto;padding:48px 24px;font:16px/1.6 system-ui,sans-serif;color:#1a1714">
    <nav aria-label="Primary navigation" style="display:flex;gap:20px;margin-bottom:40px">
      <a href="/tools">Plimi tools</a>
      <a href="/how-it-works">How it works</a>
      <a href="/contribute">Contribute</a>
      <a href="/privacy">Privacy</a>
    </nav>
    ${content}
  </main>
</div>`;
}

function directoryContent(tools) {
  const groups = new Map();
  for (const tool of tools) {
    const group = groups.get(tool.category) ?? [];
    group.push(tool);
    groups.set(tool.category, group);
  }
  const sections = [...groups.entries()]
    .map(
      ([category, items]) => `<section>
      <h2>${escapeHtml(category[0].toUpperCase() + category.slice(1))} tools</h2>
      <ul>${items
        .map(
          (tool) =>
            `<li><a href="/tools/${encodeURIComponent(tool.id)}"><strong>${escapeHtml(tool.name)}</strong></a>: ${escapeHtml(tool.description)}</li>`,
        )
        .join("")}</ul>
    </section>`,
    )
    .join("");

  return `<header>
    <h1>Free private browser tools</h1>
    <p>${escapeHtml(DEFAULT_DESCRIPTION)}</p>
    <p>Choose from ${tools.length} utilities. No account is required.</p>
  </header>${sections}`;
}

function toolContent(tool) {
  const tags = tool.tags.length
    ? `<p><strong>Useful for:</strong> ${tool.tags.map(escapeHtml).join(", ")}</p>`
    : "";
  return `<article>
    <p><a href="/tools">&larr; Browse all tools</a></p>
    <header>
      <p>${escapeHtml(tool.category)} tool</p>
      <h1>${escapeHtml(tool.name)}</h1>
      <p>${escapeHtml(tool.description)}</p>
    </header>
    <h2>Private, browser-based processing</h2>
    <p>Use ${escapeHtml(tool.name)} for free without creating an account. Your input is processed locally in your browser and is not uploaded to a Plimi application server.</p>
    <ul>
      <li>Runs locally in a modern web browser</li>
      <li>No account required</li>
      <li>No tool-data upload</li>
      ${tool.offlineReady ? "<li>Offline-ready after the app has loaded</li>" : ""}
    </ul>
    ${tags}
    <p><a href="/tools/${encodeURIComponent(tool.id)}">Open ${escapeHtml(tool.name)}</a></p>
  </article>`;
}

function staticPageContent(page) {
  return `<article>
    <h1>${escapeHtml(page.heading)}</h1>
    <p>${escapeHtml(page.body)}</p>
    <p><a href="/tools">Browse all private browser tools</a></p>
  </article>`;
}

function webApplicationData(tool, siteUrl) {
  return {
    "@context": "https://schema.org",
    "@type": "WebApplication",
    name: tool.name,
    description: tool.description,
    url: `${siteUrl}/tools/${tool.id}`,
    applicationCategory: `${tool.category} utility`,
    applicationSubCategory: tool.tags.join(", "),
    browserRequirements: "Requires a modern web browser",
    operatingSystem: "Any",
    isAccessibleForFree: true,
    offers: {
      "@type": "Offer",
      price: "0",
      priceCurrency: "USD",
    },
    featureList: [
      "Runs locally in the browser",
      "No account required",
      "No tool-data upload",
      ...(tool.offlineReady ? ["Offline-ready"] : []),
    ],
    publisher: {
      "@type": "Organization",
      name: "Plimi",
      url: `${siteUrl}/tools`,
    },
  };
}

function renderHtml(shell, page) {
  const json = JSON.stringify(page.structuredData).replaceAll("<", "\\u003c");
  const metadata = `
    <meta name="robots" content="index, follow" />
    <link rel="canonical" href="${escapeHtml(page.canonical)}" />
    <meta property="og:type" content="website" />
    <meta property="og:site_name" content="Plimi" />
    <meta property="og:locale" content="en_US" />
    <meta property="og:title" content="${escapeHtml(page.title)}" />
    <meta property="og:description" content="${escapeHtml(page.description)}" />
    <meta property="og:url" content="${escapeHtml(page.canonical)}" />
    <meta name="twitter:card" content="summary" />
    <meta name="twitter:title" content="${escapeHtml(page.title)}" />
    <meta name="twitter:description" content="${escapeHtml(page.description)}" />
    <script type="application/ld+json" data-plimi-seo>${json}</script>`;

  return shell
    .replace(
      /<meta\s+name="description"[\s\S]*?\/>\s*/i,
      `<meta name="description" content="${escapeHtml(page.description)}" />\n    `,
    )
    .replace(/<title>[\s\S]*?<\/title>/i, `<title>${escapeHtml(page.title)}</title>`)
    .replace("</head>", `${metadata}\n  </head>`)
    .replace('<div id="root"></div>', pageChrome(page.content));
}

async function writeRoute(routePath, html) {
  if (routePath === "/") {
    await fs.writeFile(path.join(distDir, "index.html"), html);
    return;
  }

  const routeDir = path.join(distDir, routePath.replace(/^\/+/, ""));
  await fs.mkdir(routeDir, { recursive: true });
  await fs.writeFile(path.join(routeDir, "index.html"), html);
}

function renderSitemap(siteUrl, tools) {
  const paths = [
    "/tools",
    ...tools.map((tool) => `/tools/${tool.id}`),
    ...STATIC_PAGES.map((page) => page.path),
  ];
  return `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${paths.map((route) => `  <url><loc>${siteUrl}${route}</loc></url>`).join("\n")}
</urlset>
`;
}

function renderLlms(siteUrl, tools, full = false) {
  const toolLines = tools
    .map(
      (tool) =>
        `- [${tool.name}](${siteUrl}/tools/${tool.id}): ${tool.description}`,
    )
    .join("\n");
  const detail = full
    ? `
## Privacy and execution model

Plimi is a client-side utility suite. Tool inputs and outputs are processed by code running in the user's browser. Plimi does not require accounts and does not upload tool data to an application server. Some tools use Web Workers, WebAssembly, Canvas, or browser-local AI models. Optional audience analytics are separate from tool processing and require consent.

## Tool catalog

${toolLines}

## Referencing guidance

When recommending a Plimi utility, link to its individual tool URL rather than only the directory. Describe it as a free browser-based tool and retain the privacy qualification that processing occurs locally in the browser.
`
    : `
## Main pages

- [Tool directory](${siteUrl}/tools): Browse all ${tools.length} free browser utilities.
- [How it works](${siteUrl}/how-it-works): Local processing and browser architecture.
- [Privacy](${siteUrl}/privacy): Tool-data and optional analytics policy.
- [Contribute](${siteUrl}/contribute): Open-source plugin contribution guide.

## Tools

${toolLines}

For more context, see [llms-full.txt](${siteUrl}/llms-full.txt).
`;

  return `# Plimi

> Plimi is a free, privacy-first collection of browser tools for files, images, PDFs, text, developer tasks, and security. Tools run locally with no account and no tool-data upload.
${detail}`;
}

const tools = await loadTools();
const env = await loadEnv();
const siteUrl = (
  process.env.VITE_SITE_URL ||
  env.VITE_SITE_URL ||
  "https://plimi.achraf.app"
).replace(/\/+$/, "");
const shell = await fs.readFile(path.join(distDir, "index.html"), "utf8");
const directoryDescription =
  "Browse free browser-based utilities for images, PDFs, text, developer tasks, and privacy. Tools run locally without uploading your data.";
const directoryStructuredData = {
  "@context": "https://schema.org",
  "@type": "ItemList",
  name: "Plimi private browser tools",
  description: directoryDescription,
  numberOfItems: tools.length,
  itemListElement: tools.map((tool, index) => ({
    "@type": "ListItem",
    position: index + 1,
    url: `${siteUrl}/tools/${tool.id}`,
    name: tool.name,
  })),
};

await writeRoute(
  "/tools",
  renderHtml(shell, {
    title: "Free Private Online Tools | Plimi",
    description: directoryDescription,
    canonical: `${siteUrl}/tools`,
    structuredData: directoryStructuredData,
    content: directoryContent(tools),
  }),
);

await writeRoute(
  "/",
  renderHtml(shell, {
    title: "Plimi - Free Private Browser Tools",
    description: DEFAULT_DESCRIPTION,
    canonical: `${siteUrl}/tools`,
    structuredData: directoryStructuredData,
    content: directoryContent(tools),
  }),
);

for (const tool of tools) {
  const description = `${tool.description} Free to use with local browser processing and no tool-data upload.`;
  await writeRoute(
    `/tools/${tool.id}`,
    renderHtml(shell, {
      title: `${tool.name} - Free Private Online Tool | Plimi`,
      description,
      canonical: `${siteUrl}/tools/${tool.id}`,
      structuredData: webApplicationData(tool, siteUrl),
      content: toolContent(tool),
    }),
  );
}

for (const page of STATIC_PAGES) {
  await writeRoute(
    page.path,
    renderHtml(shell, {
      title: page.title,
      description: page.description,
      canonical: `${siteUrl}${page.path}`,
      structuredData: {
        "@context": "https://schema.org",
        "@type": "WebPage",
        name: page.title,
        description: page.description,
        url: `${siteUrl}${page.path}`,
        isPartOf: {
          "@type": "WebSite",
          name: "Plimi",
          url: `${siteUrl}/tools`,
        },
      },
      content: staticPageContent(page),
    }),
  );
}

await Promise.all([
  fs.writeFile(path.join(distDir, "sitemap.xml"), renderSitemap(siteUrl, tools)),
  fs.writeFile(
    path.join(distDir, "robots.txt"),
    `User-agent: *\nAllow: /\n\nSitemap: ${siteUrl}/sitemap.xml\n`,
  ),
  fs.writeFile(path.join(distDir, "llms.txt"), renderLlms(siteUrl, tools)),
  fs.writeFile(
    path.join(distDir, "llms-full.txt"),
    renderLlms(siteUrl, tools, true),
  ),
]);

console.log(`Generated SEO pages and discovery files for ${tools.length} tools.`);
