<?php
/**
 * Simple CSS/JS minifier for PrimeLink admin assets.
 *
 * Usage (CLI):
 *   php tools/minify-assets.php
 *
 * Usage (browser, on local/dev only):
 *   http(s)://your-site/wp-content/plugins/primelink/tools/minify-assets.php
 *
 * This script:
 * - Scanează assets/admin/css/*.css și assets/admin/js/*.js
 * - Creează / suprascrie fișiere .min.css / .min.js în aceleași directoare
 *
 * IMPORTANT:
 * - Nu lăsa acest fișier accesibil pe un site de producție public.
 * - După ce ai generat fișierele minificate, îl poți șterge liniștit.
 */

declare(strict_types=1);

$root = dirname(__DIR__); // plugin root

function primelink_minify_css(string $css): string {
    // Remove comments
    $css = preg_replace('!/\*.*?\*/!s', '', $css);
    $css = preg_replace('/\s+/', ' ', $css);
    $search  = [' :', ': ', ' ;', '; ', ' {', '{ ', ' }', '} ', ' ,', ', '];
    $replace = [':', ':', ';', ';', '{', '{', '}', '}', ',', ','];
    $css = str_replace($search, $replace, $css);
    $css = preg_replace('/;\}/', '}', $css);
    return trim($css);
}

function primelink_minify_js(string $js): string {
    // Very simple JS minifier: strip comments & extra whitespace.
    // Not as bun ca terser, dar suficient pentru admin assets.

    // Remove /* */ comments
    $js = preg_replace('!/\*.*?\*/!s', '', $js);
    // Remove // comments (line) - use # delimiter to avoid escaping //
    $js = preg_replace('#(^|\s)//.*$#m', '$1', $js);
    // Collapse whitespace
    $js = preg_replace('/\s+/', ' ', $js);
    return trim($js);
}

function primelink_minify_file(string $src, string $dest, callable $minifier): void {
    if (!file_exists($src)) {
        echo "Skip (missing): {$src}\n";
        return;
    }

    $contents = file_get_contents($src);
    if ($contents === false) {
        echo "ERROR: Cannot read {$src}\n";
        return;
    }

    $minified = $minifier($contents);
    if (file_put_contents($dest, $minified) === false) {
        echo "ERROR: Cannot write {$dest}\n";
        return;
    }

    echo "OK: {$dest}\n";
}

// Detect execution context
$is_cli = (php_sapi_name() === 'cli');

if (!$is_cli) {
    header('Content-Type: text/plain; charset=utf-8');
}

echo "PrimeLink asset minifier\n";
echo "Root: {$root}\n\n";

// CSS files to minify
$css_files = [
    'assets/admin/css/mt-primelink-admin.css',
    'assets/admin/css/mt-primelink-glass.css',
    'assets/admin/css/activation-wizard.css',
];

echo "Minifying CSS:\n";
foreach ($css_files as $rel) {
    $src  = $root . DIRECTORY_SEPARATOR . $rel;
    $dest = preg_replace('/\.css$/', '.min.css', $src);
    primelink_minify_file($src, $dest, 'primelink_minify_css');
}

// JS files to minify
$js_files = [
    'assets/admin/js/mt-primelink-admin.js',
    'assets/admin/js/activation-wizard.js',
];

echo "\nMinifying JS:\n";
foreach ($js_files as $rel) {
    $src  = $root . DIRECTORY_SEPARATOR . $rel;
    $dest = preg_replace('/\.js$/', '.min.js', $src);
    primelink_minify_file($src, $dest, 'primelink_minify_js');
}

echo "\nDone.\n";


