<?php

namespace App\Services;

class ConfigService extends ApiService
{
    /**
     * Obtener todas las configuraciones
     */
    public function getConfigurations(): array
    {
        $response = $this->makeApiRequest('GET', 'configurations', [], [], 'get_configurations');

        if (!$response['success']) {
            throw new \Exception($response['message'] ?? 'Error al obtener configuraciones');
        }

        return $response['data'] ?? [];
    }

    /**
     * Obtener configuración por clave
     */
    public function getConfiguration(string $key): array
    {
        $response = $this->makeApiRequest('GET', "configurations/{$key}", [], [], 'get_configuration');

        if (!$response['success']) {
            throw new \Exception($response['message'] ?? 'Error al obtener configuración');
        }

        return $response['data'] ?? [];
    }

    /**
     * Actualizar configuración
     */
    public function updateConfiguration(string $key, string $value): array
    {
        $response = $this->makeApiRequest('PUT', "configurations/{$key}", ['value' => $value], [], 'update_configuration');

        if (!$response['success']) {
            throw new \Exception($response['message'] ?? 'Error al actualizar configuración');
        }

        return $response['data'] ?? [];
    }

    /**
     * Crear nueva configuración
     */
    public function createConfiguration(array $data): array
    {
        $response = $this->makeApiRequest('POST', 'configurations', $data, [], 'create_configuration');

        if (!$response['success']) {
            throw new \Exception($response['message'] ?? 'Error al crear configuración');
        }

        return $response['data'] ?? [];
    }

    /**
     * Eliminar configuración
     */
    public function deleteConfiguration(string $key): bool
    {
        $response = $this->makeApiRequest('DELETE', "configurations/{$key}", [], [], 'delete_configuration');

        if (!$response['success']) {
            throw new \Exception($response['message'] ?? 'Error al eliminar configuración');
        }

        return true;
    }

    /**
     * Obtener configuraciones por categoría
     */
    public function getConfigurationsByCategory(string $category): array
    {
        $response = $this->makeApiRequest('GET', 'configurations', [], ['category' => $category], 'get_configurations_by_category');

        if (!$response['success']) {
            throw new \Exception($response['message'] ?? 'Error al obtener configuraciones por categoría');
        }

        return $response['data'] ?? [];
    }
}