<?php

namespace App\Http\Controllers\Superadmin;

use App\Http\Controllers\Controller;
use App\Models\Setting;
use Illuminate\Http\Request;

class SystemSettingsController extends Controller
{
    /**
     * Display system settings page
     */
    public function index()
    {
        $settings = Setting::all()->groupBy('group');
        
        return view('superadmin.settings.index', compact('settings'));
    }

    /**
     * Update system settings
     */
    public function update(Request $request)
    {
        $validated = $request->validate([
            'app_name' => 'nullable|string|max:100',
            'app_description' => 'nullable|string|max:500',
            'app_version' => 'nullable|string|max:50',
            'support_email' => 'nullable|email|max:100',
            'support_phone' => 'nullable|string|max:20',
            'maintenance_mode' => 'nullable|boolean',
            'max_login_attempts' => 'nullable|integer|min:1|max:100',
            'session_timeout' => 'nullable|integer|min:5|max:1440',
            'backup_enabled' => 'nullable|boolean',
            'backup_frequency' => 'nullable|in:daily,weekly,monthly',
            'log_level' => 'nullable|in:debug,info,notice,warning,error,critical,alert,emergency',
            'sms_gateway' => 'nullable|string|max:50',
            'email_notification' => 'nullable|boolean',
        ]);

        // Application Settings
        if (isset($validated['app_name'])) {
            Setting::set('app_name', $validated['app_name'], 'application', 'Application name');
        }
        if (isset($validated['app_description'])) {
            Setting::set('app_description', $validated['app_description'], 'application', 'Application description');
        }
        if (isset($validated['app_version'])) {
            Setting::set('app_version', $validated['app_version'], 'application', 'Application version');
        }

        // Contact Settings
        if (isset($validated['support_email'])) {
            Setting::set('support_email', $validated['support_email'], 'contact', 'Support email address');
        }
        if (isset($validated['support_phone'])) {
            Setting::set('support_phone', $validated['support_phone'], 'contact', 'Support phone number');
        }

        // Security Settings
        if (isset($validated['maintenance_mode'])) {
            Setting::set('maintenance_mode', $validated['maintenance_mode'], 'security', 'Maintenance mode');
        }
        if (isset($validated['max_login_attempts'])) {
            Setting::set('max_login_attempts', $validated['max_login_attempts'], 'security', 'Maximum login attempts');
        }
        if (isset($validated['session_timeout'])) {
            Setting::set('session_timeout', $validated['session_timeout'], 'security', 'Session timeout in minutes');
        }

        // Backup Settings
        if (isset($validated['backup_enabled'])) {
            Setting::set('backup_enabled', $validated['backup_enabled'], 'backup', 'Enable automatic backup');
        }
        if (isset($validated['backup_frequency'])) {
            Setting::set('backup_frequency', $validated['backup_frequency'], 'backup', 'Backup frequency');
        }

        // Logging Settings
        if (isset($validated['log_level'])) {
            Setting::set('log_level', $validated['log_level'], 'logging', 'Log level');
        }

        // Integration Settings
        if (isset($validated['sms_gateway'])) {
            Setting::set('sms_gateway', $validated['sms_gateway'], 'integration', 'SMS gateway provider');
        }
        if (isset($validated['email_notification'])) {
            Setting::set('email_notification', $validated['email_notification'], 'integration', 'Enable email notifications');
        }

        return redirect()->route('superadmin.settings.index')
            ->with('success', 'Pengaturan sistem berhasil diperbarui');
    }

    /**
     * Clear application cache
     */
    public function clearCache()
    {
        \Illuminate\Support\Facades\Cache::flush();
        
        return redirect()->route('superadmin.settings.index')
            ->with('success', 'Cache aplikasi berhasil dihapus');
    }

    /**
     * Clear application logs
     */
    public function clearLogs()
    {
        $logPath = storage_path('logs');
        if (is_dir($logPath)) {
            array_map('unlink', glob("$logPath/*.log"));
        }
        
        return redirect()->route('superadmin.settings.index')
            ->with('success', 'Log aplikasi berhasil dihapus');
    }
}
