<?php

declare(strict_types=1);

namespace App\Controllers;

use App\Helpers\Response;
use App\Services\ConfigurationService;

class ConfigurationController extends BaseController
{
    private ConfigurationService $configService;

    public function __construct($request)
    {
        parent::__construct($request);
        $this->configService = new ConfigurationService();
    }

    public function index(): void
    {
        $configs = $this->configService->getAll();
        Response::success($configs, 'Configuraciones consultadas correctamente');
    }

    public function update(): void
    {
        $key = $this->getRouteParam('key');
        $data = $this->request->body();

        if (!isset($data['value'])) {
            Response::error('El valor es obligatorio', 422);
        }

        $config = $this->configService->update($key, $data['value'], $this->getAuthUserId());
        Response::success($config, 'Configuración actualizada correctamente');
    }
}
