<?php

it('keeps the superuser replication read endpoint and retires mutation handlers', function (): void {
    $content = file_get_contents(app_path('routes/superuserReplicationRoute.php'));

    expect($content)->not->toBeFalse();
    expect($content)->toContain('/superuser/replication');
    expect($content)->toContain('/superuser/replication/databases');
    expect($content)->toContain('/superuser/replication/redis');
    expect($content)->toContain('/superuser/replication/minio');
    expect($content)->toContain('/superuser/replication/compose-template');
    expect($content)->toContain('/superuser/replication/test-credentials');
    expect($content)->toContain('/superuser/replication/{kind}/{id}/test');
    expect($content)->toContain('/superuser/replication/{kind}/{id}/provision');
    expect($content)->toContain('/superuser/replication/{kind}/{id}/promote');
    expect($content)->toContain("\$this->patch('/superuser/replication/{kind}/{id}'");
    expect($content)->toContain("requireClassicSuperuserPermission('superuser_replication_view')");
    expect($content)->toContain("requireClassicSuperuserPermission('superuser_replication_manage')");
    expect($content)->toContain("requireClassicSuperuserPermission('superuser_replication_promote')");
    expect($content)->toContain("requireClassicSuperuserPermission('superuser_replication_remove')");
    expect($content)->toContain('RETIRED_MANAGEMENT_MESSAGE');
    expect($content)->toContain('private function rejectRetiredManagement(): void');
    expect($content)->toContain("\$response->error(['message' => self::RETIRED_MANAGEMENT_MESSAGE], 410);");
    expect($content)->not->toContain('->addHost(');
    expect($content)->not->toContain('->generateComposeTemplate(');
    expect($content)->not->toContain('->testUnsavedCredentials(');
    expect($content)->not->toContain('->testHost(');
    expect($content)->not->toContain('->provisionHost(');
    expect($content)->not->toContain('->promoteHost(');
    expect($content)->not->toContain('->renameHost(');
    expect($content)->not->toContain('->deleteHost(');
});

it('documents retired replication mutations in openapi', function (): void {
    $content = file_get_contents(app_path('openapi.yaml'));

    expect($content)->toContain('/superuser/replication:');
    expect($content)->toContain('operationId: getSuperuserReplication');
    expect($content)->toContain('summary: Retired database replication host creation');
    expect($content)->toContain('operationId: generateSuperuserReplicationComposeTemplate');
    expect($content)->toContain('operationId: testSuperuserReplicationCredentials');
    expect($content)->toContain('operationId: addSuperuserMinioReplicationHost');
    expect($content)->toContain('operationId: renameSuperuserReplicationHost');
    expect(preg_match_all('/deprecated:\s+true/', $content))->toBeGreaterThanOrEqual(9);
    expect($content)->toContain("'410': { \$ref: '#/components/responses/Gone' }");
    expect($content)->toContain('Gone:');
    expect($content)->toContain('enum: [database, redis, minio]');
    expect($content)->toContain('space_headroom_percent');
    expect($content)->toContain('SuperuserReplicationStatus');
    expect($content)->toContain('SuperuserReplicationHostCreateRequest');
    expect($content)->toContain('SuperuserReplicationHostRenameRequest');
    expect($content)->toContain('SuperuserReplicationComposeTemplateRequest');
});

it('keeps superuser system status independent from replica health', function (): void {
    $content = file_get_contents(app_path('classes/superuser_system_status_service.php'));

    expect($content)->not->toBeFalse();
    expect($content)->not->toContain('dependencyReplication');
    expect($content)->not->toContain('replicationStatusFallback');
    expect($content)->not->toContain("\$dependencies['database']['replication']");
    expect($content)->not->toContain("\$dependencies['redis']['replication']");
    expect($content)->not->toContain("\$dependencies['minio']['replication']");
});

it('rejects subuser sessions before checking replication permissions', function (): void {
    $content = file_get_contents(app_path('routes/superuserReplicationRoute.php'));

    expect($content)->not->toBeFalse();
    expect($content)->toContain('private function requireClassicSuperuserPermission(string $permission): bool');
    expect($content)->toContain('get_subuser() !== false');
    expect($content)->toContain("Subuser sessions cannot manage replication.");
    expect($content)->toContain("\$response->error('Subuser sessions cannot manage replication.', 403);");
    expect($content)->toContain('return $this->requirePermission($permission);');
    expect(preg_match_all("/requireClassicSuperuserPermission\\('superuser_replication_/", $content))->toBe(11);
    expect($content)->not->toContain("requirePermission('superuser_replication_");
});
