<?php

namespace Modules\PMCore\Database\Seeders;

use App\Services\Settings\ModuleSettingsService;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Modules\PMCore\App\Settings\PMCoreSettings;

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

        // Remove deprecated settings that are no longer in the settings class
        $deprecatedSettings = [
            'allow_future_timesheets',
            'max_hours_per_day',
            'require_task_assignment',
            'budget_warning_threshold',
            'budget_alert_threshold',
            'default_currency_symbol',
            'default_allocation_percentage',
            'allow_multiple_project_managers',
        ];

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

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

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

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