import 'package:enaklo_pos/core/constants/colors.dart';
import 'package:enaklo_pos/core/extensions/int_ext.dart';
import 'package:enaklo_pos/data/models/response/order_response_model.dart';
import 'package:flutter/material.dart';

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

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(bottom: 12),
      decoration: BoxDecoration(
        color: isSelected ? AppColors.primary.withOpacity(0.1) : Colors.white,
        border: Border.all(
          color: isSelected ? AppColors.primary : Colors.grey[300]!,
          width: isSelected ? 2 : 1,
        ),
        borderRadius: BorderRadius.circular(8),
      ),
      child: Padding(
        padding: EdgeInsets.all(16),
        child: Column(
          children: [
            Row(
              children: [
                // Product Icon
                Container(
                  width: 50,
                  height: 50,
                  decoration: BoxDecoration(
                    color: AppColors.primary.withOpacity(0.2),
                    borderRadius: BorderRadius.circular(8),
                  ),
                  child: Icon(
                    Icons.fastfood,
                    color: AppColors.primary,
                    size: 24,
                  ),
                ),
                SizedBox(width: 12),

                // Product Info
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        item.productName ?? 'Produk Tidak Diketahui',
                        style: TextStyle(
                          fontWeight: FontWeight.w600,
                          fontSize: 16,
                        ),
                      ),
                      if (item.productVariantName != null)
                        Text(
                          'Varian: ${item.productVariantName}',
                          style:
                              TextStyle(fontSize: 12, color: Colors.grey[600]),
                        ),
                      SizedBox(height: 4),
                      Row(
                        children: [
                          Container(
                            padding: EdgeInsets.symmetric(
                                horizontal: 8, vertical: 2),
                            decoration: BoxDecoration(
                              color: Colors.blue.withOpacity(0.1),
                              borderRadius: BorderRadius.circular(12),
                            ),
                            child: Text(
                              'Qty: ${item.quantity}',
                              style: TextStyle(
                                fontSize: 12,
                                fontWeight: FontWeight.w500,
                                color: Colors.blue[700],
                              ),
                            ),
                          ),
                          SizedBox(width: 8),
                          Text(
                            (item.unitPrice ?? 0).currencyFormatRpV2,
                            style: TextStyle(
                                fontSize: 12, color: Colors.grey[600]),
                          ),
                        ],
                      ),
                      if (item.notes != null && item.notes!.isNotEmpty)
                        Padding(
                          padding: EdgeInsets.only(top: 4),
                          child: Container(
                            padding: EdgeInsets.symmetric(
                                horizontal: 8, vertical: 2),
                            decoration: BoxDecoration(
                              color: Colors.orange.withOpacity(0.1),
                              borderRadius: BorderRadius.circular(4),
                            ),
                            child: Text(
                              'Catatan: ${item.notes}',
                              style: TextStyle(
                                fontSize: 11,
                                color: Colors.orange[700],
                              ),
                            ),
                          ),
                        ),
                    ],
                  ),
                ),

                // Price
                Column(
                  crossAxisAlignment: CrossAxisAlignment.end,
                  children: [
                    Text(
                      (item.totalPrice ?? 0).currencyFormatRpV2,
                      style: TextStyle(
                        fontWeight: FontWeight.bold,
                        fontSize: 16,
                        color: AppColors.primary,
                      ),
                    ),
                    if (isSelected)
                      Container(
                        padding:
                            EdgeInsets.symmetric(horizontal: 6, vertical: 2),
                        decoration: BoxDecoration(
                          color: Colors.red.withOpacity(0.1),
                          borderRadius: BorderRadius.circular(4),
                        ),
                        child: Text(
                          'Void: ${((item.unitPrice ?? 0) * voidQty).currencyFormatRpV2}',
                          style: TextStyle(
                            fontSize: 12,
                            color: Colors.red[700],
                            fontWeight: FontWeight.w500,
                          ),
                        ),
                      ),
                  ],
                ),
              ],
            ),

            // Quantity Controls (only show for item void)
            if (canSelect) ...[
              SizedBox(height: 16),
              Container(
                padding: EdgeInsets.all(12),
                decoration: BoxDecoration(
                  color: Colors.grey[50],
                  borderRadius: BorderRadius.circular(8),
                  border: Border.all(color: Colors.grey[300]!),
                ),
                child: Row(
                  children: [
                    Text(
                      'Kuantitas:',
                      style: TextStyle(
                        fontSize: 14,
                        fontWeight: FontWeight.w500,
                        color: Colors.grey[700],
                      ),
                    ),
                    Spacer(),

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

                          // Quantity Display
                          Container(
                            padding: EdgeInsets.symmetric(
                                horizontal: 16, vertical: 8),
                            decoration: BoxDecoration(
                              border: Border.symmetric(
                                vertical: BorderSide(color: Colors.grey[300]!),
                              ),
                            ),
                            child: Text(
                              '$voidQty',
                              style: TextStyle(
                                fontSize: 16,
                                fontWeight: FontWeight.w600,
                              ),
                            ),
                          ),

                          // Increase Button
                          InkWell(
                            onTap: onTapIncrease,
                            child: Container(
                              padding: EdgeInsets.all(8),
                              child: Icon(
                                Icons.add,
                                size: 16,
                                color: voidQty < (item.quantity ?? 0)
                                    ? AppColors.primary
                                    : Colors.grey[400],
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),

                    SizedBox(width: 12),

                    // Quick Actions
                    Row(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        TextButton(
                          onPressed: onTapAll,
                          style: TextButton.styleFrom(
                            padding: EdgeInsets.symmetric(
                                horizontal: 8, vertical: 4),
                            minimumSize: Size(0, 0),
                          ),
                          child: Text(
                            'Semua',
                            style: TextStyle(
                              fontSize: 12,
                              color: AppColors.primary,
                            ),
                          ),
                        ),
                        TextButton(
                          onPressed: onTapClear,
                          style: TextButton.styleFrom(
                            padding: EdgeInsets.symmetric(
                                horizontal: 8, vertical: 4),
                            minimumSize: Size(0, 0),
                          ),
                          child: Text(
                            'Hapus',
                            style: TextStyle(
                              fontSize: 12,
                              color:
                                  voidQty > 0 ? Colors.red : Colors.grey[400],
                            ),
                          ),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
            ],
          ],
        ),
      ),
    );
  }
}
