<?php

declare(strict_types=1);

namespace App\Http\Controllers\Market;

use App\Http\Controllers\Helpers\ResponseHelper;
use App\Models\User;
use Hypervel\Http\Request;
use Hypervel\Support\Facades\Auth;

class UserSettingsController
{
    /**
     * Get the current user's settings.
     */
    public function getSettings()
    {
        $user = Auth::user();
        if (!$user) {
            return ResponseHelper::returnUnauthorized();
        }

        return response()->json($user->settings ?? []);
    }

    /**
     * Update the current user's settings.
     */
    public function updateSettings(Request $request)
    {
        $user = Auth::user();
        if (!$user) {
            return ResponseHelper::returnUnauthorized();
        }

        $newSettings = $request->all();
        $currentSettings = $user->settings ?? [];

        // Merge new settings into current settings
        $updatedSettings = array_merge($currentSettings, $newSettings);

        // Save to database
        $user->settings = $updatedSettings;
        $user->save();

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