import 'package:flutter_test/flutter_test.dart';
import 'package:dio/dio.dart';

import 'package:lanebow/services/event_service.dart';
import 'package:lanebow/services/api_service.dart';

class _RecordingInterceptor extends Interceptor {
  String? lastPath;
  Map<String, dynamic>? lastQuery;
  String? lastMethod;

  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    lastPath = options.path;
    lastQuery = options.queryParameters;
    lastMethod = options.method;

    // Return canned responses based on path
    if (options.path.endsWith('/scores')) {
      handler.resolve(Response(
        requestOptions: options,
        statusCode: 200,
        data: {
          'scores': [],
        },
      ));
      return;
    }
    handler.resolve(Response(requestOptions: options, statusCode: 200, data: {}));
  }
}

void main() {
  group('EventService.fetchEventScores', () {
    test('calls GET /api/club/events/:id/scores with clubId query', () async {
      // Arrange
      final api = ApiService();
      final dio = Dio(BaseOptions(baseUrl: ''));
      final rec = _RecordingInterceptor();
      dio.interceptors.add(rec);
      api.setDio(dio);

      final service = EventService.forTest(api);
      await service.initialize('dummy-token');
      service.setClubId('3');

      // Act
      await service.fetchEventScores('1');

      // Assert
      expect(rec.lastMethod, equals('GET'));
      expect(rec.lastPath, equals('/club/events/1/scores'));
      expect(rec.lastQuery, isNotNull);
      expect(rec.lastQuery!['clubId'], equals('3'));
    });
  });
}
