module.exports = function(eleventyConfig) {

const fs = require('fs');
const path = require('path');

// add my css file from the root folder 
eleventyConfig.addPassthroughCopy("bundle.css");
eleventyConfig.addPassthroughCopy("img");
eleventyConfig.addPassthroughCopy({ "_data/images": "photos" });

// This line was to make sure my post.md weren't processed as Liquid,
// permitting this post here
eleventyConfig.setTemplateFormats(["md", "njk", "html"]);
eleventyConfig.addGlobalData("site.author", "unakt");
// Double with the .eleventyignore file, but just in case
eleventyConfig.ignores.add("/_drafts/**");
eleventyConfig.ignores.add("/README.md");

eleventyConfig.addLayoutAlias("post", "post.njk");

eleventyConfig.addCollection("postlist", function(collectionApi) {
    return collectionApi.getFilteredByGlob("./posts/*.md");
  });


// Custom collection for mini images
// It scans the ./src/_data/images/ folder for jpg files
// and groups them by their parent folder name
// Each image object contains its path, album name, and last modified date
// ========================================================
eleventyConfig.addCollection("miniImages", function(collectionApi) {
    const files = [];
    const baseDir = "./_data/images/";

    // Recursive function to find all jpgs
    const getFiles = (dir) => {
      fs.readdirSync(dir).forEach(file => {
        const fullPath = path.join(dir, file);
        if (fs.statSync(fullPath).isDirectory()) {
          getFiles(fullPath);
        } else if (file.endsWith(".jpg") || file.endsWith(".jpeg")) {
          const relativePath = path.relative("./_data/images", fullPath).replace(/\\/g, "/");
          files.push({
            path: "/photos/" + relativePath,
            album: dir.split(path.sep).pop(), // Gets the folder name
            date: fs.statSync(fullPath).mtime
          });
        }
      });
    };

    getFiles(baseDir);
    
    // Group them by album name
    const grouped = {};
    files.forEach(f => {
      if (!grouped[f.album]) grouped[f.album] = [];
      grouped[f.album].push(f);
    });

    return grouped;
  });
// ========================================================

// Date filter to format dates as YYYY-MM-DD
eleventyConfig.addFilter("date", function(dateObj, format = "YYYY-MM-DD") {
    const d = new Date(dateObj);
    return d.toISOString().split('T')[0]; // Returns YYYY-MM-DD
  });

return {
	// Ok idk this prevent markdown from being processed?
	markdownTemplateEngine: false,
	dir: {
		input: "./",
		output: "_site",
		layouts: "layouts",
		includes: "includes"
		}
	};
};