import 'package:flutter/material.dart';

import 'package:pshared/models/payment/payment.dart';
import 'package:pshared/utils/payment/quote_helpers.dart';

import 'package:pweb/pages/report/details/summary_card/amount_headline.dart';
import 'package:pweb/pages/report/details/summary_card/copy_id.dart';
import 'package:pweb/pages/report/details/summary_card/info_line.dart';
import 'package:pweb/pages/report/table/badge.dart';
import 'package:pweb/utils/report/amount_parts.dart';
import 'package:pweb/utils/report/format.dart';
import 'package:pweb/utils/report/payment_mapper.dart';
import 'package:pweb/utils/clipboard.dart';

import 'package:pweb/generated/i18n/app_localizations.dart';

class PaymentSummaryCard extends StatelessWidget {
  final Payment payment;

  const PaymentSummaryCard({super.key, required this.payment});

  @override
  Widget build(BuildContext context) {
    final loc = AppLocalizations.of(context)!;
    final theme = Theme.of(context);
    final unavailableValue = loc.valueUnavailable;
    final status = statusFromPayment(payment);
    final dateLabel = formatDateLabel(context, resolvePaymentDate(payment));

    final primaryAmount =
        payment.lastQuote?.amounts?.sourceDebitTotal ??
        payment.lastQuote?.amounts?.destinationSettlement;
    final toAmount = payment.lastQuote?.amounts?.destinationSettlement;
    final fee = quoteFeeTotal(payment.lastQuote);

    final amountLabel = formatMoney(context, primaryAmount);
    final toAmountLabel = formatMoney(context, toAmount);
    final feeLabel = formatMoney(context, fee);
    final paymentRef = (payment.paymentRef ?? '').trim();

    final showToAmount = toAmountLabel != unavailableValue;
    final showFee = payment.lastQuote != null;
    final feeText = feeLabel != unavailableValue
        ? loc.fee(feeLabel)
        : loc.fee(loc.noFee);
    final showPaymentId = paymentRef.isNotEmpty;
    final amountParts = splitAmount(amountLabel);

    return Card(
      elevation: 0,
      shape: RoundedRectangleBorder(
        side: BorderSide(color: theme.dividerColor.withAlpha(25)),
        borderRadius: BorderRadius.circular(16),
      ),
      child: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              children: [
                OperationStatusBadge(status: status),
                const Spacer(),
                Text(
                  dateLabel,
                  style: theme.textTheme.bodySmall?.copyWith(
                    color: theme.colorScheme.onSurfaceVariant,
                  ),
                ),
              ],
            ),
            const SizedBox(height: 12),
            AmountHeadline(
              amount: amountParts.amount,
              currency: amountParts.currency,
            ),
            const SizedBox(height: 6),
            if (amountLabel != unavailableValue)
              InfoLine(
                icon: Icons.send_outlined,
                text: loc.sentAmount(amountLabel),
              ),
            if (showToAmount && toAmountLabel != unavailableValue)
              InfoLine(
                icon: Icons.south_east,
                text: loc.recipientWillReceive(toAmountLabel),
              ),
            if (showFee)
              InfoLine(
                icon: Icons.receipt_long_outlined,
                text: feeText,
                muted: true,
              ),
            if (showPaymentId) ...[
              const SizedBox(height: 16),
              Divider(color: theme.dividerColor.withAlpha(35), height: 1),
              const SizedBox(height: 12),
              Align(
                alignment: Alignment.centerRight,
                child: CopyableId(
                  label: loc.paymentIdLabel,
                  value: paymentRef,
                  onCopy: () =>
                      copyToClipboard(context, paymentRef, loc.paymentIdCopied),
                ),
              ),
            ],
          ],
        ),
      ),
    );
  }
}
