import 'package:flutter_test/flutter_test.dart';
import 'package:alt_client/src/contracts/alt_contracts.dart';
import 'package:alt_client/src/generated/alt/v1/common.pb.dart';
import 'package:alt_client/src/generated/alt/v1/market.pb.dart';
import 'package:alt_client/src/generated/alt/v1/backtest.pb.dart';
import 'package:alt_client/src/generated/alt/v1/paper_trading.pb.dart';
import 'package:alt_client/src/generated/alt/v1/live_trading.pb.dart';

void main() {
  group('ALT contracts parser map helper tests', () {
    test(
      'altParserMap contains all expected parsers and passes round-trip parsing',
      () {
        final parsers = altParserMap();

        final expectedMessages = [
          HelloRequest(),
          HelloResponse(),
          ListInstrumentsRequest(),
          ListInstrumentsResponse(),
          ListBarsRequest(),
          ListBarsResponse(),
          StartBacktestRequest(),
          StartBacktestResponse(),
          GetBacktestRunRequest(),
          GetBacktestRunResponse(),
          GetBacktestResultRequest(),
          GetBacktestResultResponse(),
          BacktestResult(),
          ListBacktestRunsRequest(),
          ListBacktestRunsResponse(),
          GetBacktestRunDetailRequest(),
          GetBacktestRunDetailResponse(),
          CompareBacktestRunsRequest(),
          CompareBacktestRunsResponse(),
          StartPaperTradingRequest(),
          StartPaperTradingResponse(),
          GetPaperTradingStateRequest(),
          GetPaperTradingStateResponse(),
          PaperTradingState(),
          GetLiveBrokerCapabilitiesRequest(),
          GetLiveBrokerCapabilitiesResponse(),
          LiveBrokerCapability(),
        ];

        expect(parsers.length, equals(27));

        for (final msg in expectedMessages) {
          final qualifiedName = msg.info_.qualifiedMessageName;
          expect(
            parsers.containsKey(qualifiedName),
            isTrue,
            reason: 'Map should contain parser for $qualifiedName',
          );

          final parser = parsers[qualifiedName]!;
          final serialized = msg.writeToBuffer();
          final parsed = parser(serialized);

          expect(parsed.info_.qualifiedMessageName, equals(qualifiedName));
          expect(parsed.writeToBuffer(), equals(serialized));
        }
      },
    );
  });
}
