<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

/**
 * WordPress Toolkit Phase 2 — installed_apps genişletme + wp_action_log.
 *
 * Eklenen alanlar:
 *   wp_version, wp_debug_enabled, wp_maintenance_mode
 *   auto_update_core, auto_update_plugins, auto_update_themes
 *   security_hardened, is_staging_of, staging_label
 *
 * Yeni tablo: wp_action_log (WP-CLI komut audit trail)
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::table('installed_apps', function (Blueprint $table) {
            if (! Schema::hasColumn('installed_apps', 'wp_version')) {
                $table->string('wp_version', 20)->nullable()->after('version');
            }
            if (! Schema::hasColumn('installed_apps', 'wp_debug_enabled')) {
                $table->boolean('wp_debug_enabled')->default(false)->after('wp_version');
            }
            if (! Schema::hasColumn('installed_apps', 'wp_maintenance_mode')) {
                $table->boolean('wp_maintenance_mode')->default(false)->after('wp_debug_enabled');
            }
            if (! Schema::hasColumn('installed_apps', 'auto_update_core')) {
                $table->enum('auto_update_core', ['off', 'minor', 'all'])->default('minor')->after('wp_maintenance_mode');
            }
            if (! Schema::hasColumn('installed_apps', 'auto_update_plugins')) {
                $table->boolean('auto_update_plugins')->default(false)->after('auto_update_core');
            }
            if (! Schema::hasColumn('installed_apps', 'auto_update_themes')) {
                $table->boolean('auto_update_themes')->default(false)->after('auto_update_plugins');
            }
            if (! Schema::hasColumn('installed_apps', 'security_hardened')) {
                $table->boolean('security_hardened')->default(false)->after('auto_update_themes');
            }
            if (! Schema::hasColumn('installed_apps', 'is_staging_of')) {
                $table->foreignId('is_staging_of')
                    ->nullable()
                    ->after('security_hardened')
                    ->constrained('installed_apps')
                    ->nullOnDelete();
            }
            if (! Schema::hasColumn('installed_apps', 'staging_label')) {
                $table->string('staging_label', 50)->nullable()->after('is_staging_of');
            }
        });

        // WordPress action log — WP-CLI komutlarının audit trail'i
        Schema::create('wp_action_log', function (Blueprint $table) {
            $table->id();
            $table->foreignId('installed_app_id')->constrained('installed_apps')->cascadeOnDelete();
            $table->foreignId('triggered_by')->nullable()->constrained('users')->nullOnDelete();
            $table->string('action', 100)->comment('plugin.install, theme.activate, user.add, vb.');
            $table->json('parameters')->nullable();
            $table->boolean('success')->default(false);
            $table->text('output')->nullable();
            $table->integer('duration_ms')->nullable();
            $table->timestamp('executed_at')->useCurrent();
            $table->timestamps();

            $table->index(['installed_app_id', 'executed_at']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('wp_action_log');

        Schema::table('installed_apps', function (Blueprint $table) {
            // Foreign key önce düşürülmeli
            if (Schema::hasColumn('installed_apps', 'is_staging_of')) {
                $table->dropForeign(['is_staging_of']);
                $table->dropColumn('is_staging_of');
            }

            $cols = [
                'wp_version', 'wp_debug_enabled', 'wp_maintenance_mode',
                'auto_update_core', 'auto_update_plugins', 'auto_update_themes',
                'security_hardened', 'staging_label',
            ];

            foreach ($cols as $col) {
                if (Schema::hasColumn('installed_apps', $col)) {
                    $table->dropColumn($col);
                }
            }
        });
    }
};
