<?php

it('defines the v2 edge gateway schema bootstrap tables', function (): void {
    $bootstrapContent = file_get_contents(app_path('classes/edge_gateway_schema_bootstrap.php'));

    expect($bootstrapContent)->not->toBeFalse();
    expect($bootstrapContent)->toContain('CREATE TABLE IF NOT EXISTS edge_gateways');
    expect($bootstrapContent)->toContain('CREATE TABLE IF NOT EXISTS edge_gateway_claim_tokens');
    expect($bootstrapContent)->toContain('CREATE TABLE IF NOT EXISTS edge_gateway_device_inventory');
    expect($bootstrapContent)->toContain('CREATE TABLE IF NOT EXISTS edge_gateway_relay_bindings');
    expect($bootstrapContent)->toContain('CREATE TABLE IF NOT EXISTS edge_gateway_command_jobs');
    expect($bootstrapContent)->toContain('CREATE TABLE IF NOT EXISTS edge_gateway_expected_relay_states');
    expect($bootstrapContent)->toContain('CREATE TABLE IF NOT EXISTS edge_gateway_operations');
    expect($bootstrapContent)->toContain('CREATE TABLE IF NOT EXISTS edge_gateway_operation_events');
    expect($bootstrapContent)->toContain('CREATE TABLE IF NOT EXISTS edge_gateway_audit_logs');
    expect($bootstrapContent)->toContain('CREATE TABLE IF NOT EXISTS edge_gateway_log_entries');
    expect($bootstrapContent)->toContain('CREATE TABLE IF NOT EXISTS edge_gateway_shell_sessions');
    expect($bootstrapContent)->not->toContain('edge_gateway_shell_action_jobs');
    expect($bootstrapContent)->not->toContain('edge_gateway_shell_events');
});

it('stores operation metadata and event timelines for management workflows', function (): void {
    $bootstrapContent = file_get_contents(app_path('classes/edge_gateway_schema_bootstrap.php'));

    expect($bootstrapContent)->toContain('command_type VARCHAR(64) NOT NULL');
    expect($bootstrapContent)->toContain('delivery_json JSON NULL');
    expect($bootstrapContent)->toContain('expected_state TINYINT(1) NOT NULL DEFAULT 0');
    expect($bootstrapContent)->toContain('UNIQUE KEY uniq_edge_gateway_expected_relay_state (gateway_id, lane_id, role)');
    expect($bootstrapContent)->toContain("fallback_mode VARCHAR(32) NOT NULL DEFAULT 'PREFER_LOCAL'");
    expect($bootstrapContent)->toContain('type VARCHAR(32) NOT NULL');
    expect($bootstrapContent)->toContain("operation_type VARCHAR(32) NOT NULL DEFAULT 'DISCOVERY'");
    expect($bootstrapContent)->toContain('agent_instance_id VARCHAR(128) NULL');
    expect($bootstrapContent)->toContain('lease_expires_at DATETIME NULL');
    expect($bootstrapContent)->toContain('last_progress_at DATETIME NULL');
    expect($bootstrapContent)->toContain('attempt_count INT NOT NULL DEFAULT 0');
    expect($bootstrapContent)->toContain("ensureColumn('edge_gateway_operations', 'type', \"VARCHAR(32) NOT NULL DEFAULT 'DISCOVERY' AFTER gateway_id\")");
    expect($bootstrapContent)->toContain("ensureColumn('edge_gateway_operations', 'operation_type', \"VARCHAR(32) NOT NULL DEFAULT 'DISCOVERY' AFTER type\")");
    expect($bootstrapContent)->toContain("ensureColumn('edge_gateway_operations', 'summary_json', 'JSON NULL AFTER request_json')");
    expect($bootstrapContent)->toContain("ensureColumn('edge_gateway_operations', 'agent_instance_id', 'VARCHAR(128) NULL AFTER correlation_id')");
    expect($bootstrapContent)->toContain("ensureColumn('edge_gateway_operations', 'lease_expires_at', 'DATETIME NULL AFTER agent_instance_id')");
    expect($bootstrapContent)->toContain("ensureColumn('edge_gateway_operations', 'last_progress_at', 'DATETIME NULL AFTER lease_expires_at')");
    expect($bootstrapContent)->toContain("ensureColumn('edge_gateway_operations', 'attempt_count', 'INT NOT NULL DEFAULT 0 AFTER last_progress_at')");
    expect($bootstrapContent)->toContain('private static function syncOperationTypeColumns(): void');
    expect($bootstrapContent)->toContain('SET type = operation_type');
    expect($bootstrapContent)->toContain('private static function databaseConfigFromEnvironment(): ?array');
    expect($bootstrapContent)->toContain("databaseEnvValue('HOST', \$target)");
    expect($bootstrapContent)->toContain('private static function connectPdo(array $config): \PDO');
    expect($bootstrapContent)->toContain("ensureColumn('edge_gateway_operation_events', 'context_json', 'JSON NULL AFTER message')");
    expect($bootstrapContent)->toContain("ensureColumn('edge_gateway_audit_logs', 'context_json', 'JSON NULL AFTER severity')");
    expect($bootstrapContent)->toContain("ensureColumn('edge_gateway_log_entries', 'context_json', 'JSON NULL AFTER message')");
    expect($bootstrapContent)->toContain('summary_json JSON NULL');
    expect($bootstrapContent)->toContain('context_json JSON NULL');
    expect($bootstrapContent)->toContain('session_token_hash CHAR(64) NOT NULL');
    expect($bootstrapContent)->toContain('terminal_rows INT NULL');
    expect($bootstrapContent)->toContain("renameColumnIfPresent('edge_gateway_shell_sessions', 'rows', 'terminal_rows', 'INT NULL', 'cols')");
});
