import 'package:flutter/material.dart';
import 'package:cashumit/widgets/store_info_widget.dart';
import 'package:cashumit/widgets/receipt_group_section.dart';
import 'package:cashumit/widgets/receipt_total.dart';
import 'package:cashumit/widgets/store_disclaimer.dart';
import 'package:cashumit/widgets/thank_you_pantun.dart';
import 'package:cashumit/widgets/dotted_line.dart';
import 'package:cashumit/widgets/horizontal_divider.dart';
import 'package:cashumit/widgets/dashed_border.dart';
import 'package:cashumit/utils/app_text_styles.dart';
import 'package:cashumit/models/receipt_group.dart';

class ReceiptBody extends StatelessWidget {
  final List<ReceiptGroup> groups;
  final DateTime transactionDate;
  final VoidCallback onOpenStoreInfoConfig;
  final VoidCallback onChangeDate;
  final VoidCallback onOpenCustomTextConfig;
  final double total;
  final Function(String groupId, int index)? onEditItem;
  final Function(String groupId, int index)? onRemoveItem;
  final Function(String groupId)? onAddItem;
  final Function(String groupId)? onRemoveGroup;
  final Function(String groupId)? onSelectGroupSource;
  final VoidCallback onAddGroup;
  final VoidCallback? onSetTip;
  final VoidCallback? onClearTip;

  const ReceiptBody({
    super.key,
    required this.groups,
    required this.transactionDate,
    required this.onOpenStoreInfoConfig,
    required this.onChangeDate,
    required this.onOpenCustomTextConfig,
    required this.total,
    this.onEditItem,
    this.onRemoveItem,
    this.onAddItem,
    this.onRemoveGroup,
    this.onSelectGroupSource,
    required this.onAddGroup,
    this.onSetTip,
    this.onClearTip,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 360,
      decoration: const BoxDecoration(
        color: Colors.white,
      ),
      child: SingleChildScrollView(
        child: Column(
          children: [
            // Bagian atas - Header (akun tujuan di Config, bukan di sini)
            Container(
              padding: const EdgeInsets.all(8),
              child: Column(
                children: [
                  StoreInfoWidget(
                    onTap: onOpenStoreInfoConfig,
                    transactionDate: transactionDate,
                    onChangeDate: onChangeDate,
                  ),
                  const DottedLine(),
                  const SizedBox(height: 8),
                  const HorizontalDivider(),
                ],
              ),
            ),

            // Bagian tengah - Grup-grup dan total
            Container(
              padding: const EdgeInsets.all(8),
              child: Column(
                children: [
                  // Daftar grup
                  ...groups.asMap().entries.map((entry) {
                    int gi = entry.key;
                    ReceiptGroup group = entry.value;
                    return ReceiptGroupSection(
                      group: group,
                      groupIndex: gi,
                      onSelectSource: () =>
                          onSelectGroupSource?.call(group.id),
                      onRemoveGroup: () => onRemoveGroup?.call(group.id),
                      onEditItem: (itemIndex) =>
                          onEditItem?.call(group.id, itemIndex),
                      onRemoveItem: (itemIndex) =>
                          onRemoveItem?.call(group.id, itemIndex),
                      onAddItem: () => onAddItem?.call(group.id),
                      canRemove: groups.length > 1,
                    );
                  }),

                  // Tombol tambah grup sumber (gaya template card border putus-putus)
                  if (groups.length < 10)
                    Padding(
                      padding: const EdgeInsets.only(top: 4, bottom: 8),
                      child: GestureDetector(
                        onTap: onAddGroup,
                        child: DashedBorder(
                          color: Colors.grey,
                          padding: const EdgeInsets.all(8.0),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Icon(Icons.add,
                                  size: 16, color: Colors.grey[700]),
                              const SizedBox(width: 8),
                              Text(
                                'Tambah Sumber',
                                style: CourierStyles.bold
                                    .copyWith(color: Colors.grey[700]),
                              ),
                            ],
                          ),
                        ),
                      ),
                    ),

                  const HorizontalDivider(),
                  ReceiptTotal(
                    total: total,
                    onSetTip: onSetTip,
                    onClearTip: onClearTip,
                  ),
                ],
              ),
            ),

            // Bagian bawah - Footer
            Container(
              padding: const EdgeInsets.all(8),
              child: Column(
                children: [
                  const DottedLine(),
                  StoreDisclaimer(onTap: onOpenCustomTextConfig),
                  ThankYouPantun(onTap: onOpenCustomTextConfig),
                  const SizedBox(height: 16),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
