import fs from "fs";
import path from "path";
import matter from "gray-matter";
import { remark } from "remark";
import remarkGfm from "remark-gfm";
import html from "remark-html";
import readingTime from "reading-time";

export interface GuideMetadata {
  title: string;
  slug: string;
  excerpt: string;
  category: string;
  difficulty: "einsteiger" | "fortgeschritten" | "experte";
  readingTime: string;
}

export interface Guide extends GuideMetadata {
  contentHtml: string;
}

const GUIDES_DIR = path.join(process.cwd(), "src/content/guides");
const PAGES_DIR = path.join(process.cwd(), "src/content/pages");

const ensureDir = (dir: string) => {
  if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
};

export const getAllGuides = (): GuideMetadata[] => {
  ensureDir(GUIDES_DIR);
  const files = fs.readdirSync(GUIDES_DIR).filter((f) => f.endsWith(".md"));

  return files
    .map((filename) => {
      const filePath = path.join(GUIDES_DIR, filename);
      const fileContent = fs.readFileSync(filePath, "utf-8");
      const { data, content } = matter(fileContent);
      const stats = readingTime(content);

      return {
        title: data.title ?? filename.replace(".md", ""),
        slug: data.slug ?? filename.replace(".md", ""),
        excerpt: data.excerpt ?? "",
        category: data.category ?? "Allgemein",
        difficulty: data.difficulty ?? "fortgeschritten",
        readingTime: stats.text.replace("read", "Lesezeit"),
      };
    })
    .sort((a, b) => a.title.localeCompare(b.title, "de"));
};

export const getGuideBySlug = async (slug: string): Promise<Guide | null> => {
  ensureDir(GUIDES_DIR);
  const filePath = path.join(GUIDES_DIR, `${slug}.md`);

  if (!fs.existsSync(filePath)) return null;

  const fileContent = fs.readFileSync(filePath, "utf-8");
  const { data, content } = matter(fileContent);
  const stats = readingTime(content);
  const processed = await remark().use(remarkGfm).use(html, { sanitize: false }).process(content);

  return {
    title: data.title ?? slug,
    slug: data.slug ?? slug,
    excerpt: data.excerpt ?? "",
    category: data.category ?? "Allgemein",
    difficulty: data.difficulty ?? "fortgeschritten",
    readingTime: stats.text.replace("read", "Lesezeit"),
    contentHtml: processed.toString(),
  };
};

export const getGuideCategories = (): string[] => {
  const guides = getAllGuides();
  const categories = [...new Set(guides.map((g) => g.category))];
  return categories.sort((a, b) => a.localeCompare(b, "de"));
};

export const getPageContent = async (
  slug: string
): Promise<{ title: string; contentHtml: string } | null> => {
  ensureDir(PAGES_DIR);
  const filePath = path.join(PAGES_DIR, `${slug}.md`);

  if (!fs.existsSync(filePath)) return null;

  const fileContent = fs.readFileSync(filePath, "utf-8");
  const { data, content } = matter(fileContent);
  const processed = await remark().use(remarkGfm).use(html, { sanitize: false }).process(content);

  return {
    title: data.title ?? slug,
    contentHtml: processed.toString(),
  };
};
