import 'package:json_annotation/json_annotation.dart';

part 'time_validity.g.dart';


@JsonSerializable()
class TimeValidity {
  final DateTime? start;
  final DateTime? expiry;

  const TimeValidity({this.start, this.expiry});

  bool get isExpired => expiry?.isBefore(DateTime.now()) ?? false;
  bool get isNotStarted => start?.isAfter(DateTime.now()) ?? false;
  bool get isActive => (!isNotStarted) && (!isExpired);

  TimeValidity copyWith({
    DateTime? Function()? start, 
    DateTime? Function()? expiry, 
  }) => TimeValidity(
    start: start == null ? this.start : start(),
    expiry: expiry == null ? this.expiry : expiry(),
  );

  factory TimeValidity.fromJson(Map<String, dynamic> json) => _$TimeValidityFromJson(json);
  Map<String, dynamic> toJson() => _$TimeValidityToJson(this);
}
