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/extension_format.dart';
import 'package:niogu_app/core/enums/item_type.dart';
import 'package:niogu_app/features/stock_in/presentation/widgets/counter_button.dart';
import 'package:niogu_app/features/stock_in/domain/entities/stock_in.dart';
import 'package:sizer/sizer.dart';

class ItemInputCard extends StatelessWidget {
  final SelectedItem item;
  final String label;
  final VoidCallback? onIncrementTap;
  final VoidCallback? onDecrementTap;
  final VoidCallback onEditPressed;
  final VoidCallback onDeletePressed;
  const ItemInputCard({
    super.key,
    required this.item,
    required this.label,
    required this.onIncrementTap,
    required this.onDecrementTap,
    required this.onEditPressed,
    required this.onDeletePressed,
  });

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

    final String path =
        item.localImagePath != null && item.localImagePath!.isNotEmpty
        ? item.localImagePath!
        : 'image not found';

    final File imageFile = File(path);

    final bool imageFileExists = imageFile.existsSync();

    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(2.5.w),
        border: Border.all(color: Colors.grey.shade200),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.02),
            blurRadius: 5,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Column(
        children: [
          Padding(
            padding: EdgeInsets.all(3.w),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Container(
                  height: 15.w,
                  width: 15.w,
                  decoration: BoxDecoration(
                    border: Border.all(color: Colors.grey.shade200, width: 1.5),
                    borderRadius: BorderRadius.circular(2.5.w),
                  ),
                  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 const Icon(
                                Icons.error,
                                color: Colors.grey,
                              );
                            },
                          ),
                        )
                      : Icon(
                          item.itemType == ItemType.product_variant
                              ? Icons.image_outlined
                              : Icons.inventory_2_rounded,
                          size: 8.w,
                          color: item.itemType == ItemType.product_variant
                              ? Colors.grey
                              : Colors.grey[400],
                        ),
                ),
                SizedBox(width: 3.w),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        item.name,
                        maxLines: 2,
                        overflow: TextOverflow.ellipsis,
                        style: TextStyle(
                          fontWeight: FontWeight.bold,
                          fontSize: isTablet
                              ? AppFontSize.medium.sp
                              : AppFontSize.small.sp,
                        ),
                      ),
                      if (item.variantName != null &&
                          item.variantName!.isNotEmpty) ...[
                        SizedBox(height: 0.5.h),
                        Text(
                          "(${item.variantName})",
                          style: TextStyle(
                            color: Colors.grey[600],
                            fontWeight: FontWeight.bold,
                            fontSize: isTablet
                                ? (AppFontSize.medium - 1.25).sp
                                : (AppFontSize.small - 1.25).sp,
                          ),
                        ),
                      ],
                      SizedBox(height: 0.5.h),
                      Text(
                        "Sisa Stok: ${item.remainingStock.toStringWithoutTrailingZero()} ${item.unit}",
                        style: TextStyle(
                          color: Colors.grey[600],
                          fontSize: isTablet
                              ? (AppFontSize.medium - 1.25).sp
                              : (AppFontSize.small - 1.25).sp,
                        ),
                      ),
                    ],
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(top: 0.5.h),
                  child: IconButton(
                    onPressed: onEditPressed,
                    icon: Icon(
                      Icons.edit_note_rounded,
                      color: Colors.grey,
                      size: 5.w,
                    ),
                    padding: EdgeInsets.zero,
                    constraints: const BoxConstraints(),
                    style: IconButton.styleFrom(
                      tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                    ),
                  ),
                ),

                SizedBox(width: 2.w),

                Container(
                  margin: EdgeInsets.only(top: 0.5.h),
                  child: IconButton(
                    onPressed: onDeletePressed,
                    icon: Icon(
                      Icons.delete_outline,
                      color: Colors.red,
                      size: 5.w,
                    ),
                    padding: EdgeInsets.zero,
                    constraints: const BoxConstraints(),
                    style: IconButton.styleFrom(
                      tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                    ),
                  ),
                ),
              ],
            ),
          ),
          Divider(height: 1, color: Colors.grey[100]),
          Padding(
            padding: EdgeInsets.symmetric(horizontal: 3.w, vertical: 2.h),
            child: Row(
              children: [
                Text(
                  label,
                  style: TextStyle(
                    fontSize: isTablet
                        ? (AppFontSize.medium - 1.25).sp
                        : (AppFontSize.small - 1.25).sp,
                    fontWeight: FontWeight.w500,
                  ),
                ),
                SizedBox(width: 3.w),
                Expanded(
                  child: Container(
                    height: 5.h,
                    decoration: BoxDecoration(
                      color: Colors.grey[50],
                      borderRadius: BorderRadius.circular(2.w),
                      border: Border.all(color: Colors.grey.shade300),
                    ),
                    child: Row(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        CounterButton(
                          icon: Icons.remove,
                          onTap: onDecrementTap,
                        ),
                        Expanded(
                          child: Container(
                            margin: EdgeInsets.only(top: 0.5.h),
                            padding: EdgeInsets.symmetric(horizontal: 2.w),
                            child: Center(
                              child: Text(
                                item.quantity.toStringWithoutTrailingZero(),
                                style: TextStyle(
                                  fontWeight: FontWeight.bold,
                                  fontSize: isTablet
                                      ? (AppFontSize.medium - 1.25).sp
                                      : (AppFontSize.small - 1.25).sp,
                                  color: AppColor.primaryColor,
                                ),
                              ),
                            ),
                          ),
                        ),
                        CounterButton(icon: Icons.add, onTap: onIncrementTap),
                      ],
                    ),
                  ),
                ),
                SizedBox(width: 2.w),
                Text(
                  item.unit,
                  style: TextStyle(
                    color: Colors.grey[600],
                    fontSize: isTablet
                        ? (AppFontSize.medium - 1.25).sp
                        : (AppFontSize.small - 1.25).sp,
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
