import 'package:flutter/material.dart';
import 'package:cashumit/models/receipt_group.dart';
import 'package:cashumit/widgets/account_settings_widget.dart';
import 'package:cashumit/widgets/receipt_item_list.dart';
import 'package:cashumit/widgets/horizontal_divider.dart';
import 'package:cashumit/utils/currency_format.dart';
import 'package:cashumit/utils/app_text_styles.dart';

/// Section untuk satu grup transaksi (akun sumber + daftar item + subtotal).
class ReceiptGroupSection extends StatelessWidget {
  final ReceiptGroup group;
  final int groupIndex;
  final VoidCallback onSelectSource;
  final VoidCallback onRemoveGroup;
  final Function(int)? onEditItem;
  final Function(int)? onRemoveItem;
  final VoidCallback onAddItem;
  final bool canRemove;

  const ReceiptGroupSection({
    super.key,
    required this.group,
    required this.groupIndex,
    required this.onSelectSource,
    required this.onRemoveGroup,
    this.onEditItem,
    this.onRemoveItem,
    required this.onAddItem,
    this.canRemove = true,
  });

  @override
  Widget build(BuildContext context) {
    final primaryColor = Theme.of(context).colorScheme.primary;

    return Container(
      margin: const EdgeInsets.only(bottom: 8),
      padding: const EdgeInsets.all(8),
      decoration: BoxDecoration(
        border: Border.all(color: Colors.grey[300]!),
        borderRadius: BorderRadius.circular(4),
        color: Colors.white,
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // Header grup: dropdown "Dari:" + tombol hapus di kanan
          Row(
            children: [
              Expanded(
                child: SourceAccountSelector(
                  sourceAccount:
                      group.sourceAccountName ?? 'Pilih Sumber',
                  onSelectSource: onSelectSource,
                ),
              ),
              if (canRemove) ...[
                const SizedBox(width: 4),
                IconButton(
                  icon: const Icon(Icons.delete_outline, size: 18),
                  padding: EdgeInsets.zero,
                  constraints: const BoxConstraints(),
                  onPressed: onRemoveGroup,
                  tooltip: 'Hapus grup',
                  color: Colors.red[400],
                ),
              ],
            ],
          ),
          const SizedBox(height: 8),
          const HorizontalDivider(),
          const SizedBox(height: 8),
          // Daftar item grup
          ReceiptItemList(
            items: group.items,
            onEditItem: onEditItem,
            onRemoveItem: onRemoveItem,
            onAddItem: onAddItem,
          ),
          const SizedBox(height: 8),
          // Subtotal grup
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                'Subtotal:',
                style: CourierStyles.bold.copyWith(fontSize: 13),
              ),
              Text(
                CurrencyFormat.formatRupiah(group.total),
                style: CourierStyles.bold
                    .copyWith(fontSize: 13, color: primaryColor),
              ),
            ],
          ),
        ],
      ),
    );
  }
}
