<?php

namespace Modules\VetClinic\Entities;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

class VcModuleSetting extends Model
{
    protected $table = 'vc_module_settings';
    protected $guarded = ['id'];

    public static function get(int $businessId, string $key, $default = null)
    {
        if (!Schema::hasTable('vc_module_settings')) return $default;
        $row = DB::table('vc_module_settings')
            ->where('business_id', $businessId)
            ->where('key', $key)
            ->first();
        return $row ? $row->value : $default;
    }

    public static function set(int $businessId, string $key, $value): void
    {
        if (!Schema::hasTable('vc_module_settings')) return;
        DB::table('vc_module_settings')->updateOrInsert(
            ['business_id' => $businessId, 'key' => $key],
            ['value' => (string) $value, 'updated_at' => now(), 'created_at' => now()]
        );
    }
}
