import 'package:flutter/material.dart';
import 'package:intl/intl.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/stock_card_type.dart';
import 'package:niogu_app/core/utils/time_zone.dart';
import 'package:niogu_app/features/report/stock_report/domain/entities/stok_report.dart';
import 'package:sizer/sizer.dart';

class MovementCard extends StatelessWidget {
  final StockMovement movement;
  final String unit;
  final VoidCallback onTap;
  const MovementCard({
    super.key,
    required this.movement,
    required this.unit,
    required this.onTap,
  });

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

    bool modeChange = movement.modeChange;

    final String? beforeChange = movement.beforeChange;

    final String? afterChange = movement.afterChange;

    modeChange = modeChange && beforeChange != null && afterChange != null;

    final bool isUnlimited = movement.isUnlimited;

    final bool isStockIn = movement.isStockIn;

    final bool isInitialStock = movement.type == StockCardType.initial_stock;

    return GestureDetector(
      onTap: onTap,
      child: Container(
        margin: EdgeInsets.only(bottom: 1.5.h),
        padding: EdgeInsets.all(4.w),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(3.w),
          border: Border.all(color: Colors.grey.shade200),
        ),
        child: Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  "${DateFormat('dd MMM yyyy HH:mm').format(movement.date)} ${TimeZone.getCurrentTimeZone()}",
                  style: TextStyle(
                    fontSize: isTablet
                        ? (AppFontSize.medium - 1.25).sp
                        : (AppFontSize.small - 1.25).sp,
                    color: Colors.grey.shade700,
                  ),
                ),
                Text(
                  movement.userName,
                  style: TextStyle(
                    color: Colors.grey[700],
                    fontWeight: FontWeight.bold,
                    fontSize: isTablet
                        ? (AppFontSize.medium - 1.25).sp
                        : (AppFontSize.small - 1.25).sp,
                  ),
                ),
              ],
            ),
            Divider(height: 3.h, color: Colors.grey[100]),
            Row(
              children: [
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      if (!isUnlimited ||
                          (isUnlimited && isStockIn && modeChange) ||
                          (isUnlimited && !isInitialStock)) ...[
                        Text(
                          modeChange ? "Perubahan Stok" : movement.description,
                          style: TextStyle(
                            fontWeight: FontWeight.bold,
                            fontSize: isTablet
                                ? (AppFontSize.medium - 1.25).sp
                                : (AppFontSize.small - 1.25).sp,
                          ),
                        ),
                      ],
                      if (movement.notes != null &&
                          movement.notes!.isNotEmpty) ...[
                        SizedBox(height: 0.5.h),
                        Text(
                          movement.notes!,
                          style: TextStyle(
                            color: Colors.grey[700],
                            fontSize: isTablet
                                ? (AppFontSize.medium - 1.25).sp
                                : (AppFontSize.small - 1.25).sp,
                          ),
                        ),
                      ],
                      SizedBox(height: 0.5.h),

                      if (isUnlimited && isInitialStock) ...[
                        if (modeChange) ...[
                          Row(
                            children: [
                              Text(
                                beforeChange,
                                style: TextStyle(
                                  color: Colors.grey.shade700,
                                  fontWeight: FontWeight.bold,
                                  fontSize: isTablet
                                      ? (AppFontSize.medium - 1.25).sp
                                      : (AppFontSize.small - 1.25).sp,
                                ),
                              ),

                              SizedBox(width: 0.5.w),

                              Icon(
                                Icons.arrow_forward_outlined,
                                color: Colors.grey.shade700,
                                size: 3.w,
                              ),

                              SizedBox(width: 0.5.w),

                              Text(
                                afterChange,
                                style: TextStyle(
                                  color: Colors.grey.shade700,
                                  fontWeight: FontWeight.bold,
                                  fontSize: isTablet
                                      ? (AppFontSize.medium - 1.25).sp
                                      : (AppFontSize.small - 1.25).sp,
                                ),
                              ),
                            ],
                          ),
                        ] else
                          Text(
                            "Pembuatan Stok Baru",
                            style: TextStyle(
                              color: Colors.grey.shade700,
                              fontWeight: FontWeight.bold,
                              fontSize: isTablet
                                  ? (AppFontSize.medium - 1.25).sp
                                  : (AppFontSize.small - 1.25).sp,
                            ),
                          ),
                      ] else if ((isUnlimited &&
                              !isInitialStock &&
                              !isStockIn) ||
                          (!isUnlimited && !isStockIn)) ...[
                        Text(
                          "${movement.stockOut.toStringWithoutTrailingZero()} $unit",
                          style: TextStyle(
                            color: Colors.red[600],
                            fontWeight: FontWeight.bold,
                            fontSize: isTablet
                                ? (AppFontSize.medium - 1.25).sp
                                : (AppFontSize.small - 1.25).sp,
                          ),
                        ),
                      ] else ...[
                        if (modeChange) ...[
                          Row(
                            children: [
                              Text(
                                "${movement.stockIn.toStringWithoutTrailingZero()} $unit",
                                style: TextStyle(
                                  color: Colors.green.shade600,
                                  fontWeight: FontWeight.bold,
                                  fontSize: isTablet
                                      ? (AppFontSize.medium - 1.25).sp
                                      : (AppFontSize.small - 1.25).sp,
                                ),
                              ),

                              SizedBox(width: 1.5.w),

                              Text(
                                "• $beforeChange",
                                style: TextStyle(
                                  color: Colors.grey.shade700,
                                  fontWeight: FontWeight.bold,
                                  fontSize: isTablet
                                      ? (AppFontSize.medium - 1.25).sp
                                      : (AppFontSize.small - 1.25).sp,
                                ),
                              ),

                              SizedBox(width: 0.5.w),

                              Icon(
                                Icons.arrow_forward_outlined,
                                color: Colors.grey.shade700,
                                size: 3.w,
                              ),

                              SizedBox(width: 0.5.w),

                              Text(
                                afterChange,
                                style: TextStyle(
                                  color: Colors.grey.shade700,
                                  fontWeight: FontWeight.bold,
                                  fontSize: isTablet
                                      ? (AppFontSize.medium - 1.25).sp
                                      : (AppFontSize.small - 1.25).sp,
                                ),
                              ),
                            ],
                          ),
                        ] else
                          Text(
                            "${movement.stockIn.toStringWithoutTrailingZero()} $unit",
                            style: TextStyle(
                              color: Colors.green.shade600,
                              fontWeight: FontWeight.bold,
                              fontSize: isTablet
                                  ? (AppFontSize.medium - 1.25).sp
                                  : (AppFontSize.small - 1.25).sp,
                            ),
                          ),
                      ],
                    ],
                  ),
                ),
                Column(
                  crossAxisAlignment: CrossAxisAlignment.end,
                  children: [
                    if (movement.isUnlimited) ...[
                      Text(
                        "Produk Selalu Ada",
                        style: TextStyle(
                          color: AppColor.primaryColor,
                          fontWeight: FontWeight.bold,
                          fontSize: isTablet
                              ? (AppFontSize.medium - 1.25).sp
                              : (AppFontSize.small - 1.25).sp,
                        ),
                      ),
                    ] else ...[
                      Text(
                        "Sisa Stok",
                        style: TextStyle(
                          color: Colors.grey,
                          fontSize: isTablet
                              ? (AppFontSize.medium - 1.25).sp
                              : (AppFontSize.small - 1.25).sp,
                        ),
                      ),
                      Text(
                        "${movement.remainingStock.toStringWithoutTrailingZero() + ' ' + unit}",
                        style: TextStyle(
                          color: AppColor.primaryColor,
                          fontWeight: FontWeight.bold,
                          fontSize: isTablet
                              ? (AppFontSize.medium - 1.25).sp
                              : (AppFontSize.small - 1.25).sp,
                        ),
                      ),
                    ],
                  ],
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
