import 'package:flutter/material.dart';

class InvitationUtils {
  // Centralized tag configuration
  static const List<Map<String, dynamic>> availableTags = [
    {"id": 1, "name": "Coffee", "color_hex": "#8B4513", "icon_name": "coffee"},
    {
      "id": 2,
      "name": "Lunch",
      "color_hex": "#FF6347",
      "icon_name": "restaurant",
    },
    {
      "id": 3,
      "name": "Exercise",
      "color_hex": "#32CD32",
      "icon_name": "fitness_center",
    },
    {
      "id": 4,
      "name": "Sports",
      "color_hex": "#FF4500",
      "icon_name": "sports_soccer",
    },
    {
      "id": 5,
      "name": "Gaming",
      "color_hex": "#9932CC",
      "icon_name": "sports_esports",
    },
    {
      "id": 6,
      "name": "Learning/Networking",
      "color_hex": "#4169E1",
      "icon_name": "school",
    },
    {"id": 7, "name": "Other", "color_hex": "#708090", "icon_name": "category"},
  ];

  static IconData getIconFromName(String iconName) {
    switch (iconName) {
      // Primary supported icons (from availableTags)
      case 'coffee':
        return Icons.coffee;
      case 'restaurant':
        return Icons.restaurant;
      case 'fitness_center':
        return Icons.fitness_center;
      case 'sports_soccer':
        return Icons.sports_soccer;
      case 'sports_esports':
        return Icons.sports_esports;
      case 'school':
        return Icons.school;
      case 'category':
        return Icons.category;
      default:
        return Icons.category;
    }
  }

  static Color getColorFromHex(String hexColor) {
    String cleanHex = hexColor.replaceAll('#', '');
    if (cleanHex.length == 6) {
      cleanHex = 'FF' + cleanHex;
    }
    return Color(int.parse(cleanHex, radix: 16));
  }

  static String getRelativeTime(DateTime dateTime) {
    final now = DateTime.now();
    final difference = now.difference(dateTime);

    if (difference.inMinutes < 1) {
      return 'just now';
    } else if (difference.inMinutes < 60) {
      return '${difference.inMinutes}m ago';
    } else if (difference.inHours < 24) {
      return '${difference.inHours}h ago';
    } else if (difference.inDays == 1) {
      return 'yesterday';
    } else if (difference.inDays < 7) {
      return '${difference.inDays}d ago';
    } else {
      return '${dateTime.day}/${dateTime.month}/${dateTime.year}';
    }
  }

  static String getRelativeDateTime(DateTime dateTime) {
    final now = DateTime.now();
    final today = DateTime(now.year, now.month, now.day);
    final tomorrow = today.add(Duration(days: 1));
    final eventDate = DateTime(dateTime.year, dateTime.month, dateTime.day);

    String timeString = _formatTime(dateTime);

    if (eventDate == today) {
      return 'today at $timeString';
    } else if (eventDate == tomorrow) {
      return 'tomorrow at $timeString';
    } else if (eventDate.isAfter(today) &&
        eventDate.isBefore(today.add(Duration(days: 7)))) {
      List<String> weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
      return '${weekdays[eventDate.weekday - 1]} at $timeString';
    } else {
      return '${eventDate.day}/${eventDate.month} at $timeString';
    }
  }

  static String _formatTime(DateTime dateTime) {
    int hour = dateTime.hour;
    String period = hour >= 12 ? 'PM' : 'AM';
    hour = hour > 12 ? hour - 12 : (hour == 0 ? 12 : hour);
    String minute = dateTime.minute.toString().padLeft(2, '0');
    return '$hour:$minute $period';
  }

  static String truncateDescription(String? description, {int maxLength = 80}) {
    if (description == null || description.isEmpty) return '';
    if (description.length <= maxLength) return description;
    return '${description.substring(0, maxLength)}...';
  }

  static String getParticipantsStatus(int current, int max) {
    return '$current/$max';
  }

  static Color getParticipantsStatusColor(int current, int max) {
    int needed = max - current;
    if (needed <= 2 && needed > 0) {
      return Colors.orange;
    } else if (current == max) {
      return Colors.green;
    } else {
      return Colors.blue;
    }
  }
}
