import 'package:pshared/models/payment/customer.dart';
import 'package:pshared/models/payment/fees/treatment.dart';
import 'package:pshared/models/payment/intent.dart';
import 'package:pshared/models/payment/kind.dart';
import 'package:pshared/models/payment/methods/card.dart';
import 'package:pshared/models/payment/methods/data.dart';
import 'package:pshared/models/payment/settlement_mode.dart';
import 'package:pshared/utils/money.dart';
import 'package:pshared/utils/payment/fx_helpers.dart';

import 'package:pweb/models/payment/multiple_payouts/csv_row.dart';


class MultipleIntentBuilder {
  static const String _currency = 'RUB';

  List<PaymentIntent> buildIntents({
    required PaymentMethodData sourceMethod,
    required String sourceCurrency,
    required List<CsvPayoutRow> rows,
  }) {
    final fxIntent = FxIntentHelper.buildSellBaseBuyQuote(
      baseCurrency: sourceCurrency,
      quoteCurrency: _currency,
    );

    return rows
        .asMap()
        .entries
        .map((entry) {
          final rowIndex = entry.key;
          final row = entry.value;
          final amount = parseMoneyWithCurrencyCode(row.amount, _currency);
          if (amount == null) {
            throw FormatException(
              'Invalid CSV amount at row ${rowIndex + 1}: ${row.amount}',
            );
          }
          final destination = CardPaymentMethod(
            pan: row.pan,
            firstName: row.firstName,
            lastName: row.lastName,
            expMonth: row.expMonth,
            expYear: row.expYear,
          );
          return PaymentIntent(
            kind: PaymentKind.payout,
            source: sourceMethod,
            destination: destination,
            amount: amount,
            feeTreatment: FeeTreatment.addToSource,
            settlementMode: SettlementMode.fixReceived,
            fx: fxIntent,
            comment: row.comment,
            customer: Customer(
              id: 'csv_row_${rowIndex + 1}',
              firstName: destination.firstName,
              lastName: destination.lastName,
            ),
          );
        })
        .toList(growable: false);
  }
}
