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 HeaderCard extends StatelessWidget {
  final bool modeChange;
  final String? beforeChange;
  final String? afterChange;
  final DateTime? dateChange;
  final StockReportDetail stockReportDetail;
  const HeaderCard({
    super.key,
    required this.modeChange,
    required this.beforeChange,
    required this.afterChange,
    required this.dateChange,
    required this.stockReportDetail,
  });

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

    return Container(
      margin: EdgeInsets.all(5.w),
      padding: EdgeInsets.all(5.w),
      decoration: BoxDecoration(
        color: AppColor.primaryColor,
        borderRadius: BorderRadius.circular(4.w),
        boxShadow: [
          BoxShadow(
            color: AppColor.primaryColor.withOpacity(0.3),
            blurRadius: 10,
            offset: const Offset(0, 5),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            stockReportDetail.itemName,
            overflow: TextOverflow.ellipsis,
            style: TextStyle(
              color: Colors.white,
              fontWeight: FontWeight.bold,
              fontSize: isTablet ? AppFontSize.medium.sp : AppFontSize.small.sp,
            ),
          ),
          if (stockReportDetail.variantName != null) ...[
            SizedBox(height: 0.75.h),
            Text(
              stockReportDetail.variantName!,
              style: TextStyle(
                color: Colors.white,
                fontWeight: FontWeight.w400,
                fontSize: isTablet
                    ? (AppFontSize.medium - 1.25).sp
                    : (AppFontSize.small - 1.25).sp,
              ),
            ),
          ],
          SizedBox(height: 1.5.h),
          _buildHeaderRow(
            Icons.storefront,
            stockReportDetail.outletName,
            isTablet,
          ),
          if (stockReportDetail.isUnlimited) ...[
            _buildHeaderRow(
              Icons.check_box_outlined,
              "Produk Selalu Ada",
              isTablet,
            ),
            if (modeChange) ...[
              _buildHeaderRow(
                Icons.update_outlined,
                "Perubahan Stok Terbaru ($beforeChange ke $afterChange)",
                isTablet,
              ),
              Padding(
                padding: EdgeInsets.only(bottom: 0.8.h),
                child: Row(
                  children: [
                    Icon(Icons.date_range, color: Colors.white, size: 4.5.w),
                    SizedBox(width: 2.5.w),
                    Text(
                      "Tanggal Perubahan Terbaru: ${DateFormat('dd MMM yyyy').format(dateChange!)}",
                      style: TextStyle(
                        color: Colors.white,
                        fontWeight: FontWeight.w500,
                        fontSize: isTablet
                            ? AppFontSize.medium.sp
                            : AppFontSize.small.sp,
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ] else ...[
            if (stockReportDetail.startDate != null &&
                stockReportDetail.endDate != null)
              _buildHeaderRow(
                Icons.date_range,
                "${DateFormat('dd MMM yyyy').format(stockReportDetail.startDate!)} - ${DateFormat('dd MMM yyyy').format(stockReportDetail.endDate!)}",
                isTablet,
              ),
            _buildHeaderRow(
              Icons.inventory_2_outlined,
              "Stok Awal ${stockReportDetail.startDate != null ? 'Per ${DateFormat('dd MMM yyyy').format(stockReportDetail.startDate!)}' : ''}: ${stockReportDetail.isUnlimited ? 'Selalu Ada' : (stockReportDetail.initialStock.toStringWithoutTrailingZero() + ' ' + stockReportDetail.unit)}",
              isTablet,
            ),
            if (modeChange) ...[
              _buildHeaderRow(
                Icons.update_outlined,
                "Perubahan Stok Terbaru ($beforeChange ke $afterChange)",
                isTablet,
              ),
              Padding(
                padding: EdgeInsets.only(bottom: 0.8.h),
                child: Row(
                  children: [
                    Icon(Icons.date_range, color: Colors.white, size: 4.5.w),
                    SizedBox(width: 2.5.w),
                    Text(
                      "Tanggal Perubahan Terbaru: ${DateFormat('dd MMM yyyy').format(dateChange!)}",
                      style: TextStyle(
                        color: Colors.white,
                        fontWeight: FontWeight.w500,
                        fontSize: isTablet
                            ? AppFontSize.medium.sp
                            : AppFontSize.small.sp,
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ],
        ],
      ),
    );
  }

  Widget _buildHeaderRow(IconData icon, String text, bool isTablet) {
    return Padding(
      padding: EdgeInsets.only(bottom: 0.8.h),
      child: Row(
        children: [
          Icon(icon, color: Colors.white, size: 4.5.w),
          SizedBox(width: 2.5.w),
          Text(
            text,
            style: TextStyle(
              color: Colors.white,
              fontWeight: FontWeight.w500,
              fontSize: isTablet ? AppFontSize.medium.sp : AppFontSize.small.sp,
            ),
          ),
        ],
      ),
    );
  }
}
