import 'package:flutter_test/flutter_test.dart';

import 'package:lanebow/models/public_event.dart';
import 'package:lanebow/models/public_participant.dart';

void main() {
  group('PublicEvent.fromJson', () {
    test('parses minimal and coerces types', () {
      final e = PublicEvent.fromJson({
        'id': 10,
        'title': 'Open',
        'gameCount': '5',
      });
      expect(e.id, '10');
      expect(e.title, 'Open');
      expect(e.gameCount, 5);
      expect(e.description, isNull);
    });

    test('parses dates and optionals', () {
      final e = PublicEvent.fromJson({
        'id': 'A1',
        'title': 'E',
        'startDate': '2024-01-01T10:00:00Z',
        'registrationDeadline': '2024-01-02T10:00:00Z',
        'participantFee': '10000',
        'maxParticipants': '30',
      });
      expect(e.id, 'A1');
      expect(e.startDate, isNotNull);
      expect(e.registrationDeadline, isNotNull);
      expect(e.participantFee, 10000);
      expect(e.maxParticipants, 30);
    });
  });

  group('PublicParticipant.fromJson', () {
    test('handles numeric scores and fills length to gameCount', () {
      final p = PublicParticipant.fromJson({
        'participantId': 7,
        'name': 'Kim',
        'scores': [120, 130],
        'handicap': 10,
      }, gameCount: 3);
      expect(p.participantId, '7');
      expect(p.name, 'Kim');
      expect(p.rawScores, [120, 130, null]);
      expect(p.handicaps, [0, 0, 0]);
    });

    test('handles object scores with handicap', () {
      final p = PublicParticipant.fromJson({
        'participantId': '9',
        'name': 'Lee',
        'scores': [
          {'score': 200, 'handicap': 0},
          {'score': '150', 'handicap': '5'},
          null
        ],
      }, gameCount: 3);
      expect(p.rawScores, [200, 150, null]);
      expect(p.handicaps, [0, 5, 0]);
    });

    test('coerces fields and defaults', () {
      final p = PublicParticipant.fromJson({
        'id': '11',
        'name': null,
        'average': '192.5',
        'memberType': 'guest',
        'status': 'pending',
        'paymentStatus': 'unpaid',
        'scores': [],
      }, gameCount: 2);
      expect(p.participantId, '11');
      expect(p.name, '이름없음');
      expect(p.average, 192.5);
      expect(p.isGuest, false); // isGuest는 명시 true일 때만 처리
      expect(p.rawScores, [null, null]);
      expect(p.handicaps, [0, 0]);
    });
  });
}
