<?php

namespace Modules\Vrc\Services\Settings;

use Modules\Vrc\Entities\VrcSetting;

/**
 * Typed accessor over the per-business vrc_settings KV store, falling back to
 * config('vrc.defaults'). This is the single place that knows the repair-
 * specific settings surface (spec §6.15) — labor rate, estimate validity,
 * the native-vs-ERP source strategy for estimates & catalog, and the comms
 * matrix. Currency/tax/timezone/numbering are NOT here — they stay in ERP
 * business settings.
 */
class VrcSettings
{
    /** Keys managed by the VRC settings screen and their default source. */
    public static function defaults(): array
    {
        $d = (array) config('vrc.defaults', []);

        return [
            'default_labor_rate'     => $d['default_labor_rate'] ?? 0,
            'estimate_validity_days' => $d['estimate_validity_days'] ?? 14,
            'estimate_source'        => $d['estimate_source'] ?? 'native',
            'service_catalog_source' => $d['service_catalog_source'] ?? 'native',
            'comms_channel'          => $d['comms_channel'] ?? 'sms',
            'comms_matrix'           => $d['comms_matrix'] ?? [],
        ];
    }

    /** All VRC settings for a business, merged over defaults. */
    public static function all(int $businessId): array
    {
        $out = [];
        foreach (self::defaults() as $key => $default) {
            $out[$key] = VrcSetting::get($businessId, $key, $default);
        }
        return $out;
    }

    public static function get(int $businessId, string $key, $default = null)
    {
        $defaults = self::defaults();
        return VrcSetting::get($businessId, $key, $default ?? ($defaults[$key] ?? null));
    }

    public static function put(int $businessId, string $key, $value): void
    {
        VrcSetting::put($businessId, $key, $value);
    }
}
