import 'package:flutter_test/flutter_test.dart';
import 'package:iop_client/gen/proto/iop/control.pb.dart';
import 'package:iop_client/gen/proto/iop/runtime.pb.dart';
import 'package:iop_client/gen/proto/iop/node.pb.dart';
import 'package:iop_client/gen/proto/iop/job.pb.dart';
import 'package:protobuf/well_known_types/google/protobuf/struct.pb.dart';

void main() {
  test('Generated proto compile guard and field verification', () {
    // Verify control.pb.dart
    final helloReq = ClientHelloRequest()..clientId = 'client-test';
    expect(helloReq.clientId, equals('client-test'));

    // Verify runtime.pb.dart with google.protobuf.Struct fields
    final runReq = RunRequest()
      ..runId = 'run-999'
      ..policy = Struct()
      ..input = Struct();

    expect(runReq.runId, equals('run-999'));
    expect(runReq.policy, isNotNull);
    expect(runReq.input, isNotNull);

    // Verify the protocol-profile and selected operation fields generated from
    // runtime.proto remain available to Dart consumers.
    final protocolProfile = ConcreteProtocolProfile()
      ..id = 'openai'
      ..driver = 'openai_chat'
      ..baseUrl = 'https://api.openai.com/v1';
    final adapterConfig = OpenAICompatAdapterConfig()
      ..protocolProfile = protocolProfile;
    expect(adapterConfig.protocolProfile.id, equals('openai'));
    expect(adapterConfig.protocolProfile.driver, equals('openai_chat'));

    final tunnelReq = ProviderTunnelRequest()
      ..operation = 'chat_completions';
    expect(tunnelReq.operation, equals('chat_completions'));

    // Verify runtime.pb.dart node startup contract (active Edge-Node registration).
    final registerReq = RegisterRequest()..token = 'node-token';
    expect(registerReq.token, equals('node-token'));

    final registerRes = RegisterResponse()
      ..accepted = true
      ..nodeId = 'node-1'
      ..alias = 'alpha';
    expect(registerRes.accepted, isTrue);
    expect(registerRes.nodeId, equals('node-1'));
    expect(registerRes.alias, equals('alpha'));

    // Verify node.pb.dart legacy tombstones carry no active fields. The
    // reserved messages still exist but must serialize empty, so reintroducing
    // a legacy direct-registration field would break this guard.
    expect(NodeInfo().writeToBuffer(), isEmpty);
    expect(NodeRegisterRequest().writeToBuffer(), isEmpty);
    expect(NodeRegisterResponse().writeToBuffer(), isEmpty);
    expect(NodeStatus.values.length, equals(1));
    expect(NodeStatus.NODE_STATUS_UNSPECIFIED.value, equals(0));

    // Verify job.pb.dart
    final job = Job()
      ..jobId = 'job-1'
      ..runId = 'run-1'
      ..nodeId = 'node-1'
      ..adapter = 'mock'
      ..target = 'fake-cli';

    expect(job.jobId, equals('job-1'));
    expect(job.runId, equals('run-1'));
    expect(job.adapter, equals('mock'));
    expect(job.target, equals('fake-cli'));
  });
}
