<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

/**
 * PRC-318 / D-11 — addendum to seed default values for the new cycle
 * settings. The main PRC-318 migration only DELETED the legacy enum
 * keys; the new `domain_*_sync_cycle_days` keys relied on
 * `Setting::getValue($key, $default)` fallback semantics. Functional
 * but admin Settings UI shows an "unset" state instead of the
 * intended default. Insert default rows for consistency with
 * PRC-317 / other recent settings migrations.
 */
return new class extends Migration
{
    private array $defaults = [
        'domain_sync_cycle_days' => 7,
        'domain_expiring_sync_cycle_days' => 1,
        'domain_transfer_sync_cycle_days' => 1,
    ];

    public function up(): void
    {
        foreach ($this->defaults as $key => $default) {
            DB::table('settings')->updateOrInsert(
                ['key' => $key],
                [
                    'value' => (string) $default,
                    'group' => 'domains',
                    'created_at' => now(),
                    'updated_at' => now(),
                ]
            );
        }
    }

    public function down(): void
    {
        DB::table('settings')->whereIn('key', array_keys($this->defaults))->delete();
    }
};
