import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

// Get the directory of the current module
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// Path to the config directory (two levels up from lib directory)
const configDir = path.resolve(__dirname, '../../config');

/**
 * Cache to avoid loading config files multiple times
 */
const configCache = new Map<string, any>();

/**
 * Load and parse a JSON configuration file
 * @param name The name of the config file without extension
 * @returns The parsed configuration object
 */
export function loadConfig<T>(name: string): T {
  // If the config is already in cache, return it
  if (configCache.has(name)) {
    return configCache.get(name) as T;
  }

  try {
    // Load the config file
    const configPath = path.join(configDir, `${name}.json`);
    const configData = fs.readFileSync(configPath, 'utf-8');
    const config = JSON.parse(configData);

    // Cache the result
    configCache.set(name, config);
    
    return config as T;
  } catch (error) {
    console.error(`Error loading config '${name}':`, error);
    throw new Error(`Failed to load config: ${name}`);
  }
}

/**
 * Site configuration type
 */
export interface SiteConfig {
  username: string;
  rootUrl: string;
  shortDescription: string;
  longDescription: string;
  articlesName: string;
  projectsName: string;
  viewAll: string;
  noArticles: string;
  noProjects: string;
  blogTitle: string;
  blogShortDescription: string;
  blogLongDescription: string;
  projectTitle: string;
  projectShortDescription: string;
  projectLongDescription: string;
  profileImage: string;
  menu: Record<string, string>;
}

/**
 * Service category type (from types.ts)
 */
export type ServiceCategory = Record<string, Service[]>;

/**
 * Service type (based on types.ts)
 */
export interface Service {
  name: string;
  link: string;
  icon: string;
  iconDark?: string;
  alt: string;
  tags?: string[];
}

/**
 * Social media type (based on types.ts)
 */
export interface SocialMedia {
  name: string;
  url: string;
  iconDark: string;
  iconLight: string;
  alt: string;
  showInFooter: boolean;
  showInAbout: boolean;
}

// Convenience exports for commonly used configs
export const site = loadConfig<SiteConfig>('site');
export const services = loadConfig<ServiceCategory>('services');
export const socials = loadConfig<SocialMedia[]>('socials');
