import {MetadataRoute} from "next";
import fs from "fs";
import path from "path";

export default function sitemap(): MetadataRoute.Sitemap {
    const baseUrl = "https://dki.one";

    // Static pages
    const staticRoutes = [
        {url: `${baseUrl}/`, lastModified: new Date().toISOString()},
        {url: `${baseUrl}/privacy-policy`, lastModified: new Date().toISOString()},
    ];

    // Read project markdown files from the public folder
    const projectsDirectory = path.join(process.cwd(), "public/projects");
    let projectRoutes: { url: string; lastModified: string; }[] = [];

    if (fs.existsSync(projectsDirectory)) {
        const projectFiles = fs.readdirSync(projectsDirectory).filter(file => file.endsWith(".md"));

        projectRoutes = projectFiles.map((file) => {
            const slug = file.replace(".md", "");
            return {
                url: `${baseUrl}/projects/${slug}`,
                lastModified: new Date().toISOString(),
            };
        });
    }

    return [...staticRoutes, ...projectRoutes];
}
