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

part 'payment_line_model.g.dart';

@HiveType(typeId: HiveTypeIds.paymentLineModel)
class PaymentLineModel extends HiveObject {
  PaymentLineModel({
    required this.paymentLineId,
    required this.invoiceId,
    required this.paymentNumber,
    required this.paymentDate,
    required this.amount,
    required this.paymentMethod,
    this.bankName,
    this.bankAccount,
    this.referenceNumber,
    this.notes,
    required this.status,
    this.receiptUrl,
    this.erpnextPaymentEntry,
    required this.createdAt,
    this.processedAt,
  });

  @HiveField(0)
  final String paymentLineId;
  @HiveField(1)
  final String invoiceId;
  @HiveField(2)
  final String paymentNumber;
  @HiveField(3)
  final DateTime paymentDate;
  @HiveField(4)
  final double amount;
  @HiveField(5)
  final PaymentMethod paymentMethod;
  @HiveField(6)
  final String? bankName;
  @HiveField(7)
  final String? bankAccount;
  @HiveField(8)
  final String? referenceNumber;
  @HiveField(9)
  final String? notes;
  @HiveField(10)
  final PaymentStatus status;
  @HiveField(11)
  final String? receiptUrl;
  @HiveField(12)
  final String? erpnextPaymentEntry;
  @HiveField(13)
  final DateTime createdAt;
  @HiveField(14)
  final DateTime? processedAt;

  factory PaymentLineModel.fromJson(Map<String, dynamic> json) =>
      PaymentLineModel(
        paymentLineId: json['payment_line_id'] as String,
        invoiceId: json['invoice_id'] as String,
        paymentNumber: json['payment_number'] as String,
        paymentDate: DateTime.parse(json['payment_date']?.toString() ?? ''),
        amount: (json['amount'] as num).toDouble(),
        paymentMethod: PaymentMethod.values.firstWhere(
          (e) => e.name == json['payment_method'],
        ),
        bankName: json['bank_name'] as String?,
        bankAccount: json['bank_account'] as String?,
        referenceNumber: json['reference_number'] as String?,
        notes: json['notes'] as String?,
        status: PaymentStatus.values.firstWhere(
          (e) => e.name == json['status'],
        ),
        receiptUrl: json['receipt_url'] as String?,
        erpnextPaymentEntry: json['erpnext_payment_entry'] as String?,
        createdAt: DateTime.parse(json['created_at']?.toString() ?? ''),
        processedAt: json['processed_at'] != null
            ? DateTime.parse(json['processed_at']?.toString() ?? '')
            : null,
      );

  Map<String, dynamic> toJson() => {
    'payment_line_id': paymentLineId,
    'invoice_id': invoiceId,
    'payment_number': paymentNumber,
    'payment_date': paymentDate.toIso8601String(),
    'amount': amount,
    'payment_method': paymentMethod.name,
    'bank_name': bankName,
    'bank_account': bankAccount,
    'reference_number': referenceNumber,
    'notes': notes,
    'status': status.name,
    'receipt_url': receiptUrl,
    'erpnext_payment_entry': erpnextPaymentEntry,
    'created_at': createdAt.toIso8601String(),
    'processed_at': processedAt?.toIso8601String(),
  };
}
