<?php

namespace App\Services\Settings;

use App\Models\Configuration;

class ConfigurationService
{
    public function list(): array
    {
        return Configuration::query()
            ->orderBy('id')
            ->get()
            ->toArray();
    }

    public function store(array $payload): Configuration
    {
        return Configuration::query()->create([
            'config_key' => (string) $payload['config_key'],
            'config_value' => (string) $payload['config_value'],
        ]);
    }

    public function update(int $id, array $payload): ?Configuration
    {
        $config = Configuration::query()->find($id);
        if (!$config) {
            return null;
        }

        $config->update([
            'config_key' => (string) $payload['config_key'],
            'config_value' => (string) $payload['config_value'],
        ]);

        return $config;
    }

    public function delete(int $id): bool
    {
        $config = Configuration::query()->find($id);
        if (!$config) {
            return false;
        }

        return (bool) $config->delete();
    }
}
