<?php

app_require('classes/edge_gateway_manager.php');

use classes\edge_gateway_manager;

it('adds a gateway metadata update endpoint with label and primary assignment fields', function (): void {
    $routeSource = file_get_contents(app_path('routes/edgeGatewaysRoute.php'));

    expect($routeSource)->not->toBeFalse();
    expect($routeSource)->toContain("put('/edge-gateways/{id}'");
    expect($routeSource)->toContain("self::requireParameters(['label', 'is_primary']);");
    expect($routeSource)->toContain("self::requireType(self::getParameter('label'), self::TYPE_STRING());");
    expect($routeSource)->toContain("self::requireType(self::getParameter('is_primary'), self::TYPE_BOOL());");
    expect($routeSource)->toContain('updateGatewayMetadata(');
});

it('reassigns department primary gateways through dedicated manager helpers', function (): void {
    $source = file_get_contents(app_path('classes/edge_gateway_manager.php'));
    $reflection = new ReflectionClass(edge_gateway_manager::class);

    expect($source)->not->toBeFalse();
    expect($source)->toContain("\$this->setGatewayPrimaryState(\$gateway, true);");
    expect($source)->toContain("\$replacement = \$this->findAlternateGatewayForDepartment(");
    expect($source)->toContain("throw new Exception('Department must retain a primary gateway');");
    expect($reflection->hasMethod('updateGatewayMetadata'))->toBeTrue();
    expect($reflection->hasMethod('setGatewayPrimaryState'))->toBeTrue();
    expect($reflection->hasMethod('findAlternateGatewayForDepartment'))->toBeTrue();
});
