class DailyChallenge {
  final String wordle;
  final bool attempted;
  final bool solved;
  final int? attempts;
  final DateTime? solveTime;

  DailyChallenge({
    required this.wordle,
    required this.attempted,
    required this.solved,
    this.attempts,
    this.solveTime,
  });

  factory DailyChallenge.fromJson(Map<String, dynamic> json) {
    return DailyChallenge(
      wordle: json['wordle'] ?? '',
      attempted: json['attempted'] ?? false,
      solved: json['solved'] ?? false,
      attempts: json['attempts'],
      solveTime: json['solveTime'] != null 
          ? DateTime.parse(json['solveTime']) 
          : null,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'wordle': wordle,
      'attempted': attempted,
      'solved': solved,
      'attempts': attempts,
      'solveTime': solveTime?.toIso8601String(),
    };
  }
}

class DailyChallengeResponse {
  final bool status;
  final String? message;
  final DailyChallenge? data;

  DailyChallengeResponse({
    required this.status,
    this.message,
    this.data,
  });

  factory DailyChallengeResponse.fromJson(Map<String, dynamic> json) {
    return DailyChallengeResponse(
      status: json['status'] ?? false,
      message: json['message'],
      data: json['data'] != null 
          ? DailyChallenge.fromJson(json['data']) 
          : null,
    );
  }
}

class PuzzleAttempt {
  final int attempts;
  final bool solved;

  PuzzleAttempt({
    required this.attempts,
    required this.solved,
  });

  Map<String, dynamic> toJson() {
    return {
      'attempts': attempts,
      'solved': solved,
    };
  }
}

class GameState {
  final String targetWord;
  final List<String> guesses;
  final int currentAttempt;
  final bool isGameComplete;
  final bool isWon;
  final DateTime? solveTime;
  final String gameDate;

  GameState({
    required this.targetWord,
    required this.guesses,
    required this.currentAttempt,
    required this.isGameComplete,
    required this.isWon,
    this.solveTime,
    required this.gameDate,
  });

  factory GameState.fromJson(Map<String, dynamic> json) {
    return GameState(
      targetWord: json['targetWord'] ?? '',
      guesses: List<String>.from(json['guesses'] ?? []),
      currentAttempt: json['currentAttempt'] ?? 0,
      isGameComplete: json['isGameComplete'] ?? false,
      isWon: json['isWon'] ?? false,
      solveTime: json['solveTime'] != null 
          ? DateTime.parse(json['solveTime']) 
          : null,
      gameDate: json['gameDate'] ?? '',
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'targetWord': targetWord,
      'guesses': guesses,
      'currentAttempt': currentAttempt,
      'isGameComplete': isGameComplete,
      'isWon': isWon,
      'solveTime': solveTime?.toIso8601String(),
      'gameDate': gameDate,
    };
  }

  GameState copyWith({
    String? targetWord,
    List<String>? guesses,
    int? currentAttempt,
    bool? isGameComplete,
    bool? isWon,
    DateTime? solveTime,
    String? gameDate,
  }) {
    return GameState(
      targetWord: targetWord ?? this.targetWord,
      guesses: guesses ?? this.guesses,
      currentAttempt: currentAttempt ?? this.currentAttempt,
      isGameComplete: isGameComplete ?? this.isGameComplete,
      isWon: isWon ?? this.isWon,
      solveTime: solveTime ?? this.solveTime,
      gameDate: gameDate ?? this.gameDate,
    );
  }
}

class LeaderboardEntry {
  final String displayName;
  final String username;
  final String? avatar;
  final int attempts;
  final bool solved;
  final DateTime submittedAt;

  LeaderboardEntry({
    required this.displayName,
    required this.username,
    this.avatar,
    required this.attempts,
    required this.solved,
    required this.submittedAt,
  });

  factory LeaderboardEntry.fromJson(Map<String, dynamic> json) {
    return LeaderboardEntry(
      displayName: json['displayName'] ?? '',
      username: json['username'] ?? '',
      avatar: json['avatar'],
      attempts: json['attempts'] ?? 0,
      solved: json['solved'] ?? false,
      submittedAt: DateTime.parse(json['submittedAt']),
    );
  }
}

class LeaderboardResponse {
  final bool status;
  final String? message;
  final List<LeaderboardEntry>? data;

  LeaderboardResponse({
    required this.status,
    this.message,
    this.data,
  });

  factory LeaderboardResponse.fromJson(Map<String, dynamic> json) {
    return LeaderboardResponse(
      status: json['status'] ?? false,
      message: json['message'],
      data: json['data'] != null 
          ? (json['data'] as List).map((e) => LeaderboardEntry.fromJson(e)).toList()
          : null,
    );
  }
}