<?php

use App\Models\Setting;
use Illuminate\Database\Migrations\Migration;

return new class extends Migration
{
    public function up(): void
    {
        // PRC-355: disable_reply_email_logging → reply_email_logging_enabled
        // rename + semantic invert (enabled = !disabled). Admin niyetini
        // korur: old disable=true → new enabled=false; old disable=false /
        // anahtar yok → new enabled=true (yeni default).
        $old = Setting::where('key', 'disable_reply_email_logging')->first();
        $disabled = $old
            ? filter_var($old->value, FILTER_VALIDATE_BOOLEAN)
            : false;

        Setting::updateOrCreate(
            ['key' => 'reply_email_logging_enabled'],
            ['value' => $disabled ? '0' : '1', 'group' => 'support']
        );

        Setting::where('key', 'disable_reply_email_logging')->delete();
    }

    public function down(): void
    {
        $new = Setting::where('key', 'reply_email_logging_enabled')->first();
        $enabled = $new
            ? filter_var($new->value, FILTER_VALIDATE_BOOLEAN)
            : true;

        Setting::updateOrCreate(
            ['key' => 'disable_reply_email_logging'],
            ['value' => $enabled ? '0' : '1', 'group' => 'support']
        );

        Setting::where('key', 'reply_email_logging_enabled')->delete();
    }
};
