<?php
/**
 * Bulk Update Script for Report Headers
 * This script updates all print report files with dynamic store settings
 * 
 * Run this script once to update all remaining files
 */

$baseDir = __DIR__;

// CSS template for company section
$cssTemplate = <<<'CSS'
        .company-section {
            flex: 1;
            display: flex;
            align-items: flex-start;
            gap: 12px;
        }

        .logo-container {
            flex-shrink: 0;
        }

        .logo-container img {
            width: 60px;
            height: 60px;
            object-fit: contain;
        }

        .company-info {
            flex: 1;
        }

        .company-name {
            font-size: 16px;
            font-weight: 700;
            letter-spacing: 0.5px;
            color: var(--primary-color);
            text-transform: uppercase;
        }

        .company-details {
            font-size: 9px;
            color: var(--muted-text);
            margin-top: 2px;
            line-height: 1.5;
        }
CSS;

// HTML template for company header
$htmlTemplate = <<<'HTML'
                    <div class="company-section">
                        <?php if ($storeSettings && !empty($storeSettings['StoreLogo1'])): ?>
                        <div class="logo-container">
                            <img src="../<?php echo htmlspecialchars($storeSettings['StoreLogo1']); ?>" alt="Logo">
                        </div>
                        <?php endif; ?>
                        <div class="company-info">
                            <div class="company-name"><?php echo strtoupper(htmlspecialchars($storeSettings['StoreName'] ?? 'STORE NAME')); ?></div>
                            <div class="company-details">
                                <?php if (!empty($storeSettings['Address'])): ?>
                                <?php echo htmlspecialchars($storeSettings['Address']); ?><br>
                                <?php endif; ?>
                                <?php if (!empty($storeSettings['City'])): ?>
                                <?php echo htmlspecialchars($storeSettings['City']); ?><?php echo !empty($storeSettings['Postcode']) ? ' - ' . htmlspecialchars($storeSettings['Postcode']) : ''; ?><br>
                                <?php endif; ?>
                                <?php if (!empty($storeSettings['Mobile'])): ?>
                                Ph: <?php echo htmlspecialchars($storeSettings['Mobile']); ?>
                                <?php endif; ?>
                                <?php if (!empty($storeSettings['GSTNumber'])): ?>
                                | GSTIN: <?php echo htmlspecialchars($storeSettings['GSTNumber']); ?>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
HTML;

echo "Bulk Update Script for Report Headers\n";
echo "=====================================\n\n";

// Files to update (add paths here)
$files = [
    'SalesReports/print-detailed-sales.php',
    'SalesReports/print-cash-flow.php',
    'SalesReports/print-item-stock-report.php',
    'SalesReports/print-customer-accounts.php',
    'PurchaseReports/print-gst-purchase.php',
    'PurchaseReports/print-item-wise-purchase.php',
    'Purchase/print-supplier-ledger.php',
];

foreach ($files as $file) {
    $fullPath = $baseDir . '/' . $file;
    echo "Processing: $file\n";
    
    if (!file_exists($fullPath)) {
        echo "  [SKIP] File not found\n\n";
        continue;
    }
    
    $content = file_get_contents($fullPath);
    $modified = false;
    
    // Step 1: Update CSS if old pattern exists
    $oldCss = '/\.company-section\s*\{\s*flex:\s*1;\s*\}\s*\.company-name/s';
    if (preg_match($oldCss, $content)) {
        $content = preg_replace($oldCss, $cssTemplate . "\n\n        .company-name", $content);
        $modified = true;
        echo "  [OK] Updated CSS\n";
    }
    
    // Step 2: Replace hardcoded company names
    $patterns = [
        '/<div class="company-section">\s*<div class="company-name">UZRS Inventory System<\/div>\s*<div class="company-details">.*?<\/div>\s*<\/div>\s*<div class="report-info">/s',
        '/<div class="company-section">\s*<div class="company-name">UZRS INVENTORY SYSTEM<\/div>\s*<div class="company-details">.*?<\/div>\s*<\/div>\s*<div class="report-info">/s',
    ];
    
    foreach ($patterns as $pattern) {
        if (preg_match($pattern, $content)) {
            $replacement = $htmlTemplate . "\n                    <div class=\"report-info\">";
            $content = preg_replace($pattern, $replacement, $content);
            $modified = true;
            echo "  [OK] Updated HTML header\n";
            break;
        }
    }
    
    if ($modified) {
        file_put_contents($fullPath, $content);
        echo "  [SUCCESS] File updated\n\n";
    } else {
        echo "  [INFO] No changes needed\n\n";
    }
}

echo "=====================================\n";
echo "Bulk update complete!\n";
?>
