import 'package:intl/intl.dart';

class DateTimeFormatter {
  DateTimeFormatter._();

  static String format(
    DateTime? value, {
    String pattern = 'yyyy-MM-dd HH:mm',
    String locale = 'zh_CN',
  }) {
    if (value == null) return '';
    return DateFormat(pattern, locale).format(value);
  }

  static String dayOrDate(String? isoString, {DateTime? now}) {
    if (isoString == null || isoString.trim().isEmpty) return '';

    final parsed = DateTime.tryParse(isoString)?.toLocal();
    if (parsed == null) return '';

    final current = (now ?? DateTime.now()).toLocal();
    final currentDate = DateTime(current.year, current.month, current.day);
    final parsedDate = DateTime(parsed.year, parsed.month, parsed.day);
    final diffDays = currentDate.difference(parsedDate).inDays;

    final time = DateFormat('HH:mm').format(parsed);
    return switch (diffDays) {
      0 => '今天 $time',
      1 => '昨天 $time',
      2 => '前天 $time',
      _ => DateFormat('yyyy-MM-dd').format(parsed),
    };
  }
}
