<?php

namespace Database\Seeders;

use App\Models\PlatformSetting;
use Illuminate\Database\Seeder;

class PlatformSettingSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $settings = [
            [
                'key' => 'billing.unlock',
                'value' => [
                    'currency' => config('services.razorpay.currency', 'INR'),
                    'amount_paise' => config('services.razorpay.unlock_amount_paise', 0),
                ],
                'group' => 'billing',
                'type' => 'array',
                'is_encrypted' => false,
                'is_public' => false,
                'description' => 'One-time unlock amount used when creating Razorpay orders.',
            ],
            [
                'key' => 'jobs.categories',
                'value' => [
                    'tech',
                    'design',
                    'marketing',
                    'sales',
                    'writing',
                    'support',
                    'product',
                    'operations',
                    'virtual-assistant',
                    'finance',
                    'hr',
                    'data',
                    'ai',
                    'general',
                ],
                'group' => 'jobs',
                'type' => 'array',
                'is_encrypted' => false,
                'is_public' => true,
                'description' => 'Supported remote job categories shown throughout the product.',
            ],
            [
                'key' => 'features',
                'value' => [
                    'job_imports_enabled' => true,
                    'resume_analysis_enabled' => true,
                    'payments_enabled' => true,
                    'applications_enabled' => true,
                ],
                'group' => 'features',
                'type' => 'array',
                'is_encrypted' => false,
                'is_public' => false,
                'description' => 'Operational feature flags for core platform workflows.',
            ],
        ];

        foreach ($settings as $setting) {
            PlatformSetting::query()->updateOrCreate(
                [
                    'group' => $setting['group'],
                    'key' => $setting['key'],
                ],
                $setting
            );
        }

        foreach ((array) config('settings.groups', []) as $group => $groupDefinition) {
            foreach (($groupDefinition['settings'] ?? []) as $key => $definition) {
                $setting = PlatformSetting::query()->firstOrNew([
                    'group' => (string) $group,
                    'key' => (string) $key,
                ]);

                $defaultValue = ($definition['encrypted'] ?? false) || ($definition['read_only'] ?? false)
                    ? null
                    : ($definition['default'] ?? null);

                if (! $setting->exists || ($setting->value === null && $defaultValue !== null)) {
                    $setting->value = $defaultValue;
                }

                $setting->type = (string) ($definition['type'] ?? 'string');
                $setting->is_encrypted = (bool) ($definition['encrypted'] ?? false);
                $setting->is_public = (bool) (($definition['public'] ?? false) && ! ($definition['encrypted'] ?? false));
                $setting->description = $definition['helper'] ?? $definition['description'] ?? null;
                $setting->save();
            }
        }
    }
}
