import fs from 'fs';
import path from 'path';

// Fungsi untuk membaca config secara live tanpa build ulang (Runtime)
export function getSiteConfig() {
  // Gunakan path absolute agar tidak bingung dengan root workspace di Windows
  const configPath = path.join(process.cwd(), 'site_config.json');
  
  try {
    if (fs.existsSync(configPath)) {
      const fileContents = fs.readFileSync(configPath, 'utf8');
      return JSON.parse(fileContents);
    } else {
      // Coba fallback ke folder saat ini jika process.cwd() meleset
      const fallbackPath = path.join(__dirname, '..', '..', 'site_config.json');
      if (fs.existsSync(fallbackPath)) {
        return JSON.parse(fs.readFileSync(fallbackPath, 'utf8'));
      }
    }
  } catch (err) {
    console.error("Gagal membaca site_config.json:", err);
  }
  
  // Default jika file tidak ada atau error
  return {
    histatsId: '5016007',
    gaId: 'G-Q7YYG6BQ16',
    sitemapToken: 'rahasia123',
    googleVerification: '',
    adKeyBelowTitle: '72aae8a75da17a34e48ed84feaa311bf',
    adKeyMiddle: '4b03159602cba0243869c415124b923e',
    adKeyBelowArticle: '5c2ef2cb97dff829617ea4c4e1d5ca7b',
    redirectUrl: 'https://biggerbreakerfind.com/m5c9h7sz5?key=b5c00cea2c5f93da4bf36a756da22d91',
    popUnderScript: 'https://biggerbreakerfind.com/48/5f/74/485f7426bb614d7e620b0e088268e010.js',
    rssTitle: 'Official Feed',
    rssDescription: 'Latest content and updates',
  };
}

// Untuk kompatibilitas kode lama (tetap ekspor objek tapi panggil fungsi)
export const SITE_CONFIG = getSiteConfig();
