<?php

use App\Models\SystemSetting;

class SystemSettingSeeder extends \Hyperf\Database\Seeders\Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $settings = [
            // General Settings
            [
                'key' => 'app_name',
                'value' => 'BukidBounty',
                'type' => 'text',
                'group' => 'general',
                'label' => 'Application Name',
                'description' => 'The public name of the platform.'
            ],
            [
                'key' => 'app_description',
                'value' => 'Agricultural Management Platform & Marketplace ecosystem for modern farming communities.',
                'type' => 'text',
                'group' => 'general',
                'label' => 'Application Description',
                'description' => 'Brief description of the application.'
            ],
            [
                'key' => 'app_tagline',
                'value' => 'Bounty of the Fields at Your Fingertips',
                'type' => 'text',
                'group' => 'general',
                'label' => 'Tagline',
                'description' => 'Application tagline or slogan.'
            ],

            // Branding Settings
            [
                'key' => 'app_logo',
                'value' => null,
                'type' => 'image',
                'group' => 'branding',
                'label' => 'Application Logo',
                'description' => 'Upload a logo to replace the default icon.'
            ],
            [
                'key' => 'primary_color',
                'value' => '#0d6efd',
                'type' => 'text',
                'group' => 'branding',
                'label' => 'Primary Color',
                'description' => 'Global primary accent color for the theme. Auto-derived from the uploaded logo.'
            ],
            [
                'key' => 'accent_color',
                'value' => '#533dea',
                'type' => 'text',
                'group' => 'branding',
                'label' => 'Accent Color',
                'description' => 'Secondary accent color. Auto-derived from the uploaded logo.'
            ],
            [
                'key' => 'background_tint',
                'value' => '#f5f4ff',
                'type' => 'text',
                'group' => 'branding',
                'label' => 'Background Tint',
                'description' => 'Soft background tint derived from the logo for hero/home backgrounds.'
            ],

            // Advanced Settings
            [
                'key' => 'footer_text',
                'value' => '&copy; 2026 BukidBounty Ecosystem. All rights reserved.',
                'type' => 'text',
                'group' => 'advanced',
                'label' => 'Footer Text',
                'description' => 'Text displayed in the application footer.'
            ],
            [
                'key' => 'maintenance_mode',
                'value' => 'false',
                'type' => 'boolean',
                'group' => 'advanced',
                'label' => 'Maintenance Mode',
                'description' => 'Enable or disable maintenance mode globally.'
            ],
            [
                'key' => 'priority_sectors',
                'value' => json_encode(['INDIGENT', "PWD's", 'SENIOR', 'Solo Parents', "OFW's", 'Indigenious Youth', 'Farmers', 'FISHERMEN', 'Calamity', 'Retrenchment', 'Former Rebels', 'Former Violent Extremist Groups']),
                'type' => 'json',
                'group' => 'general',
                'label' => 'Priority Sectors',
                'description' => 'List of sectors eligible for priority programs and assistance.'
            ],
            [
                'key' => 'group_types',
                'value' => json_encode(['COOPERATIVE', 'ASSOCIATION', 'CORPORATION', 'NGO', 'OTHER']),
                'type' => 'json',
                'group' => 'general',
                'label' => 'Group Types',
                'description' => 'Types of organizations or associations members can belong to.'
            ],
            [
                'key' => 'default_address_fields',
                'value' => json_encode(['house_no', 'street', 'barangay', 'city', 'province', 'region', 'country', 'zipcode']),
                'type' => 'json',
                'group' => 'general',
                'label' => 'Default Address Fields',
                'description' => 'Standard fields for address objects in the system.'
            ],
            [
                'key' => 'app_mode',
                'value' => 'corporate',
                'type' => 'select',
                'options' => json_encode(['corporate', 'cooperative', 'ngo', 'tandem', 'others']),
                'group' => 'general',
                'label' => 'Application Mode',
                'description' => 'Defines the operational mode of the platform, which affects available features and terminology.'
            ],
            [
                'key' => 'main_organization',
                'value' => null,
                'type' => 'organization',
                'group' => 'general',
                'label' => 'Main Cooperative / Organization',
                'description' => 'Designates the primary cooperative or organization for the platform. Used mostly when Application Mode is set to cooperative.'
            ],
            [
                'key' => 'chapter_positions',
                'value' => json_encode(['National Director', 'Regional Director', 'Provincial Coordinator', 'City/Municipal Officer', 'Barangay Captain', 'Secretary', 'Treasurer', 'Auditor', 'Member']),
                'type' => 'json',
                'group' => 'general',
                'label' => 'Chapter Positions',
                'description' => 'Available position titles that can be assigned to members within regional chapters.'
            ],
            [
                'key' => 'accounting_theme',
                'value' => 'blank',
                'type' => 'select',
                'options' => json_encode(['blank', 'general_business', 'retail_pos', 'service_business', 'agri_coop']),
                'group' => 'general',
                'label' => 'Accounting Theme',
                'description' => 'Starter Chart of Accounts preset for this deployment. Defined in config/accounting/themes.php. Switching only changes the theme tag used for branching and drift detection — it does not migrate existing accounts.'
            ],
            [
                'key' => 'top_up_enabled',
                'value' => 'false',
                'type' => 'boolean',
                'group' => 'advanced',
                'label' => 'Top Up Enabled',
                'description' => 'Enable or disable the credit top-up feature globally.'
            ],
            [
                'key' => 'default_org_type',
                'value' => 'COOPERATIVE',
                'type' => 'select',
                'options' => json_encode(['COOPERATIVE', 'NGO', 'CORPORATION']),
                'group' => 'general',
                'label' => 'Default Organization Type',
                'description' => 'Pre-selected type when creating a new organization. Options: Cooperative, NGO, Corporation/Company.',
            ],
        ];

        foreach ($settings as $setting) {
            $exists = SystemSetting::where('key', $setting['key'])->first();
            if ($exists) {
                // Preserve the operator's chosen value; only refresh schema/metadata
                // (type, options, group, label, description) so re-seeding never
                // clobbers configured settings.
                $update = $setting;
                unset($update['value']);
                $exists->update($update);
            } else {
                SystemSetting::create($setting);
            }
        }

        SystemSetting::clearCache();
    }
}
