<?php

namespace App\Http\Controllers\Api;

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

class SettingController extends Controller
{
    public function index()
    {
        $settings = Setting::all()->pluck('value', 'key');
        return response()->json($settings);
    }

    public function update(Request $request)
    {
        $data = $request->validate([
            'tax_percentage' => 'nullable|numeric|min:0|max:100',
            'service_fee' => 'nullable|numeric|min:0',
        ]);

        foreach ($data as $key => $value) {
            if ($value !== null) {
                Setting::updateOrCreate(['key' => $key], ['value' => (string) $value]);
            }
        }

        return response()->json(['message' => 'Pengaturan berhasil disimpan', 'settings' => Setting::all()->pluck('value', 'key')]);
    }
}
