<?php
/**
 * Fix All Broken Namespaces - Final Fix
 * Fixes all files with incorrect namespace declarations containing file paths
 */

if (php_sapi_name() !== 'cli') {
    die('This script must be run from command line');
}

$vendorPath = __DIR__ . '/vendor';

echo "Fixing all broken namespaces...\n";

// Fix FacebookAds files
$facebookPath = $vendorPath . '/facebook/php-business-sdk/src/FacebookAds';
if (is_dir($facebookPath)) {
    $iterator = new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator($facebookPath, RecursiveDirectoryIterator::SKIP_DOTS),
        RecursiveIteratorIterator::SELF_FIRST
    );
    
    $count = 0;
    foreach ($iterator as $file) {
        if ($file->isFile() && $file->getExtension() === 'php') {
            $filePath = $file->getRealPath();
            $content = file_get_contents($filePath);
            
            // Check if namespace has file path in it
            if (preg_match('/^namespace\s+PYS_PRO_GLOBAL\\\\FacebookAds\\\\[A-Z]:/m', $content)) {
                // Get relative path from FacebookAds directory
                $relativePath = str_replace($facebookPath . DIRECTORY_SEPARATOR, '', $filePath);
                $relativePath = str_replace(DIRECTORY_SEPARATOR, '\\', $relativePath);
                $relativePath = str_replace('.php', '', $relativePath);
                
                // Get directory path (without filename)
                $dirPath = dirname($relativePath);
                if ($dirPath === '.' || $dirPath === '') {
                    $namespaceSuffix = '';
                } else {
                    $namespaceSuffix = '\\' . $dirPath;
                }
                
                $correctNamespace = 'PYS_PRO_GLOBAL\\FacebookAds' . $namespaceSuffix;
                
                // Fix namespace - replace any broken namespace
                $content = preg_replace(
                    '/^namespace\s+PYS_PRO_GLOBAL\\\\FacebookAds\\\\[^;]+;/m',
                    'namespace ' . $correctNamespace . ';',
                    $content
                );
                
                file_put_contents($filePath, $content);
                $count++;
            }
        }
    }
    
    echo "✓ FacebookAds: $count files fixed\n";
}

// Fix GuzzleHttp files - all three packages
$guzzleConfigs = [
    ['path' => $vendorPath . '/guzzlehttp/guzzle/src', 'baseNs' => 'GuzzleHttp'],
    ['path' => $vendorPath . '/guzzlehttp/promises/src', 'baseNs' => 'GuzzleHttp\\Promise'],
    ['path' => $vendorPath . '/guzzlehttp/psr7/src', 'baseNs' => 'GuzzleHttp\\Psr7'],
];

$guzzleTotal = 0;
foreach ($guzzleConfigs as $config) {
    $guzzlePath = $config['path'];
    $baseNamespace = $config['baseNs'];
    
    if (!is_dir($guzzlePath)) {
        continue;
    }
    
    $iterator = new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator($guzzlePath, RecursiveDirectoryIterator::SKIP_DOTS),
        RecursiveIteratorIterator::SELF_FIRST
    );
    
    $count = 0;
    foreach ($iterator as $file) {
        if ($file->isFile() && $file->getExtension() === 'php') {
            $filePath = $file->getRealPath();
            $content = file_get_contents($filePath);
            
            // Check if namespace has file path in it (Windows path)
            if (preg_match('/^namespace\s+PYS_PRO_GLOBAL\\\\GuzzleHttp\\\\[A-Z]:/m', $content)) {
                // Get relative path
                $relativePath = str_replace($guzzlePath . DIRECTORY_SEPARATOR, '', $filePath);
                $relativePath = str_replace(DIRECTORY_SEPARATOR, '\\', $relativePath);
                $relativePath = str_replace('.php', '', $relativePath);
                
                // Get directory path (without filename)
                $dirPath = dirname($relativePath);
                if ($dirPath === '.' || $dirPath === '') {
                    $namespaceSuffix = '';
                } else {
                    $namespaceSuffix = '\\' . $dirPath;
                }
                
                $correctNamespace = 'PYS_PRO_GLOBAL\\' . $baseNamespace . $namespaceSuffix;
                
                // Fix namespace
                $content = preg_replace(
                    '/^namespace\s+PYS_PRO_GLOBAL\\\\GuzzleHttp\\\\[^;]+;/m',
                    'namespace ' . $correctNamespace . ';',
                    $content
                );
                
                file_put_contents($filePath, $content);
                $count++;
            }
        }
    }
    
    echo "✓ $baseNamespace: $count files fixed\n";
    $guzzleTotal += $count;
}

echo "\n✓ Total files fixed: " . ($count ?? 0) + $guzzleTotal . "\n";
echo "✓ All broken namespaces fixed!\n";

