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

class StickyFooter extends StatelessWidget {
  final bool modeChange;
  final StockReportDetail stockReportDetail;
  const StickyFooter({
    super.key,
    required this.modeChange,
    required this.stockReportDetail,
  });

  @override
  Widget build(BuildContext context) {
    final bool isTablet = 100.w >= 600;
    return Container(
      padding: EdgeInsets.fromLTRB(5.w, 1.5.h, 5.w, 2.h),
      decoration: BoxDecoration(
        color: Colors.white,
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.05),
            blurRadius: 10,
            offset: const Offset(0, -5),
          ),
        ],
      ),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          if (stockReportDetail.isUnlimited) ...[
            if (modeChange) ...[
              SizedBox(height: 0.75.h),
              Text(
                "Total terjual dihitung sejak tanggal perubahan stok terbaru",
                style: TextStyle(
                  fontSize: isTablet
                      ? (AppFontSize.medium - 1.25).sp
                      : (AppFontSize.small - 1.25).sp,
                  color: Colors.grey[700],
                  fontStyle: FontStyle.italic,
                ),
              ),
            ],
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text(
                  "Total terjual ${stockReportDetail.endDate != null ? 'hingga ${DateFormat('dd MMMM yyyy').format(stockReportDetail.endDate!)}' : 'seluruh  periode'}",
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: isTablet
                        ? (AppFontSize.medium - 1.25).sp
                        : (AppFontSize.small - 1.25).sp,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                SizedBox(width: 2.5.w),
                Text(
                  "${stockReportDetail.totalStockOut.toStringWithoutTrailingZero()} ${stockReportDetail.unit}",
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: isTablet
                        ? (AppFontSize.medium - 1.25).sp
                        : (AppFontSize.small - 1.25).sp,
                    color: Colors.red[700]!,
                  ),
                ),
              ],
            ),
          ] else ...[
            if (modeChange) ...[
              Text(
                "Akumulasi dihitung sejak tanggal perubahan stok terbaru",
                style: TextStyle(
                  fontSize: isTablet
                      ? (AppFontSize.medium - 1.25).sp
                      : (AppFontSize.small - 1.25).sp,
                  color: Colors.grey[700],
                  fontStyle: FontStyle.italic,
                ),
              ),
              SizedBox(height: 0.75.h),
            ],
            Text(
              "Informasi akumulasi ${stockReportDetail.endDate != null ? 'per ${DateFormat('dd MMMM yyyy').format(stockReportDetail.endDate!)}' : 'seluruh  periode'}",
              style: TextStyle(
                fontSize: isTablet
                    ? (AppFontSize.medium - 1.25).sp
                    : (AppFontSize.small - 1.25).sp,
                color: Colors.black,
                fontWeight: FontWeight.bold,
              ),
            ),
            SizedBox(height: 1.5.h),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                _footerItem(
                  "Total Masuk",
                  "${(stockReportDetail.totalStockIn.toStringWithoutTrailingZero() + ' ' + stockReportDetail.unit)}",
                  Colors.green[700]!,
                  isTablet,
                ),
                _footerItem(
                  "Total Keluar",
                  "${stockReportDetail.totalStockOut.toStringWithoutTrailingZero()} ${stockReportDetail.unit}",
                  Colors.red[700]!,
                  isTablet,
                ),
                _footerItem(
                  "Stok Akhir",
                  "${(stockReportDetail.lastStock.toStringWithoutTrailingZero() + ' ' + stockReportDetail.unit)}",
                  AppColor.primaryColor,
                  isTablet,
                ),
              ],
            ),
          ],
        ],
      ),
    );
  }

  Widget _footerItem(String label, String value, Color color, bool isTablet) {
    return Column(
      children: [
        Text(
          label,
          style: TextStyle(
            fontSize: isTablet
                ? (AppFontSize.medium - 1.25).sp
                : (AppFontSize.small - 1.25).sp,
            color: Colors.grey,
          ),
        ),
        SizedBox(height: 0.5.h),
        Text(
          value,
          style: TextStyle(
            fontWeight: FontWeight.bold,
            fontSize: isTablet
                ? (AppFontSize.medium - 1.25).sp
                : (AppFontSize.small - 1.25).sp,
            color: color,
          ),
        ),
      ],
    );
  }
}
