<?php

declare(strict_types=1);

namespace CloudXM\WooSDK\Config;

use CloudXM\WooSDK\Support\HasLogger;

/**
 * WordPress option wrapper with default merging. Stores everything under a single option row.
 */
class SettingsService
{
    use HasLogger;

    public function __construct(
        private string $optionName,
        private array $defaults = []
    ) {}

    /** Retrieve all settings, merged with defaults. */
    public function all(): array
    {
        $stored = get_option($this->optionName, []);
        if (!is_array($stored)) {
            $stored = [];
        }
        return array_merge($this->defaults, $stored);
    }

    /** Get a single setting value. */
    public function get(string $key, mixed $default = null): mixed
    {
        $settings = $this->all();
        return $settings[$key] ?? $default;
    }

    /** Persist settings – merges with previously stored array. */
    public function save(array $values): bool
    {
        $merged = array_merge($this->all(), $values);
        return update_option($this->optionName, $merged);
    }

    /** Delete option entirely. */
    public function reset(): bool
    {
        return delete_option($this->optionName);
    }
}
