import fs from "fs";
import path from "path";
import matter from "gray-matter";
import readingTime from "reading-time";

const BLOG_DIR = path.join(process.cwd(), "src/content/blog");

export interface BlogPost {
  slug: string;
  title: string;
  excerpt: string;
  date: string;
  author: string;
  category: string;
  tags: string[];
  coverImage?: string;
  readingTime: string;
  content: string;
}

export function getAllPosts(locale: "fa" | "en" = "fa"): BlogPost[] {
  if (!fs.existsSync(BLOG_DIR)) return [];
  const files = fs.readdirSync(BLOG_DIR).filter((f) => f.endsWith(`.${locale}.mdx`));
  return files
    .map((file) => {
      const slug = file.replace(`.${locale}.mdx`, "");
      return getPostBySlug(slug, locale);
    })
    .filter(Boolean)
    .sort((a, b) => new Date(b!.date).getTime() - new Date(a!.date).getTime()) as BlogPost[];
}

export function getPostBySlug(slug: string, locale: "fa" | "en" = "fa"): BlogPost | null {
  const filePath = path.join(BLOG_DIR, `${slug}.${locale}.mdx`);
  if (!fs.existsSync(filePath)) return null;
  const raw = fs.readFileSync(filePath, "utf8");
  const { data, content } = matter(raw);
  const rt = readingTime(content);
  return {
    slug,
    title: data.title ?? "",
    excerpt: data.excerpt ?? "",
    date: data.date ?? "",
    author: data.author ?? "",
    category: data.category ?? "",
    tags: data.tags ?? [],
    coverImage: data.coverImage,
    readingTime: locale === "fa" ? `${Math.ceil(rt.minutes)} دقیقه مطالعه` : rt.text,
    content,
  };
}
