import 'package:intl/intl.dart';

class DateTimeUtils {
  /// Default date format
  static const String defaultFormat = 'dd MMM yyyy';

  /// Converts a UTC datetime string to local time and formats it.
  static String convertUtcToLocal(String utcTimeString,
      {String format = 'dd-MM-yyyy'}) {
    try {
      final parsed = DateTime.parse(utcTimeString);
      final utcDateTime = DateTime.utc(
        parsed.year,
        parsed.month,
        parsed.day,
        parsed.hour,
        parsed.minute,
        parsed.second,
        parsed.millisecond,
        parsed.microsecond,
      );

      final localDateTime = utcDateTime.toLocal();
      return _formatDateTime(localDateTime, format: format);
    } catch (e) {
      return 'Invalid Date';
    }
  }

  /// Public utility for formatting any DateTime.
  static String formatDate(DateTime date, String format) {
    try {
      return DateFormat(format).format(date);
    } catch (e) {
      return 'Invalid Date';
    }
  }

  /// Parses a date string using the given format.
  static DateTime? parseDate(String dateString, String format) {
    try {
      return DateFormat(format).parse(dateString);
    } catch (e) {
      return null;
    }
  }

  /// Internal formatter with default format.
  static String _formatDateTime(DateTime dateTime,
      {String format = 'dd-MM-yyyy'}) {
    return DateFormat(format).format(dateTime);
  }
}
