// lib/providers/receipt_state.dart

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

class ReceiptState {
  List<ReceiptItem> items;
  DateTime transactionDate;
  List<Map<String, dynamic>> accounts;
  String? sourceAccountId;
  String? sourceAccountName;
  String? destinationAccountId;
  String? destinationAccountName;
  String? fireflyUrl;
  String? accessToken;

  ReceiptState({
    List<ReceiptItem>? items,
    DateTime? transactionDate,
    List<Map<String, dynamic>>? accounts,
    this.sourceAccountId,
    this.sourceAccountName,
    this.destinationAccountId,
    this.destinationAccountName,
    this.fireflyUrl,
    this.accessToken,
  })  : items = items ?? [],
        transactionDate = transactionDate ?? DateTime.now(),
        accounts = accounts ?? [];

  // CopyWith method untuk membuat salinan state dengan perubahan tertentu
  ReceiptState copyWith({
    List<ReceiptItem>? items,
    DateTime? transactionDate,
    List<Map<String, dynamic>>? accounts,
    String? sourceAccountId,
    String? sourceAccountName,
    String? destinationAccountId,
    String? destinationAccountName,
    String? fireflyUrl,
    String? accessToken,
  }) {
    return ReceiptState(
      items: items ?? this.items,
      transactionDate: transactionDate ?? this.transactionDate,
      accounts: accounts ?? this.accounts,
      sourceAccountId: sourceAccountId ?? this.sourceAccountId,
      sourceAccountName: sourceAccountName ?? this.sourceAccountName,
      destinationAccountId: destinationAccountId ?? this.destinationAccountId,
      destinationAccountName: destinationAccountName ?? this.destinationAccountName,
      fireflyUrl: fireflyUrl ?? this.fireflyUrl,
      accessToken: accessToken ?? this.accessToken,
    );
  }

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

  @override
  String toString() {
    return 'ReceiptState(items: $items, transactionDate: $transactionDate, accounts: $accounts, sourceAccountId: $sourceAccountId, sourceAccountName: $sourceAccountName, destinationAccountId: $destinationAccountId, destinationAccountName: $destinationAccountName, fireflyUrl: $fireflyUrl, accessToken: $accessToken)';
  }
}