<?php

namespace Database\Seeders;

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

class SettingsSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $settings = [
            // Application Settings
            'app.name' => 'Seraph3',
            'app.version' => '3.0.0',
            'app.timezone' => 'Europe/London',
            'app.locale' => 'en',
            
            // Company Information
            'company.name' => 'IT Support Services Ltd',
            'company.address' => '123 Technology Street',
            'company.city' => 'Manchester',
            'company.postcode' => 'M1 1AA',
            'company.phone' => '0161 123 4567',
            'company.email' => 'info@seraph.local',
            'company.website' => 'https://seraph.local',
            'company.vat_number' => 'GB123456789',
            'company.registration_number' => '12345678',
            
            // Email Settings
            'mail.default' => 'smtp',
            'mail.from_address' => 'noreply@seraph.local',
            'mail.from_name' => 'Seraph3 System',
            
            // Notification Settings
            'notifications.email_enabled' => true,
            'notifications.sms_enabled' => true,
            'notifications.desktop_enabled' => true,
            'notifications.task_assignment' => true,
            'notifications.task_completion' => true,
            'notifications.invoice_sent' => true,
            'notifications.payment_received' => true,
            
            // Business Settings
            'business.default_vat_rate' => 20,
            'business.currency' => 'GBP',
            'business.currency_symbol' => '£',
            'business.working_hours_start' => '09:00',
            'business.working_hours_end' => '17:00',
            'business.working_days' => json_encode(['monday', 'tuesday', 'wednesday', 'thursday', 'friday']),
            
            // Billing Settings
            'billing.default_payment_terms' => 30,
            'billing.late_payment_fee' => 25.00,
            'billing.invoice_prefix' => 'INV-',
            'billing.quote_prefix' => 'QUO-',
            'billing.next_invoice_number' => 1001,
            'billing.next_quote_number' => 2001,
            
            // Task Management
            'tasks.auto_assign' => false,
            'tasks.default_labour_rate' => 75.00,
            'tasks.emergency_labour_rate' => 100.00,
            'tasks.weekend_labour_rate' => 90.00,
            'tasks.minimum_charge' => 30.00,
            'tasks.default_booking_fee' => 15.00,
            
            // Stock Management
            'stock.low_stock_threshold' => 5,
            'stock.reorder_threshold' => 10,
            'stock.auto_reorder' => false,
            'stock.default_markup' => 20,
            'stock.business_markup' => 15,
            
            // Integration Settings - Default Values
            // Note: Actual credentials are managed through Admin → Integration Settings
            // These are just default enabled/disabled states for new installations
            'integrations.kashflow.enabled' => false,
            'integrations.kashflow.sync_years_back' => 1,
            'integrations.kashflow.is_sandbox' => false,
            'integrations.xero.enabled' => false,
            'integrations.twilio.enabled' => false,
            			// 'integrations.asterisk.enabled' removed - replaced with 3CX
            
            // Security Settings
            'security.session_timeout' => 120, // minutes
            'security.password_min_length' => 8,
            'security.require_2fa' => false,
            'security.login_attempts' => 5,
            'security.lockout_duration' => 15, // minutes
            
            // Backup Settings
            'backup.enabled' => true,
            'backup.frequency' => 'daily',
            'backup.retention_days' => 30,
            'backup.include_files' => true,
            
            // Feature Flags
            'features.chat_enabled' => true,
            'features.calendar_enabled' => true,
            'features.reports_enabled' => true,
            'features.gamification_enabled' => true,
            'features.mobile_app_enabled' => true,
            'features.api_enabled' => true,
            
            // Dashboard Settings
            'dashboard.default_widgets' => json_encode([
                'tasks_overview',
                'recent_tasks',
                'stock_alerts',
                'revenue_chart',
                'team_performance'
            ]),
            'dashboard.refresh_interval' => 300, // seconds
            
            // Maintenance Mode
            'maintenance.enabled' => false,
            'maintenance.message' => 'System is currently under maintenance. Please try again later.',
            'maintenance.allowed_ips' => json_encode(['127.0.0.1']),
        ];

        foreach ($settings as $key => $value) {
            // Use Laravel5 style - check if exists, then update or create
            $setting = Setting::find($key);
            if ($setting) {
                $setting->value = is_string($value) ? $value : json_encode($value);
                $setting->save();
            } else {
                $setting = new Setting();
                $setting->key = $key;
                $setting->value = is_string($value) ? $value : json_encode($value);
                $setting->save();
            }
        }

        $this->command->info('Created ' . count($settings) . ' system settings');
    }
} 