import 'package:flutter/material.dart';

import 'package:pshared/models/payment/execution_operation.dart';

import 'package:pweb/utils/report/operations/state_mapper.dart';
import 'package:pweb/pages/report/details/sections/operations/state_chip.dart';
import 'package:pweb/utils/money_display.dart';
import 'package:pweb/utils/report/operations/time_format.dart';
import 'package:pweb/utils/report/operations/title_mapper.dart';

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


class OperationHistoryTile extends StatelessWidget {
  final PaymentExecutionOperation operation;
  final bool canDownloadDocument;
  final VoidCallback? onDownloadDocument;

  const OperationHistoryTile({
    super.key,
    required this.operation,
    required this.canDownloadDocument,
    this.onDownloadDocument,
  });

  @override
  Widget build(BuildContext context) {
    final loc = AppLocalizations.of(context)!;
    final theme = Theme.of(context);
    final operationLabel = operation.label?.trim();
    final title = (operationLabel != null && operationLabel.isNotEmpty)
        ? operationLabel
        : resolveOperationTitle(loc, operation.code);
    // final operationComment = operation.comment?.trim();
    final stateView = resolveStepStateView(context, operation.state);
    final completedAt = formatCompletedAt(context, operation.completedAt);
    final amount = formatMoneyUi(context, operation.amount);
    final convertedAmount = formatMoneyUi(context, operation.convertedAmount);
    final canDownload = canDownloadDocument && onDownloadDocument != null;

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Expanded(
              child: Text(
                title,
                style: theme.textTheme.bodyMedium?.copyWith(
                  fontWeight: FontWeight.w600,
                ),
              ),
            ),
            const SizedBox(width: 8),
            StepStateChip(view: stateView),
          ],
        ),
        const SizedBox(height: 6),
        Text(
          '${loc.completedAtLabel}: $completedAt',
          style: theme.textTheme.bodySmall?.copyWith(
            color: theme.colorScheme.onSurfaceVariant,
          ),
        ),
        const SizedBox(height: 4),
        Text(
          '${loc.amountColumn}: $amount',
          style: theme.textTheme.bodySmall?.copyWith(
            color: theme.colorScheme.onSurfaceVariant,
          ),
        ),
        if (operation.convertedAmount != null) ...[
          const SizedBox(height: 4),
          Text(
            '${loc.toAmountColumn}: $convertedAmount',
            style: theme.textTheme.bodySmall?.copyWith(
              color: theme.colorScheme.onSurfaceVariant,
            ),
          ),
        ],
        // if (operationComment != null && operationComment.isNotEmpty) ...[
        //   const SizedBox(height: 4),
        //   Text(
        //     '${loc.comment}: $operationComment',
        //     style: theme.textTheme.bodySmall?.copyWith(
        //       color: theme.colorScheme.onSurfaceVariant,
        //     ),
        //   ),
        // ],
        if (canDownload) ...[
          const SizedBox(height: 8),
          TextButton.icon(
            onPressed: onDownloadDocument,
            icon: const Icon(Icons.download),
            label: Text(loc.downloadAct),
          ),
        ],
      ],
    );
  }
}
