<?php

namespace Modules\SystemCore\Database\Seeders;

use App\Services\Settings\ModuleSettingsService;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Modules\SystemCore\App\Services\SystemCoreSettings;

class SystemCoreSettingsSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $settingsService = app(ModuleSettingsService::class);
        $settings = app(SystemCoreSettings::class);

        // Remove old/deprecated settings
        $deprecatedSettings = [
            'default_sync_strategy',
            'sync_batch_size',
            'enable_sync_logging',
        ];

        DB::table('module_settings')
            ->where('module', 'SystemCore')
            ->whereIn('key', $deprecatedSettings)
            ->delete();

        // Seed new defaults
        $defaults = $settings->getDefaults();

        foreach ($defaults as $key => $value) {
            // Only seed if setting doesn't exist
            if ($settingsService->get('SystemCore', $key) === null) {
                $settingsService->set('SystemCore', $key, $value);
            }
        }

        $this->command->info('SystemCore settings seeded successfully.');
    }
}
