import 'package:flutter/material.dart';

import '../../../../common/extension/extension.dart';
import '../../../../common/theme/theme.dart';
import '../../../../domain/order/order.dart';
import '../../../components/spaces/space.dart';

class VoidProductCard extends StatelessWidget {
  final bool isSelected;
  final OrderItem item;
  final int voidQty;
  final bool canSelect;
  final Function()? onTapDecrease;
  final Function()? onTapIncrease;
  const VoidProductCard({
    super.key,
    required this.isSelected,
    required this.item,
    required this.voidQty,
    required this.canSelect,
    required this.onTapDecrease,
    required this.onTapIncrease,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.only(bottom: 12),
      decoration: BoxDecoration(
        color: isSelected ? AppColor.primary.withOpacity(0.1) : Colors.white,
        border: Border.all(
          color: isSelected ? AppColor.primary : AppColor.border,
          width: isSelected ? 2 : 1,
        ),
        borderRadius: BorderRadius.circular(8),
      ),
      child: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          children: [
            Row(
              children: [
                // Product Info
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        item.productName,
                        style: AppStyle.lg.copyWith(
                          fontWeight: FontWeight.w600,
                        ),
                      ),
                      if (item.productVariantName.isNotEmpty)
                        Text(
                          'Varian: ${item.productVariantName}',
                          style: AppStyle.sm.copyWith(
                            color: AppColor.textSecondary,
                          ),
                        ),
                      const SpaceHeight(4),
                      Row(
                        children: [
                          Container(
                            padding: const EdgeInsets.symmetric(
                              horizontal: 8,
                              vertical: 2,
                            ),
                            decoration: BoxDecoration(
                              color: Colors.blue.withOpacity(0.1),
                              borderRadius: BorderRadius.circular(12),
                            ),
                            child: Text(
                              'Qty: ${item.quantity}',
                              style: AppStyle.sm.copyWith(
                                fontWeight: FontWeight.w500,
                                color: Colors.blue[700],
                              ),
                            ),
                          ),
                          const SpaceWidth(8),
                          Text(
                            (item.unitPrice).currencyFormatRpV2,
                            style: AppStyle.sm.copyWith(
                              color: AppColor.textSecondary,
                            ),
                          ),
                        ],
                      ),
                      if (item.notes.isNotEmpty)
                        Padding(
                          padding: const EdgeInsets.only(top: 4),
                          child: Container(
                            padding: const EdgeInsets.symmetric(
                              horizontal: 8,
                              vertical: 2,
                            ),
                            decoration: BoxDecoration(
                              color: Colors.orange.withOpacity(0.1),
                              borderRadius: BorderRadius.circular(4),
                            ),
                            child: Text(
                              'Catatan: ${item.notes}',
                              style: AppStyle.xs.copyWith(
                                color: AppColor.warning,
                              ),
                            ),
                          ),
                        ),
                    ],
                  ),
                ),

                if (canSelect) ...[
                  Container(
                    decoration: BoxDecoration(
                      border: Border.all(color: AppColor.border),
                      borderRadius: BorderRadius.circular(6),
                    ),
                    child: Row(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        // Decrease Button
                        InkWell(
                          onTap: onTapDecrease,
                          child: Container(
                            padding: const EdgeInsets.all(8),
                            child: Icon(
                              Icons.remove,
                              size: 16,
                              color: voidQty > 0
                                  ? AppColor.primary
                                  : Colors.grey[400],
                            ),
                          ),
                        ),

                        // Quantity Display
                        Container(
                          padding: const EdgeInsets.symmetric(
                            horizontal: 16,
                            vertical: 8,
                          ),
                          decoration: const BoxDecoration(
                            border: Border.symmetric(
                              vertical: BorderSide(color: AppColor.border),
                            ),
                          ),
                          child: Text(
                            '$voidQty',
                            style: AppStyle.sm.copyWith(
                              fontWeight: FontWeight.w600,
                            ),
                          ),
                        ),

                        // Increase Button
                        InkWell(
                          onTap: onTapIncrease,
                          child: Container(
                            padding: const EdgeInsets.all(8),
                            child: Icon(
                              Icons.add,
                              size: 16,
                              color: voidQty < (item.quantity)
                                  ? AppColor.primary
                                  : Colors.grey[400],
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                  const SpaceWidth(20),
                ],
                // Price
                Column(
                  crossAxisAlignment: CrossAxisAlignment.end,
                  children: [
                    Text(
                      (item.totalPrice).currencyFormatRpV2,
                      style: AppStyle.lg.copyWith(
                        fontWeight: FontWeight.bold,
                        color: AppColor.primary,
                      ),
                    ),
                    if (isSelected)
                      Container(
                        padding: const EdgeInsets.symmetric(
                          horizontal: 6,
                          vertical: 2,
                        ),
                        decoration: BoxDecoration(
                          color: Colors.red.withOpacity(0.1),
                          borderRadius: BorderRadius.circular(4),
                        ),
                        child: Text(
                          'Void: ${((item.unitPrice) * voidQty).currencyFormatRpV2}',
                          style: AppStyle.sm.copyWith(
                            fontSize: 12,
                            color: AppColor.error,
                            fontWeight: FontWeight.w500,
                          ),
                        ),
                      ),
                  ],
                ),
              ],
            ),

            // Quantity Controls (only show for item void)
          ],
        ),
      ),
    );
  }
}
