<?php

namespace App\Http\Controllers\API\SuperAdmin;

use App\Http\Controllers\API\BaseApiController;
use App\Models\SystemSetting;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class SystemSettingController extends BaseApiController
{
    public function index(): JsonResponse
    {
        return $this->successResponse(SystemSetting::all()->groupBy('group'));
    }

    public function update(Request $request, string $key): JsonResponse
    {
        $setting = SystemSetting::where('key', $key)->firstOrFail();
        $setting->update(['value' => $request->value]);
        return $this->successResponse($setting, 'Configuração actualizada');
    }

    public function bulkUpdate(Request $request): JsonResponse
    {
        $request->validate(['settings' => 'required|array']);

        foreach ($request->settings as $key => $value) {
            SystemSetting::where('key', $key)->update(['value' => $value]);
        }

        return $this->successResponse(null, 'Configurações actualizadas');
    }
}
