import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import logger from "../utils/logger.js";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

// Map blog categories to their data file paths
const STATIC_DATA_MAP = {
  "Blogs-AI-Automation": {
    file: "blogs.aiAutomation.ts",
    exportName: "aiAutomationBlogData",
  },
  "Blogs-Web-Design": {
    file: "blogs.webDesign.ts",
    exportName: "webDesignBlogData",
  },
  "Blogs-Business-Growth": {
    file: "blogs.businessGrowth.ts",
    exportName: "businessGrowthBlogData",
  },
  "Blogs-IT-Staff": {
    file: "blogs.itstaff.ts",
    exportName: "ItStaffBlogData",
  },
  "Blogs-Mobile-App": {
    file: "blogs.mobdesign.ts",
    exportName: "mobileAppDesignBlogData",
  },
  "Blogs-Social-Marketing": {
    file: "blogs.Social.ts",
    exportName: "SEOBlogData",
  },
  "Blogs-Custom-Software": {
    file: null, // May not exist yet
    exportName: null,
  },
  "Blogs-Custom-Marketing": {
    file: null, // May not exist yet
    exportName: null,
  },
};

// Path to the frontend data directory
// Adjust this path based on your project structure
const DATA_DIR = path.join(__dirname, "../../../aibit_soft/src/data");

export async function getStaticDataForCategory(category) {
  try {
    const config = STATIC_DATA_MAP[category];
    if (!config || !config.file) {
      logger.warn(`No static data file configured for category: ${category}`);
      return null;
    }

    const filePath = path.join(DATA_DIR, config.file);
    
    // Check if file exists
    if (!fs.existsSync(filePath)) {
      logger.warn(`Static data file not found: ${filePath}`);
      return null;
    }

    // Read and parse the TypeScript file
    // Note: This is a simplified approach. In production, you might want to:
    // 1. Use a TypeScript compiler to properly parse the file
    // 2. Or convert the data files to JSON
    // 3. Or use a build step to generate JSON from TS files
    
    const fileContent = fs.readFileSync(filePath, "utf-8");
    
    // Extract the exported object using regex (simplified approach)
    // This works for simple object exports but may need refinement
    const exportMatch = fileContent.match(
      new RegExp(`export const ${config.exportName}\\s*=\\s*({[\\s\\S]*});`, "m")
    );
    
    if (!exportMatch) {
      logger.warn(`Could not parse export from file: ${filePath}`);
      return null;
    }

    // For now, return a message indicating the file was found
    // In a real implementation, you'd want to properly parse the TypeScript
    // or convert these files to JSON format
    
    return {
      category,
      filePath,
      exportName: config.exportName,
      message: "Static data file found. Consider converting to JSON for easier parsing.",
      // In production, you would parse and return the actual data here
    };
  } catch (error) {
    logger.error(`Error reading static data for ${category}:`, error);
    return null;
  }
}

