<?php
  
// Default configuration
$defaultConfig = [
    'site_title' => 'CodePress',
    'content_dir' => __DIR__ . '/../../content',
    'templates_dir' => __DIR__ . '/../templates',
    'default_page' => 'auto',
    'homepage' => 'auto'
];

// Check for config.json in project root
$projectRoot = __DIR__ . '/../../';
$configJsonPath = $projectRoot . 'config.json';

if (file_exists($configJsonPath)) {
    $jsonContent = file_get_contents($configJsonPath);
    $jsonConfig = json_decode($jsonContent, true);
    
    if (json_last_error() === JSON_ERROR_NONE && is_array($jsonConfig)) {
        // Merge JSON config with defaults, converting relative paths to absolute
        $mergedConfig = array_merge($defaultConfig, $jsonConfig);
        
        // Convert relative paths to absolute paths (inline function to avoid redeclaration)
        $isAbsolutePath = function($path) {
            return (strpos($path, '/') === 0) || (preg_match('/^[A-Za-z]:/', $path));
        };
        
        if (isset($mergedConfig['content_dir']) && !$isAbsolutePath($mergedConfig['content_dir'])) {
            $mergedConfig['content_dir'] = $projectRoot . $mergedConfig['content_dir'];
        }
        if (isset($mergedConfig['templates_dir']) && !$isAbsolutePath($mergedConfig['templates_dir'])) {
            $mergedConfig['templates_dir'] = $projectRoot . $mergedConfig['templates_dir'];
        }
        
        return $mergedConfig;
    }
}

// Fallback to default config
return $defaultConfig;