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/report/stock_report/domain/entities/stok_report.dart';
import 'package:sizer/sizer.dart';

class StockCardItem extends StatelessWidget {
  final StockReportItem item;
  final VoidCallback onTap;
  const StockCardItem({super.key, required this.item, required this.onTap});

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

    final bool imagePathAvailable =
        item.imageProduct != null &&
        item.imageProduct!.isNotEmpty &&
        item.imageProduct!.isNotEmpty;

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

    final bool imageFileExists = imageFile.existsSync();

    return GestureDetector(
      onTap: onTap,
      child: Container(
        clipBehavior: Clip.antiAlias,
        margin: EdgeInsets.only(bottom: 2.h),
        padding: EdgeInsets.all(4.w),
        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: 10,
              offset: const Offset(0, 4),
            ),
          ],
        ),
        child: Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                Container(
                  width: 18.w,
                  height: 18.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 Icon(
                                Icons.error,
                                size: 8.w,
                                color: Colors.grey.shade400,
                              );
                            },
                          ),
                        )
                      : Center(
                          child: Icon(
                            item.itemType == ItemType.product_variant
                                ? Icons.image
                                : Icons.inventory,
                            size: 8.w,
                            color: Colors.grey.shade400,
                          ),
                        ),
                ),
                SizedBox(width: 2.w),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        item.name,
                        style: TextStyle(
                          color: item.hasBeenDelete
                              ? Colors.grey.shade500
                              : Colors.grey.shade700,
                          fontWeight: FontWeight.bold,
                          fontSize: isTablet
                              ? AppFontSize.medium.sp
                              : AppFontSize.small.sp,
                          decoration: item.hasBeenDelete
                              ? TextDecoration.lineThrough
                              : TextDecoration.none,
                        ),
                      ),

                      if (item.variantName != null &&
                          item.variantName!.isNotEmpty) ...[
                        SizedBox(height: 0.75.h),
                        Text(
                          item.variantName!,
                          style: TextStyle(
                            color: item.hasBeenDelete
                                ? Colors.grey.shade500
                                : Colors.grey.shade700,
                            fontSize: isTablet
                                ? (AppFontSize.medium - 1.25).sp
                                : (AppFontSize.small - 1.25).sp,
                            decoration: item.hasBeenDelete
                                ? TextDecoration.lineThrough
                                : TextDecoration.none,
                          ),
                        ),
                      ],

                      if (item.hasBeenDelete) ...[
                        SizedBox(height: 0.75.h),
                        Text(
                          "Barang Dihapus ${item.isDeleteByOwner ? "Oleh Owner" : ""}",
                          style: TextStyle(
                            color: Colors.red.shade300,
                            fontSize: isTablet
                                ? (AppFontSize.medium - 1.25).sp
                                : (AppFontSize.small - 1.25).sp,
                          ),
                        ),
                      ],
                    ],
                  ),
                ),
              ],
            ),
            SizedBox(height: 2.h),

            Table(
              children: [
                if (item.isUnlimited) ...[
                  _buildTableRow(
                    "Terjual",
                    "${item.totalStockOut.toStringWithoutTrailingZero()} ${item.unit}",
                    Colors.red[600]!,
                    isTablet,
                  ),
                ] else ...[
                  _buildTableRow(
                    "Stok Awal",
                    "${item.initialStock.toStringWithoutTrailingZero()} ${item.unit}",
                    item.isUnlimited ? Colors.black : Colors.grey[700]!,
                    isTablet,
                  ),
                  _buildTableRow(
                    "Stok Masuk",
                    "${item.totalStockIn.toStringWithoutTrailingZero()} ${item.unit}",
                    item.isUnlimited ? Colors.black : Colors.green[600]!,
                    isTablet,
                  ),
                  _buildTableRow(
                    "Stok Keluar",
                    "${item.totalStockOut.toStringWithoutTrailingZero()} ${item.unit}",
                    Colors.red[600]!,
                    isTablet,
                  ),
                ],
              ],
            ),

            Divider(height: 3.h, color: Colors.grey[100]),

            Row(
              mainAxisAlignment: item.isUnlimited
                  ? MainAxisAlignment.center
                  : MainAxisAlignment.spaceBetween,
              children: [
                if (item.isUnlimited) ...[
                  Text(
                    "Produk Selalu Ada",
                    style: TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: isTablet
                          ? (AppFontSize.medium - 1.25).sp
                          : (AppFontSize.small - 1.25).sp,
                    ),
                  ),
                ] else ...[
                  Text(
                    "Stok Akhir",
                    style: TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: isTablet
                          ? (AppFontSize.medium - 1.25).sp
                          : (AppFontSize.small - 1.25).sp,
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.symmetric(
                      horizontal: 3.w,
                      vertical: 0.5.h,
                    ),
                    decoration: BoxDecoration(
                      color: AppColor.primaryColor.withOpacity(0.1),
                      borderRadius: BorderRadius.circular(1.5.w),
                    ),
                    child: Text(
                      "${item.lastStock.toStringWithoutTrailingZero()} ${item.unit}",
                      style: TextStyle(
                        color: AppColor.primaryColor,
                        fontWeight: FontWeight.bold,
                        fontSize: isTablet
                            ? (AppFontSize.medium - 1.25).sp
                            : (AppFontSize.small - 1.25).sp,
                      ),
                    ),
                  ),
                ],
              ],
            ),
          ],
        ),
      ),
    );
  }

  TableRow _buildTableRow(
    String label,
    String value,
    Color valueColor,
    bool isTablet,
  ) {
    return TableRow(
      children: [
        Padding(
          padding: EdgeInsets.symmetric(vertical: 0.8.h),
          child: Text(
            label,
            style: TextStyle(
              color: Colors.grey[600],
              fontSize: isTablet
                  ? (AppFontSize.medium - 1.25).sp
                  : (AppFontSize.small - 1.25).sp,
            ),
          ),
        ),
        Padding(
          padding: EdgeInsets.symmetric(vertical: 0.8.h),
          child: Text(
            value,
            textAlign: TextAlign.right,
            style: TextStyle(
              color: valueColor,
              fontWeight: FontWeight.w600,
              fontSize: isTablet
                  ? (AppFontSize.medium - 1.25).sp
                  : (AppFontSize.small - 1.25).sp,
            ),
          ),
        ),
      ],
    );
  }
}
