import 'package:hive_ce/hive.dart';
import 'package:worker/core/constants/storage_constants.dart';
import 'package:worker/core/database/models/enums.dart';

part 'payment_reminder_model.g.dart';

@HiveType(typeId: HiveTypeIds.paymentReminderModel)
class PaymentReminderModel extends HiveObject {
  PaymentReminderModel({
    required this.reminderId,
    required this.invoiceId,
    required this.userId,
    required this.reminderType,
    required this.subject,
    required this.message,
    required this.isRead,
    required this.isSent,
    this.scheduledAt,
    this.sentAt,
    this.readAt,
  });

  @HiveField(0)
  final String reminderId;
  @HiveField(1)
  final String invoiceId;
  @HiveField(2)
  final String userId;
  @HiveField(3)
  final ReminderType reminderType;
  @HiveField(4)
  final String subject;
  @HiveField(5)
  final String message;
  @HiveField(6)
  final bool isRead;
  @HiveField(7)
  final bool isSent;
  @HiveField(8)
  final DateTime? scheduledAt;
  @HiveField(9)
  final DateTime? sentAt;
  @HiveField(10)
  final DateTime? readAt;

  factory PaymentReminderModel.fromJson(Map<String, dynamic> json) =>
      PaymentReminderModel(
        reminderId: json['reminder_id'] as String,
        invoiceId: json['invoice_id'] as String,
        userId: json['user_id'] as String,
        reminderType: ReminderType.values.firstWhere(
          (e) => e.name == json['reminder_type'],
        ),
        subject: json['subject'] as String,
        message: json['message'] as String,
        isRead: json['is_read'] as bool? ?? false,
        isSent: json['is_sent'] as bool? ?? false,
        scheduledAt: json['scheduled_at'] != null
            ? DateTime.parse(json['scheduled_at']?.toString() ?? '')
            : null,
        sentAt: json['sent_at'] != null
            ? DateTime.parse(json['sent_at']?.toString() ?? '')
            : null,
        readAt: json['read_at'] != null
            ? DateTime.parse(json['read_at']?.toString() ?? '')
            : null,
      );

  Map<String, dynamic> toJson() => {
    'reminder_id': reminderId,
    'invoice_id': invoiceId,
    'user_id': userId,
    'reminder_type': reminderType.name,
    'subject': subject,
    'message': message,
    'is_read': isRead,
    'is_sent': isSent,
    'scheduled_at': scheduledAt?.toIso8601String(),
    'sent_at': sentAt?.toIso8601String(),
    'read_at': readAt?.toIso8601String(),
  };
}
