import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:sizer/sizer.dart';
import 'package:niogu_app/core/constants/app_color.dart';
import 'package:niogu_app/core/constants/app_font_size.dart';

class InsufficientStockDialog extends StatelessWidget {
  final String materialName;
  final String currentStock;
  final String totalNeeded;
  final String missingAmount;
  final String unit;
  final String triggerProductName;
  final int otherProductsCount;

  final VoidCallback onAddStockPressed;
  final VoidCallback onEditUsagePressed;

  const InsufficientStockDialog({
    super.key,
    required this.materialName,
    required this.currentStock,
    required this.totalNeeded,
    required this.missingAmount,
    required this.unit,
    required this.triggerProductName,
    this.otherProductsCount = 0,
    required this.onAddStockPressed,
    required this.onEditUsagePressed,
  });

  @override
  Widget build(BuildContext context) {
    const Color warningColor = Color(0xFFE65100);
    final bool isTablet = 100.w >= 600;
    final double maxWidth = 100.w >= 1280
        ? 1200
        : isTablet
        ? 800
        : 400;

    return Dialog(
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4.w)),
      backgroundColor: Colors.white,
      elevation: 0,
      child: Container(
        padding: EdgeInsets.all(5.w),
        width: double.infinity,
        constraints: BoxConstraints(maxWidth: maxWidth),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Container(
              padding: EdgeInsets.all(3.w),
              decoration: BoxDecoration(
                color: warningColor.withOpacity(0.1),
                shape: BoxShape.circle,
              ),
              child: Icon(
                Icons.warning_amber_rounded,
                color: warningColor,
                size: 8.w,
              ),
            ),
            SizedBox(height: 2.h),
            Text(
              "Peringatan Stok Bahan Baku",
              textAlign: TextAlign.center,
              style: TextStyle(
                fontSize: AppFontSize.medium.sp,
                fontWeight: FontWeight.bold,
                color: Colors.black87,
              ),
            ),
            SizedBox(height: 1.h),
            Text(
              "Stok bahan baku tidak mencukupi untuk memproses pesanan ini.",
              textAlign: TextAlign.center,
              style: TextStyle(
                fontSize: isTablet
                    ? AppFontSize.medium.sp
                    : AppFontSize.small.sp,
                color: Colors.grey[600],
              ),
            ),

            SizedBox(height: 3.h),

            Container(
              padding: EdgeInsets.all(4.w),
              decoration: BoxDecoration(
                color: Colors.grey[50],
                borderRadius: BorderRadius.circular(2.5.w),
                border: Border.all(color: Colors.grey.shade200),
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    children: [
                      Icon(
                        Icons.inventory_2_outlined,
                        size: 4.w,
                        color: Colors.black54,
                      ),
                      SizedBox(width: 2.w),
                      Expanded(
                        child: Text(
                          materialName,
                          style: TextStyle(
                            fontWeight: FontWeight.bold,
                            fontSize: isTablet
                                ? AppFontSize.medium.sp
                                : AppFontSize.small.sp,
                          ),
                        ),
                      ),
                    ],
                  ),
                  Divider(height: 3.h, color: Colors.grey.shade300),

                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      _buildStatItem(isTablet, "Sisa", "$currentStock $unit"),
                      Container(
                        width: 1,
                        height: 4.h,
                        color: Colors.grey.shade300,
                      ),
                      _buildStatItem(isTablet, "Butuh", "$totalNeeded $unit"),
                      Container(
                        width: 1,
                        height: 4.h,
                        color: Colors.grey.shade300,
                      ),
                      _buildStatItem(
                        isTablet,
                        "Kurang",
                        "$missingAmount $unit",
                        isHighlight: true, // Merah
                      ),
                    ],
                  ),
                ],
              ),
            ),

            SizedBox(height: 2.h),

            Container(
              width: double.infinity,
              padding: EdgeInsets.symmetric(horizontal: 3.w, vertical: 1.5.h),
              decoration: BoxDecoration(
                color: Colors.blue[50],
                borderRadius: BorderRadius.circular(2.w),
                border: Border.all(color: Colors.blue.withOpacity(0.2)),
              ),
              child: Row(
                children: [
                  Icon(
                    Icons.shopping_bag_outlined,
                    size: 4.5.w,
                    color: Colors.blue[700],
                  ),
                  SizedBox(width: 3.w),
                  Expanded(
                    child: RichText(
                      text: TextSpan(
                        style: TextStyle(
                          fontSize: isTablet
                              ? AppFontSize.medium.sp
                              : AppFontSize.small.sp,
                          color: Colors.blue[900],
                          overflow: TextOverflow.ellipsis,
                        ),
                        children: [
                          const TextSpan(text: "Digunakan oleh: "),
                          TextSpan(
                            text: triggerProductName,
                            style: const TextStyle(fontWeight: FontWeight.bold),
                          ),
                          if (otherProductsCount > 0)
                            TextSpan(
                              text: " +$otherProductsCount lainnya",
                              style: TextStyle(
                                color: Colors.blue[700],
                                overflow: TextOverflow.ellipsis,
                              ),
                            ),
                        ],
                      ),
                    ),
                  ),
                ],
              ),
            ),

            SizedBox(height: 3.5.h),

            Column(
              children: [
                SizedBox(
                  width: double.infinity,
                  height: 6.h,
                  child: ElevatedButton.icon(
                    onPressed: () {
                      context.pop();
                      onAddStockPressed();
                    },
                    style: ElevatedButton.styleFrom(
                      backgroundColor: AppColor.primaryColor,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(2.5.w),
                      ),
                      elevation: 0,
                    ),
                    icon: Icon(
                      Icons.add_box_rounded,
                      color: Colors.white,
                      size: 5.w,
                    ),
                    label: Text(
                      "Tambah Bahan Baku",
                      style: TextStyle(
                        color: Colors.white,
                        fontWeight: FontWeight.bold,
                        fontSize: AppFontSize.medium.sp,
                      ),
                    ),
                  ),
                ),

                SizedBox(height: 1.5.h),

                SizedBox(
                  width: double.infinity,
                  height: 6.h,
                  child: OutlinedButton(
                    onPressed: () {
                      context.pop();
                      onEditUsagePressed();
                    },
                    style: OutlinedButton.styleFrom(
                      side: BorderSide(color: AppColor.primaryColor),
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(2.5.w),
                      ),
                    ),
                    child: Text(
                      "Ubah Jumlah Penggunaan",
                      style: TextStyle(
                        color: AppColor.primaryColor,
                        fontWeight: FontWeight.bold,
                        fontSize: AppFontSize.medium.sp,
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildStatItem(
    bool isTablet,
    String label,
    String value, {
    bool isHighlight = false,
  }) {
    return Column(
      children: [
        Text(
          label,
          style: TextStyle(
            fontSize: isTablet ? AppFontSize.medium.sp : AppFontSize.small.sp,
            color: Colors.grey[600],
          ),
        ),
        SizedBox(height: 0.5.h),
        Text(
          value,
          style: TextStyle(
            fontSize: isTablet ? AppFontSize.medium.sp : AppFontSize.small.sp,
            fontWeight: FontWeight.bold,
            color: isHighlight ? Colors.red : Colors.black87,
          ),
        ),
      ],
    );
  }
}
