part of 'extensions.dart';

extension DateHelpers on DateTime {
  String nowString() {
    return DateTime.now().toUtc().toIso8601String();
  }

  bool isToday() {
    final now = DateTime.now();
    return _isSameDay(this, now);
  }

  bool isYesterday() {
    final yesterday = DateTime.now().subtract(const Duration(days: 1));
    return _isSameDay(this, yesterday);
  }

  bool isTomorrow() {
    final tomorrow = DateTime.now().add(const Duration(days: 1));
    return _isSameDay(this, tomorrow);
  }

  bool isMorning() => hour > 3 && hour <= 12;
  bool isAfternoon() => hour > 12 && hour <= 18;
  bool isEvening() => (hour >= 0 && hour <= 3) || (hour > 18 && hour <= 23);

  String getTimeOfDay() {
    if (isMorning()) return 'morning';
    if (isAfternoon()) return 'afternoon';
    return 'evening';
  }

  DateTime findFirstDateOfTheWeek() {
    return subtract(Duration(days: weekday - 1));
  }

  DateTime findLastDateOfTheWeek() {
    return add(Duration(days: DateTime.daysPerWeek - weekday));
  }

  String formatSeconds(int value) {
    final hours = (value ~/ 3600).toString().padLeft(2, '0');
    final minutes = ((value % 3600) ~/ 60).toString().padLeft(2, '0');
    final seconds = (value % 60).toString().padLeft(2, '0');
    return "$hours:$minutes:$seconds";
  }

  String formatMinutes(int value, {bool allowZero = false}) {
    final hours = (value ~/ 3600).toString().padLeft(2, '0');
    var minutes = ((value % 3600) ~/ 60).toString().padLeft(2, '0');
    if (minutes == '00' && !allowZero) {
      minutes = '01';
    }
    return "$hours:$minutes";
  }

  // Helper method to check if two dates are on the same day
  bool _isSameDay(DateTime date1, DateTime date2) {
    return date1.day == date2.day &&
        date1.month == date2.month &&
        date1.year == date2.year;
  }

  DateTime firstDayOfYear() {
    return DateTime(year);
  }

  DateTime lastDayOfYear() {
    return DateTime(year + 1, 1, 0);
  }
}
