<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\StructureSetting;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;

class StructureSettingController extends Controller
{
    /**
     * Get all structure settings.
     * GET /api/structure-settings
     */
    public function index(): JsonResponse
    {
        $settings = StructureSetting::all()->pluck('value', 'key');

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

    /**
     * Update structure settings.
     * POST /api/structure-settings
     */
    public function update(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'presidium_label'   => 'required|string|max:255',
            'koordinator_label' => 'required|string|max:255',
            'seksi_label'       => 'required|string|max:255',
        ]);

        foreach ($validated as $key => $value) {
            StructureSetting::updateOrCreate(
                ['key' => $key],
                ['value' => $value]
            );
        }

        return response()->json([
            'success' => true,
            'message' => 'Structure settings updated successfully.',
            'data' => StructureSetting::all()->pluck('value', 'key'),
        ]);
    }
}
