<?php

app_require('classes/module_usage_registry.php');
app_require('classes/module_usage_service.php');

use classes\module_usage_registry;
use classes\module_usage_service;

it('registers existing writable quota metrics with their current enforcement defaults', function (): void {
    $registry = new module_usage_registry();

    $motorApi = $registry->find('motorapi', 'lookup_calls');
    $fxRates = $registry->find('fxratesapi', 'rate_fetch_calls');
    $virkData = $registry->find('virkdata', 'company_search_calls');

    expect($motorApi)->not->toBeNull()
        ->and($motorApi['config_module'])->toBe('motorapi')
        ->and($motorApi['config_variable'])->toBe('daily_limit')
        ->and($motorApi['default_enforce_mode'])->toBe('block')
        ->and($motorApi['writable_limit'])->toBeTrue()
        ->and($fxRates)->not->toBeNull()
        ->and($fxRates['config_variable'])->toBe('daily_limit')
        ->and($fxRates['default_enforce_mode'])->toBe('block')
        ->and($virkData)->not->toBeNull()
        ->and($virkData['config_variable'])->toBe('monthly_limit')
        ->and($virkData['default_enforce_mode'])->toBe('observe');
});

it('normalizes provider usage payloads into module usage metrics without requiring a database', function (): void {
    $previousDb = $GLOBALS['db'] ?? null;
    unset($GLOBALS['db']);

    try {
        $metric = (new module_usage_service())->recordProviderSnapshotFromLegacyUsage('licenseplaterecognizer', [
            'calls_used' => 2250,
            'quota_calls' => 2500,
            'calls_remaining' => 250,
            'usage_percent' => 90.0,
            'version' => '1.54.0',
        ]);
    } finally {
        if ($previousDb !== null) {
            $GLOBALS['db'] = $previousDb;
        }
    }

    expect($metric)->not->toBeNull()
        ->and($metric['module_key'])->toBe('licenseplaterecognizer')
        ->and($metric['metric_key'])->toBe('plate_recognition_calls')
        ->and($metric['used'])->toBe(2250.0)
        ->and($metric['limit'])->toBe(2500.0)
        ->and($metric['remaining'])->toBe(250.0)
        ->and($metric['usage_percent'])->toBe(90.0)
        ->and($metric['status'])->toBe('near_limit')
        ->and($metric['version'])->toBe('1.54.0');
});

it('normalizes unavailable provider quota payloads into placeholder metrics without requiring a database', function (): void {
    $previousDb = $GLOBALS['db'] ?? null;
    unset($GLOBALS['db']);

    try {
        $metric = (new module_usage_service())->recordProviderSnapshotFromLegacyUsage('email', [
            'usage_available' => false,
            'status' => 'unknown',
            'calls_used' => null,
            'quota_calls' => null,
            'calls_remaining' => null,
            'usage_percent' => null,
            'unavailable_reason' => 'missing_limit',
            'detected_keys' => ['plan', 'usage.calls'],
        ]);
    } finally {
        if ($previousDb !== null) {
            $GLOBALS['db'] = $previousDb;
        }
    }

    expect($metric)->not->toBeNull()
        ->and($metric['module_key'])->toBe('email')
        ->and($metric['metric_key'])->toBe('mailersend_messages')
        ->and($metric['used'])->toBeNull()
        ->and($metric['limit'])->toBeNull()
        ->and($metric['usage_percent'])->toBeNull()
        ->and($metric['status'])->toBe('unknown')
        ->and($metric['usage_available'])->toBeFalse()
        ->and($metric['unavailable_reason'])->toBe('missing_limit')
        ->and($metric['detected_keys'])->toBe(['plan', 'usage.calls']);
});
