import 'package:built_value/serializer.dart';

/// A custom serializer for [DateTime] objects that are represented as
/// a simple date string "yyyy-MM-dd" in JSON.
class CustomDateTimeSerializer implements PrimitiveSerializer<DateTime> {
  @override
  final Iterable<Type> types = const [DateTime];

  @override
  final String wireName = 'DateTime';

  @override
  Object serialize(Serializers serializers, DateTime dateTime,
      {FullType specifiedType = FullType.unspecified}) {
    // On serialization, convert DateTime to a "yyyy-MM-dd" string.
    return dateTime.toIso8601String().substring(0, 10);
  }

  @override
  DateTime deserialize(Serializers serializers, Object serialized,
      {FullType specifiedType = FullType.unspecified}) {
    // On deserialization, parse the string to a DateTime object.
    // This handles formats like "2025-09-23".
    final parts = (serialized as String).split('-');
    final dateUtc = DateTime.utc(
      int.parse(parts[0]),
      int.parse(parts[1]),
      int.parse(parts[2]),
    );
    return dateUtc;
  }
}
