<?php

namespace App\Modules\Admin\Operations\Services;

use App\Modules\Admin\Operations\Models\FeatureFlag;

final class FeatureFlagService
{
    public function enabled(string $key, ?int $tenantId = null): bool
    {
        if ($tenantId !== null) {
            $tenantOverride = FeatureFlag::query()
                ->where('tenant_id', $tenantId)
                ->where('key', $key)
                ->value('enabled');
            if ($tenantOverride !== null) {
                return (bool) $tenantOverride;
            }
        }

        $global = FeatureFlag::query()
            ->whereNull('tenant_id')
            ->where('key', $key)
            ->value('enabled');
        if ($global !== null) {
            return (bool) $global;
        }

        return (bool) data_get(config('features.defaults', []), $key, false);
    }
}

