<?php
/**
 * ITSWEBER CMS – Migration 028: Deactivate Core Plugins
 *
 * Removes plugins from plugins_active.json that are now built-in
 * core sections. The plugin files themselves are not deleted.
 *
 * @author    Benny Weber (ITSWEBER)
 * @copyright © ITSWEBER – www.itsweber.de
 * @contact   kontakt@itsweber.de
 */

use App\Database\Migration;

class DeactivateCorePlugins extends Migration
{
    /**
     * Plugin slugs that have been migrated into core sections.
     */
    private array $coreSlugs = [
        'before-after-section',
        'feature-showcase-section',
        'html-section',
        'process-steps-section',
        'rating-highlight-section',
        'service-cards-section',
        'site-notice',
        'social-embed-section',
        'team-section',
        'trust-showcase-section',
        'video-section',
    ];

    public function up(): void
    {
        $basePath = defined('BASE_PATH') ? BASE_PATH : dirname(__DIR__, 2);
        $file = $basePath . '/storage/plugins_active.json';

        if (!is_file($file)) {
            return;
        }

        $json = @file_get_contents($file);
        $active = @json_decode($json, true);

        if (!is_array($active)) {
            return;
        }

        $changed = false;
        foreach ($this->coreSlugs as $slug) {
            $key = array_search($slug, $active, true);
            if ($key !== false) {
                unset($active[$key]);
                $changed = true;
            }
        }

        if ($changed) {
            $active = array_values($active); // Re-index
            file_put_contents($file, json_encode($active, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
        }
    }

    public function down(): void
    {
        // Cannot reliably restore which plugins were active before.
        // Manual re-activation via Admin > Plugins is required.
    }
}
