<?php

use App\Services\Graph\GraphContractRegistry;

it('builds settings write method and path from the contract', function () {
    config()->set('graph_contracts.types.settingsCatalogPolicy', [
        'settings_write' => [
            'path_template' => 'deviceManagement/configurationPolicies/{id}/settings/{settingId}',
            'method' => 'PATCH',
        ],
    ]);

    $registry = app(GraphContractRegistry::class);

    expect($registry->settingsWriteMethod('settingsCatalogPolicy'))->toBe('PATCH');
    expect($registry->settingsWritePath('settingsCatalogPolicy', 'policy-1', 'setting-9'))
        ->toBe('deviceManagement/configurationPolicies/policy-1/settings/setting-9');
});

it('returns null when settings write path requires a setting id', function () {
    config()->set('graph_contracts.types.settingsCatalogPolicy', [
        'settings_write' => [
            'path_template' => 'deviceManagement/configurationPolicies/{id}/settings/{settingId}',
            'method' => 'PATCH',
        ],
    ]);

    $registry = app(GraphContractRegistry::class);

    expect($registry->settingsWritePath('settingsCatalogPolicy', 'policy-1'))->toBeNull();
});

it('defaults settings write body shape to collection', function () {
    config()->set('graph_contracts.types.settingsCatalogPolicy', [
        'settings_write' => [
            'path_template' => 'deviceManagement/configurationPolicies/{id}/settings',
            'method' => 'POST',
        ],
    ]);

    $registry = app(GraphContractRegistry::class);

    expect($registry->settingsWriteBodyShape('settingsCatalogPolicy'))->toBe('collection');
    expect($registry->settingsWriteFallbackBodyShape('settingsCatalogPolicy'))->toBeNull();
});

it('returns null when settings write contract is missing', function () {
    config()->set('graph_contracts.types.settingsCatalogPolicy', []);

    $registry = app(GraphContractRegistry::class);

    expect($registry->settingsWriteMethod('settingsCatalogPolicy'))->toBeNull();
    expect($registry->settingsWritePath('settingsCatalogPolicy', 'policy-1', 'setting-9'))->toBeNull();
});

it('returns fallback body shape when configured', function () {
    config()->set('graph_contracts.types.settingsCatalogPolicy', [
        'settings_write' => [
            'path_template' => 'deviceManagement/configurationPolicies/{id}/settings',
            'method' => 'POST',
            'body_shape' => 'collection',
            'fallback_body_shape' => 'wrapped',
        ],
    ]);

    $registry = app(GraphContractRegistry::class);

    expect($registry->settingsWriteBodyShape('settingsCatalogPolicy'))->toBe('collection');
    expect($registry->settingsWriteFallbackBodyShape('settingsCatalogPolicy'))->toBe('wrapped');
});
