import 'package:flutter/material.dart';
import 'package:niogu_app/core/constants/app_color.dart';
import 'package:niogu_app/core/constants/app_font_size.dart';
import 'package:niogu_app/core/utils/currency_format.dart';
import 'package:niogu_app/core/utils/extension_format.dart';
import 'package:niogu_app/core/enums/stock_type.dart';
import 'package:niogu_app/features/pos/domain/entities/pos.dart';
import 'package:sizer/sizer.dart';

class CartItemTile extends StatelessWidget {
  final CartItems cartItem;
  final void Function(CartItems) onDecrementTap;
  final void Function(CartItems) onIncrementTap;
  final void Function(CartItems) onEditPressed;
  final void Function(CartItems) onDeletePressed;

  const CartItemTile({
    super.key,
    required this.cartItem,
    required this.onDecrementTap,
    required this.onIncrementTap,
    required this.onEditPressed,
    required this.onDeletePressed,
  });

  @override
  Widget build(BuildContext context) {
    final bool isTablet = 100.w >= 600;

    return Padding(
      padding: EdgeInsets.only(bottom: 2.h),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            width: 10.w,
            height: 10.w,
            padding: EdgeInsets.symmetric(horizontal: 2.w, vertical: 0.5.h),
            decoration: BoxDecoration(
              color: Colors.grey[200],
              borderRadius: BorderRadius.circular(1.w),
            ),
            child: Center(
              child: Text(
                "${cartItem.quantity.toStringWithoutTrailingZero()}",
                style: TextStyle(
                  fontWeight: FontWeight.bold,
                  fontSize: isTablet
                      ? AppFontSize.medium.sp
                      : AppFontSize.small.sp,
                ),
              ),
            ),
          ),
          SizedBox(width: 3.w),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  cartItem.name,
                  style: TextStyle(
                    overflow: TextOverflow.ellipsis,
                    fontWeight: FontWeight.w600,
                    fontSize: isTablet
                        ? AppFontSize.medium.sp
                        : AppFontSize.small.sp,
                    height: 1.2,
                  ),
                ),

                if (cartItem.variantName != null &&
                    cartItem.variantName!.isNotEmpty) ...[
                  SizedBox(height: 0.5.h),
                  Text(
                    cartItem.variantName!,
                    style: TextStyle(
                      overflow: TextOverflow.ellipsis,
                      color: Colors.grey,
                      fontSize: isTablet
                          ? AppFontSize.medium.sp
                          : AppFontSize.small.sp,
                      height: 1.2,
                    ),
                  ),
                ],

                SizedBox(height: 0.5.h),

                Text(
                  cartItem.stockType == StockType.unlimited
                      ? "Stok : Selalu Ada"
                      : "Sisa Stok : ${cartItem.remainingStock.toStringWithoutTrailingZero()} ${cartItem.unit}",
                  style: TextStyle(
                    fontSize: isTablet
                        ? AppFontSize.medium.sp
                        : AppFontSize.small.sp,
                  ),
                ),
                Text(
                  CurrencyFormat.formatToIdr(cartItem.sellingPrice, 0),
                  style: TextStyle(
                    color: AppColor.primaryColor,
                    fontSize: isTablet
                        ? AppFontSize.medium.sp
                        : AppFontSize.small.sp,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ],
            ),
          ),
          Container(
            decoration: BoxDecoration(
              color: Colors.grey[50],
              borderRadius: BorderRadius.circular(2.w),
              border: Border.all(color: Colors.grey.shade200),
            ),
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                InkWell(
                  onTap: cartItem.quantity > 1
                      ? () => onDecrementTap(cartItem)
                      : null,
                  child: Padding(
                    padding: EdgeInsets.all(1.5.w),
                    child: Icon(
                      Icons.remove,
                      size: 4.w,
                      color: AppColor.primaryColor,
                    ),
                  ),
                ),

                Container(
                  margin: EdgeInsets.only(top: 0.5.h),
                  width: 10.w,
                  padding: EdgeInsets.symmetric(horizontal: 2.w),
                  child: Center(
                    child: Text(
                      "${cartItem.quantity.toStringWithoutTrailingZero()}",
                      style: TextStyle(
                        fontWeight: FontWeight.bold,
                        fontSize: isTablet
                            ? AppFontSize.medium.sp
                            : AppFontSize.small.sp,
                      ),
                    ),
                  ),
                ),

                InkWell(
                  onTap: () => onIncrementTap(cartItem),
                  child: Padding(
                    padding: EdgeInsets.all(1.5.w),
                    child: Icon(
                      Icons.add,
                      size: 4.w,
                      color: AppColor.primaryColor,
                    ),
                  ),
                ),
              ],
            ),
          ),

          SizedBox(width: 1.w),

          Container(
            margin: EdgeInsets.only(top: 0.5.h),
            child: IconButton(
              onPressed: () => onEditPressed(cartItem),
              icon: Icon(
                Icons.edit_note_rounded,
                color: Colors.grey,
                size: 6.w,
              ),
              padding: EdgeInsets.zero,
              constraints: const BoxConstraints(),
              style: IconButton.styleFrom(
                tapTargetSize: MaterialTapTargetSize.shrinkWrap,
              ),
            ),
          ),

          SizedBox(width: 1.w),

          Container(
            margin: EdgeInsets.only(top: 0.5.h),
            child: IconButton(
              onPressed: () => onDeletePressed(cartItem),
              icon: Icon(Icons.delete_outline, color: Colors.red, size: 6.w),
              padding: EdgeInsets.zero,
              constraints: const BoxConstraints(),
              style: IconButton.styleFrom(
                tapTargetSize: MaterialTapTargetSize.shrinkWrap,
              ),
            ),
          ),
        ],
      ),
    );
  }
}
