import 'dart:io';

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:niogu_app/features/pos/presentation/widgets/counter_button.dart';
import 'package:sizer/sizer.dart';

class ProductCard extends StatelessWidget {
  final DisplayProductPos product;
  final bool isInCart;
  final double quantity;
  final VoidCallback onPressed;
  final VoidCallback onDecrementTap;
  final VoidCallback onIncrementTap;

  const ProductCard({
    super.key,
    required this.product,
    required this.isInCart,
    required this.quantity,
    required this.onPressed,
    required this.onDecrementTap,
    required this.onIncrementTap,
  });

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

    final bool imagePathAvailable =
        product.imagePath != null &&
        product.imagePath!.isNotEmpty &&
        product.imagePath!.isNotEmpty;

    final File imageFile = File(
      imagePathAvailable ? product.imagePath! : "image not found",
    );

    final bool imageFileExists = imageFile.existsSync();

    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(2.5.w),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.05),
            blurRadius: 10,
            offset: const Offset(0, 4),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Expanded(
            child: Container(
              decoration: BoxDecoration(
                color: imageFileExists ? Colors.transparent : Colors.grey[200],
                borderRadius: BorderRadius.vertical(
                  top: Radius.circular(2.5.w),
                ),
                border: Border.all(color: Colors.grey.shade200, width: 1.5),
              ),
              child: imageFileExists
                  ? ClipRRect(
                      borderRadius: BorderRadius.circular(2.5.w),
                      child: Image.file(
                        imageFile,
                        fit: BoxFit.cover,
                        width: double.infinity,
                        height: double.infinity,
                        errorBuilder: (context, error, stackTrace) {
                          return Icon(
                            Icons.error,
                            size: 10.w,
                            color: Colors.grey,
                          );
                        },
                      ),
                    )
                  : Center(
                      child: Icon(Icons.image, size: 10.w, color: Colors.grey),
                    ),
            ),
          ),

          Padding(
            padding: EdgeInsets.all(3.w),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  product.name,
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(
                    fontSize: isTablet
                        ? AppFontSize.medium.sp
                        : AppFontSize.small.sp,
                    fontWeight: FontWeight.bold,
                  ),
                ),

                SizedBox(height: 0.5.h),
                
                Text(
                  product.hasVariant ? product.variantName : "",
                  style: TextStyle(
                    color: Colors.grey,
                    fontSize: isTablet
                        ? AppFontSize.medium.sp
                        : AppFontSize.small.sp,
                  ),
                ),

                SizedBox(height: 1.h),

                Text(
                  product.stockType == StockType.unlimited
                      ? "Stok : Selalu Ada"
                      : "Sisa Stok : ${product.remainingStock.toStringWithoutTrailingZero()} ${product.unitName}",
                  style: TextStyle(
                    fontSize: isTablet
                        ? AppFontSize.medium.sp
                        : AppFontSize.small.sp,
                  ),
                ),
                SizedBox(height: 0.5.h),

                Text(
                  CurrencyFormat.formatToIdr(product.sellingPrice, 0),
                  style: TextStyle(
                    fontSize: isTablet
                        ? AppFontSize.medium.sp
                        : AppFontSize.small.sp,
                    color: AppColor.primaryColor,
                    fontWeight: FontWeight.bold,
                  ),
                ),

                SizedBox(height: 1.h),

                if (isInCart)
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      CounterButton(onTap: onDecrementTap, icon: Icons.remove),
                      Text(
                        "${quantity.toStringWithoutTrailingZero()}",
                        style: TextStyle(
                          fontWeight: FontWeight.bold,
                          fontSize: isTablet
                              ? AppFontSize.medium.sp
                              : AppFontSize.small,
                        ),
                      ),
                      CounterButton(onTap: onIncrementTap, icon: Icons.add),
                    ],
                  )
                else
                  SizedBox(
                    width: double.infinity,
                    height: 4.h,
                    child: OutlinedButton(
                      onPressed: onPressed,
                      style: OutlinedButton.styleFrom(
                        side: BorderSide(color: AppColor.primaryColor),
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(2.w),
                        ),
                      ),
                      child: Text(
                        "Tambah",
                        style: TextStyle(
                          fontSize: AppFontSize.medium.sp,
                          color: AppColor.primaryColor,
                        ),
                      ),
                    ),
                  ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
