import 'package:xp_nix/src/config/config_manager.dart';

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});

  Map<String, dynamic> toJson() => {
    'type': type.id,
    'application': application,
    'metadata': metadata,
    'timestamp': timestamp.millisecondsSinceEpoch,
  };
}

enum ActivityEventType {
  coding('coding', 'Coding', '💻'),
  focusedBrowsing('focused_browsing', 'Focused Browsing', '🔍'),
  collaboration('collaboration', 'Collaboration', '🤝'),
  meetings('meetings', 'Meetings', '📅'),
  misc('misc', 'Miscellaneous', '📝'),
  uncategorized('uncategorized', 'Uncategorized', '❓');

  const ActivityEventType(this.id, this.displayName, this.icon);

  final String id;
  final String displayName;
  final String icon;

  int get baseXp {
    return ConfigManager.instance.getBaseXP(id);
  }

  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 {
  none,
  zoomFocused, // Zoom window active, no media
  zoomBackground, // Zoom running, not focused, no media
  backgroundMeeting, // Meeting active but Zoom not focused
  activeMeeting, // Meeting active and Zoom focused
}
