import 'package:json_annotation/json_annotation.dart';

part 'activity_event.g.dart';

@JsonSerializable()
class ActivityEvent {
  final ActivityEventType type;
  final String application;
  final String? metadata;
  final DateTime timestamp;

  ActivityEvent({
    required this.type,
    required this.application,
    this.metadata,
    required this.timestamp,
  });

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

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

enum ActivityEventType {
  @JsonValue('coding')
  coding,
  @JsonValue('focused_browsing')
  focusedBrowsing,
  @JsonValue('collaboration')
  collaboration,
  @JsonValue('meetings')
  meetings,
  @JsonValue('misc')
  misc,
  @JsonValue('uncategorized')
  uncategorized;

  String get id {
    switch (this) {
      case ActivityEventType.coding:
        return 'coding';
      case ActivityEventType.focusedBrowsing:
        return 'focused_browsing';
      case ActivityEventType.collaboration:
        return 'collaboration';
      case ActivityEventType.meetings:
        return 'meetings';
      case ActivityEventType.misc:
        return 'misc';
      case ActivityEventType.uncategorized:
        return 'uncategorized';
    }
  }

  String get displayName {
    switch (this) {
      case ActivityEventType.coding:
        return 'Coding';
      case ActivityEventType.focusedBrowsing:
        return 'Focused Browsing';
      case ActivityEventType.collaboration:
        return 'Collaboration';
      case ActivityEventType.meetings:
        return 'Meetings';
      case ActivityEventType.misc:
        return 'Miscellaneous';
      case ActivityEventType.uncategorized:
        return 'Uncategorized';
    }
  }

  String get icon {
    switch (this) {
      case ActivityEventType.coding:
        return '💻';
      case ActivityEventType.focusedBrowsing:
        return '🔍';
      case ActivityEventType.collaboration:
        return '🤝';
      case ActivityEventType.meetings:
        return '📅';
      case ActivityEventType.misc:
        return '📝';
      case ActivityEventType.uncategorized:
        return '❓';
    }
  }

  static ActivityEventType categorize({
    String? eventId,
    String? applicationId,
    String? applicationTitle,
  }) {
    // Direct type mappings
    if (eventId != null) {
      final matchType = ActivityEventType.values
          .where((e) => e.id == eventId)
          .firstOrNull;
      if (matchType != null) {
        return matchType;
      }
    }

    if (applicationId != null) {
      // Fallback to application-based categorization for 'other' type
      switch (applicationId.toLowerCase()) {
        case 'codium':
        case 'code':
        case 'vscode':
        case 'nvim':
        case 'vim':
        case 'emacs':
          return coding;
        case 'com.slack.slack':
        case 'slack':
        case 'discord':
        case 'teams':
        case 'mattermost':
          return collaboration;
        // terminals
        case 'com.mitchellh.ghostty':
        case 'alacritty':
        case 'kitty':
        case 'gnome-terminal':
        case 'konsole':
        case 'foot':
          switch (applicationTitle) {
            case String s when s.contains('git'):
            case String s when s == 'hx' || s == 'helix':
              return coding;
            case null:
            default:
              return misc;
          }
        case 'org.keepassxc.keepassxc':
        case 'keepassxc':
        case 'bitwarden':
        case '1password':
          return misc; // security tools now categorized as misc
        case 'firefox':
        case 'chrome':
        case 'safari':
        case 'edge':
          return focusedBrowsing;
        case 'zoom':
        case 'zoom.us':
        case 'us.zoom.xos':
          return meetings;
        default:
          return uncategorized;
      }
    }

    return uncategorized;
  }
}

enum ZoomStatus {
  @JsonValue('none')
  none,
  @JsonValue('zoom_focused')
  zoomFocused, // Zoom window active, no media
  @JsonValue('zoom_background')
  zoomBackground, // Zoom running, not focused, no media
  @JsonValue('background_meeting')
  backgroundMeeting, // Meeting active but Zoom not focused
  @JsonValue('active_meeting')
  activeMeeting, // Meeting active and Zoom focused
}
