<?php

namespace App\Http\Controllers\Admin;

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

class ImpostazioniController extends Controller
{
    public function __construct()
    {
        $this->middleware('permission:view-impostazioni');
    }

    public function index()
    {
        // Carica le impostazioni attuali dell'utente
        $settings = [
            'dark_mode' => UserSetting::get('dark_mode', 'false'),
            'bg_color' => UserSetting::get('bg_color', '#ffffff'),
            'text_color' => UserSetting::get('text_color', '#1e293b'),
            'accent_color' => UserSetting::get('accent_color', '#6366f1'),
            'sidebar_bg_color' => UserSetting::get('sidebar_bg_color', '#fde047'),
            'sidebar_text_color' => UserSetting::get('sidebar_text_color', '#1e293b'),
            'sidebar_accent_color' => UserSetting::get('sidebar_accent_color', '#6366f1'),
        ];

        return view('admin.impostazioni.index', compact('settings'));
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'dark_mode' => 'string|in:true,false',
            'bg_color' => 'string|max:7',
            'text_color' => 'string|max:7',
            'accent_color' => 'string|max:7',
            'sidebar_bg_color' => 'string|max:7',
            'sidebar_text_color' => 'string|max:7',
            'sidebar_accent_color' => 'string|max:7',
        ]);

        // Salva le impostazioni per l'utente corrente
        foreach ($validated as $key => $value) {
            UserSetting::set($key, $value);
        }

        return response()->json(['success' => true, 'message' => 'Impostazioni salvate con successo!']);
    }

    public function theme(Request $request)
    {
        $theme = $request->input('theme', 'default');
        
        $themes = [
            'default' => [
                'bg_color' => '#ffffff',
                'text_color' => '#1e293b',
                'accent_color' => '#6366f1',
                'sidebar_bg_color' => '#fde047',
                'sidebar_text_color' => '#1e293b',
                'sidebar_accent_color' => '#6366f1',
            ],
            'dark' => [
                'bg_color' => '#1e293b',
                'text_color' => '#f1f5f9',
                'accent_color' => '#fbbf24',
                'sidebar_bg_color' => '#374151',
                'sidebar_text_color' => '#f1f5f9',
                'sidebar_accent_color' => '#fbbf24',
            ],
            'ocean' => [
                'bg_color' => '#f0f9ff',
                'text_color' => '#0c4a6e',
                'accent_color' => '#0ea5e9',
                'sidebar_bg_color' => '#0ea5e9',
                'sidebar_text_color' => '#ffffff',
                'sidebar_accent_color' => '#f0f9ff',
            ],
        ];

        if (isset($themes[$theme])) {
            foreach ($themes[$theme] as $key => $value) {
                UserSetting::set($key, $value);
            }
        }

        return response()->json(['success' => true, 'settings' => $themes[$theme] ?? []]);
    }
}