import 'package:lanebow/models/event_model.dart';
import 'package:lanebow/models/participant_model.dart';
import 'package:lanebow/models/score_model.dart';
import 'package:lanebow/models/team_model.dart';
import 'package:lanebow/models/member_model.dart';

/// 공용 더미 테스트 데이터 유틸리티
/// - 테스트에서 반복되는 더미 생성 로직을 한 곳에 모아 재사용합니다.
/// - 각 메서드는 합리적인 기본값을 제공하며, 필요 시 매개변수로 오버라이드할 수 있습니다.
class DummyTestData {
  /// 더미 이벤트 생성
  static Event event({
    String id = '1',
    String clubId = '1',
    String title = '테스트 이벤트',
    String description = '테스트 설명',
    String type = '팀전',
    String status = '예정',
    DateTime? startDate,
    DateTime? endDate,
    String location = '테스트 볼링장',
    int maxParticipants = 20,
    int gameCount = 3,
    double participantFee = 10000.0,
    DateTime? registrationDeadline,
    String publicHash = 'test123',
    String accessPassword = 'pass123',
    bool isActive = true,
  }) {
    final now = DateTime.now();
    return Event(
      id: id,
      clubId: clubId,
      title: title,
      description: description,
      type: type,
      status: status,
      startDate: startDate ?? now,
      endDate: endDate ?? now.add(const Duration(hours: 3)),
      location: location,
      maxParticipants: maxParticipants,
      gameCount: gameCount,
      participantFee: participantFee,
      registrationDeadline: registrationDeadline ?? now.add(const Duration(days: 1)),
      publicHash: publicHash,
      accessPassword: accessPassword,
      isActive: isActive,
    );
  }

  /// 더미 멤버 생성
  static Member member({
    required String id,
    String clubId = '1',
    String name = '테스트 회원',
    DateTime? birthDate,
    String userId = 'test_user',
    String email = 'test@example.com',
    bool isActive = true,
  }) {
    return Member(
      id: id,
      clubId: clubId,
      name: name,
      birthDate: birthDate ?? DateTime(1990, 1, 1),
      userId: userId,
      email: email,
      isActive: isActive,
    );
  }

  /// 더미 참가자 목록 생성
  static List<Participant> participants({
    required String eventId,
    int count = 4,
    List<String>? memberIds,
    String status = 'confirmed',
  }) {
    final ids = memberIds ?? List.generate(count, (i) => '${i + 1}');
    return List.generate(ids.length, (index) {
      final mid = ids[index];
      return Participant(
        id: '${index + 1}',
        eventId: eventId,
        memberId: mid,
        name: '참가자 ${index + 1}',
        status: status,
      );
    });
  }

  /// 더미 점수 목록 생성
  /// 기본 패턴:
  /// - 첫 번째 참가자: 총점 100 (모든 프레임 10)
  /// - 두 번째/세 번째: 총점 90, 두 번째는 handicap 10
  /// - 네 번째: 총점 80, handicap 20
  static List<Score> scoresForParticipants({
    required String eventId,
    required String clubId,
    required List<Participant> participants,
  }) {
    final now = DateTime.now();
    return participants.asMap().entries.map((entry) {
      final idx = entry.key;
      final p = entry.value;
      if (idx == 0) {
        return Score(
          id: '1',
          memberId: p.memberId,
          eventId: eventId,
          clubId: clubId,
          frames: List.filled(10, 10),
          totalScore: 100,
          handicap: 0,
          date: now,
          notes: '',
          participantName: p.name,
        );
      } else if (idx == 1) {
        return Score(
          id: '2',
          memberId: p.memberId,
          eventId: eventId,
          clubId: clubId,
          frames: List.filled(10, 9),
          totalScore: 90,
          handicap: 10,
          date: now,
          notes: '',
          participantName: p.name,
        );
      } else if (idx == 2) {
        return Score(
          id: '3',
          memberId: p.memberId,
          eventId: eventId,
          clubId: clubId,
          frames: List.filled(10, 9),
          totalScore: 90,
          handicap: 0,
          date: now,
          notes: '',
          participantName: p.name,
        );
      } else {
        return Score(
          id: '${idx + 1}',
          memberId: p.memberId,
          eventId: eventId,
          clubId: clubId,
          frames: List.filled(10, 8),
          totalScore: 80,
          handicap: 20,
          date: now,
          notes: '',
          participantName: p.name,
        );
      }
    }).toList();
  }

  /// 더미 팀 목록 생성: 참가자를 teamSize로 균등 분배
  static List<Team> teams({
    required String eventId,
    required List<Participant> participants,
    int teamSize = 3,
  }) {
    final List<Team> teams = [];
    int teamIndex = 0;

    for (int i = 0; i < participants.length; i += teamSize) {
      final chunk = participants
          .skip(i)
          .take(teamSize)
          .map((p) => p.memberId)
          .toList();
      teams.add(Team(
        id: '${teamIndex + 1}',
        eventId: eventId,
        name: '팀 ${teamIndex + 1}',
        memberIds: chunk,
      ));
      teamIndex++;
    }
    return teams;
  }
}
