import 'package:cashumit/models/receipt_item.dart';

class LocalReceipt {
  final String id;
  final List<ReceiptItem> items;
  final String? sourceAccountId;
  final String? sourceAccountName;
  final String? destinationAccountId;
  final String? destinationAccountName;
  final DateTime transactionDate;
  final String?
      transactionDescription; // Deskripsi transaksi untuk tampilan di daftar
  final String? fireflyTransactionId; // ID transaksi di FireFly III
  final String? fireflyTransactionUrl; // URL transaksi di FireFly III
  final bool isSubmitted;
  final String? submissionError;
  final DateTime? submittedAt;
  final DateTime createdAt;

  LocalReceipt({
    required this.id,
    required this.items,
    this.sourceAccountId,
    this.sourceAccountName,
    this.destinationAccountId,
    this.destinationAccountName,
    required this.transactionDate,
    this.transactionDescription,
    this.fireflyTransactionId,
    this.fireflyTransactionUrl,
    this.isSubmitted = false,
    this.submissionError,
    this.submittedAt,
    required this.createdAt,
  });

  double get total => items.fold(0.0, (sum, item) => sum + item.total);

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'items': items.map((item) => item.toJson()).toList(),
      'sourceAccountId': sourceAccountId,
      'sourceAccountName': sourceAccountName,
      'destinationAccountId': destinationAccountId,
      'destinationAccountName': destinationAccountName,
      'transactionDate': transactionDate.toIso8601String(),
      'transactionDescription': transactionDescription,
      'fireflyTransactionId': fireflyTransactionId,
      'fireflyTransactionUrl': fireflyTransactionUrl,
      'isSubmitted': isSubmitted,
      'submissionError': submissionError,
      'submittedAt': submittedAt?.toIso8601String(),
      'createdAt': createdAt.toIso8601String(),
    };
  }

  factory LocalReceipt.fromJson(Map<String, dynamic> json) {
    return LocalReceipt(
      id: json['id'],
      items: (json['items'] as List)
          .map((item) => ReceiptItem.fromJson(item))
          .toList(),
      sourceAccountId: json['sourceAccountId'],
      sourceAccountName: json['sourceAccountName'],
      destinationAccountId: json['destinationAccountId'],
      destinationAccountName: json['destinationAccountName'],
      transactionDate: DateTime.parse(json['transactionDate']),
      transactionDescription: json['transactionDescription'],
      fireflyTransactionId: json['fireflyTransactionId'],
      fireflyTransactionUrl: json['fireflyTransactionUrl'],
      isSubmitted: json['isSubmitted'] ?? false,
      submissionError: json['submissionError'],
      submittedAt: json['submittedAt'] != null
          ? DateTime.parse(json['submittedAt'])
          : null,
      createdAt: DateTime.parse(json['createdAt']),
    );
  }

  LocalReceipt copyWith({
    String? id,
    List<ReceiptItem>? items,
    String? sourceAccountId,
    String? sourceAccountName,
    String? destinationAccountId,
    String? destinationAccountName,
    DateTime? transactionDate,
    String? transactionDescription,
    String? fireflyTransactionId,
    String? fireflyTransactionUrl,
    bool? isSubmitted,
    String? submissionError,
    DateTime? submittedAt,
    DateTime? createdAt,
  }) {
    return LocalReceipt(
      id: id ?? this.id,
      items: items ?? this.items,
      sourceAccountId: sourceAccountId ?? this.sourceAccountId,
      sourceAccountName: sourceAccountName ?? this.sourceAccountName,
      destinationAccountId: destinationAccountId ?? this.destinationAccountId,
      destinationAccountName:
          destinationAccountName ?? this.destinationAccountName,
      transactionDate: transactionDate ?? this.transactionDate,
      transactionDescription:
          transactionDescription ?? this.transactionDescription,
      fireflyTransactionId: fireflyTransactionId ?? this.fireflyTransactionId,
      fireflyTransactionUrl:
          fireflyTransactionUrl ?? this.fireflyTransactionUrl,
      isSubmitted: isSubmitted ?? this.isSubmitted,
      submissionError: submissionError ?? this.submissionError,
      submittedAt: submittedAt ?? this.submittedAt,
      createdAt: createdAt ?? this.createdAt,
    );
  }
}
