<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ThemeController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function update(Request $request)
    {
        $request->validate([
            'theme'         => 'sometimes|string|in:' . implode(',', array_keys(config('themes', []))),
            'sidebar_style' => 'sometimes|string|in:dark,light',
        ]);

        $user = auth()->user();

        if ($request->has('theme')) {
            $user->theme = $request->theme;
        }
        if ($request->has('sidebar_style')) {
            $user->sidebar_style = $request->sidebar_style;
        }

        $user->save();

        if ($request->expectsJson()) {
            return response()->json(['success' => true]);
        }

        return back()->with('success', 'Appearance updated.');
    }
}
