import 'package:json_annotation/json_annotation.dart';

part 'dashboard_stats.g.dart';

@JsonSerializable()
class DashboardStats {
  final TodayStats today;
  final StreakStats streaks;
  @JsonKey(name: 'recent_activity')
  final List<RecentActivity> recentActivity;
  @JsonKey(fromJson: _timestampFromJson, toJson: _timestampToJson)
  final DateTime timestamp;

  DashboardStats({
    required this.today,
    required this.streaks,
    required this.recentActivity,
    required this.timestamp,
  });

  factory DashboardStats.fromJson(Map<String, dynamic> json) =>
      _$DashboardStatsFromJson(json);

  Map<String, dynamic> toJson() => _$DashboardStatsToJson(this);

  static DateTime _timestampFromJson(dynamic timestamp) {
    if (timestamp is int) {
      return DateTime.fromMillisecondsSinceEpoch(timestamp);
    } else if (timestamp is String) {
      return DateTime.parse(timestamp);
    }
    throw ArgumentError('Invalid timestamp format: $timestamp');
  }

  static int _timestampToJson(DateTime timestamp) {
    return timestamp.millisecondsSinceEpoch;
  }
}

@JsonSerializable()
class TodayStats {
  final int level;
  final int xp;
  @JsonKey(name: 'focus_time')
  final int focusTime;
  @JsonKey(name: 'meeting_time')
  final int meetingTime;
  @JsonKey(name: 'focus_sessions')
  final int focusSessions;

  TodayStats({
    required this.level,
    required this.xp,
    required this.focusTime,
    required this.meetingTime,
    required this.focusSessions,
  });

  factory TodayStats.fromJson(Map<String, dynamic> json) =>
      _$TodayStatsFromJson(json);

  Map<String, dynamic> toJson() => _$TodayStatsToJson(this);
}

@JsonSerializable()
class StreakStats {
  @JsonKey(name: 'current_streak')
  final int currentStreak;
  @JsonKey(name: 'longest_streak')
  final int longestStreak;

  StreakStats({
    required this.currentStreak,
    required this.longestStreak,
  });

  factory StreakStats.fromJson(Map<String, dynamic> json) =>
      _$StreakStatsFromJson(json);

  Map<String, dynamic> toJson() => _$StreakStatsToJson(this);
}

@JsonSerializable()
class RecentActivity {
  final String? type;
  final String? application;
  @JsonKey(fromJson: _timestampFromJson, toJson: _timestampToJson)
  final DateTime timestamp;
  @JsonKey(name: 'duration_seconds')
  final int durationSeconds;

  RecentActivity({
    this.type,
    this.application,
    required this.timestamp,
    required this.durationSeconds,
  });

  factory RecentActivity.fromJson(Map<String, dynamic> json) =>
      _$RecentActivityFromJson(json);

  Map<String, dynamic> toJson() => _$RecentActivityToJson(this);

  static DateTime _timestampFromJson(dynamic timestamp) {
    if (timestamp is int) {
      return DateTime.fromMillisecondsSinceEpoch(timestamp);
    } else if (timestamp is String) {
      return DateTime.parse(timestamp);
    }
    throw ArgumentError('Invalid timestamp format: $timestamp');
  }

  static int _timestampToJson(DateTime timestamp) {
    return timestamp.millisecondsSinceEpoch;
  }
}

@JsonSerializable()
class StatsHistory {
  final String date;
  final int level;
  final int xp;
  @JsonKey(name: 'focus_time')
  final int focusTime;
  @JsonKey(name: 'meeting_time')
  final int meetingTime;

  StatsHistory({
    required this.date,
    required this.level,
    required this.xp,
    required this.focusTime,
    required this.meetingTime,
  });

  factory StatsHistory.fromJson(Map<String, dynamic> json) =>
      _$StatsHistoryFromJson(json);

  Map<String, dynamic> toJson() => _$StatsHistoryToJson(this);
}
