import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/app_assets.dart';
import 'package:hmg_patient_app_new/core/utils/size_utils.dart';
import 'package:hmg_patient_app_new/core/utils/utils.dart';
import 'package:hmg_patient_app_new/extensions/string_extensions.dart';
import 'package:hmg_patient_app_new/extensions/widget_extensions.dart';
import 'package:hmg_patient_app_new/features/offers_and_discounts/models/offers_and_discounts_response_model.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';
import 'package:hmg_patient_app_new/widgets/appbar/collapsing_list_view.dart';
import 'package:hmg_patient_app_new/widgets/buttons/custom_button.dart';
import 'package:hmg_patient_app_new/widgets/chip/app_custom_chip_widget.dart';

class OffersAndDiscountsDetailedPage extends StatelessWidget {
  final OffersAndDiscountsResponseModel offer;

  const OffersAndDiscountsDetailedPage({
    super.key,
    required this.offer,
  });

  String _formatDateString(String? dateString) {
    if (dateString == null || dateString.isEmpty) return '';
    try {
      DateTime date = DateTime.parse(dateString);
      return DateFormat('d MMM, yyyy').format(date);
    } catch (e) {
      return dateString;
    }
  }

  // Determine the status of the offer based on dates and isActive flag
  String _getOfferStatus(String? endDate, bool? isActive) {
    if (endDate == null || endDate.isEmpty) return 'Expired';
    try {
      DateTime end = DateTime.parse(endDate);
      DateTime now = DateTime.now();

      if (now.isAfter(end)) {
        return 'Expired';
      } else {
        if (isActive == true) {
          return 'Active';
        } else {
          return 'Availed';
        }
      }
    } catch (e) {
      return 'Expired';
    }
  }

  Color _getStatusBgColor(String status) {
    switch (status) {
      case 'Active':
        return AppColors.successColor.withValues(alpha: 0.1);
      case 'Availed':
        return AppColors.infoColor.withValues(alpha: 0.1);
      case 'Expired':
        return AppColors.errorColor.withValues(alpha: 0.1);
      default:
        return AppColors.greyColor;
    }
  }

  Color _getStatusTextColor(String status) {
    switch (status) {
      case 'Active':
        return AppColors.successColor;
      case 'Availed':
        return AppColors.infoColor;
      case 'Expired':
        return AppColors.errorColor;
      default:
        return AppColors.textColor;
    }
  }

  @override
  Widget build(BuildContext context) {
    final status = _getOfferStatus(offer.endDate, offer.isActive);

    return CollapsingListView(
      title: "${LocaleKeys.offers.tr(context: context)} ${LocaleKeys.details.tr(context: context)}",
      child: SingleChildScrollView(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // Offer Image
            if (offer.imageUrl != null)
              Stack(
                children: [
                  ClipRRect(
                    borderRadius: BorderRadius.circular(24.h),
                    child: Utils.buildImgWithNetwork(
                      url: offer.imageUrl!,
                      iconColor: AppColors.blackBgColor,
                      width: double.infinity,
                      height: 250.h,
                      fit: BoxFit.cover,
                      errorBuilder: (context, error, stackTrace) {
                        return Container(
                          height: 250.h,
                          decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
                            color: AppColors.greyColor,
                            borderRadius: 24.h,
                          ),
                          child: Center(
                            child: Icon(Icons.image_not_supported, size: 64.h),
                          ),
                        );
                      },
                    ),
                  ),
                  // Discount badge at top right on image
                  // if (offer.discount != null)
                  //   Positioned(
                  //     top: 16.h,
                  //     right: 16.w,
                  //     child: Container(
                  //       padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 8.h),
                  //       decoration: BoxDecoration(
                  //         color: AppColors.primaryRedColor,
                  //         borderRadius: BorderRadius.circular(8.r),
                  //       ),
                  //       child: Text(
                  //         offer.discount!,
                  //         style: TextStyle(
                  //           fontSize: 14.f,
                  //           fontWeight: FontWeight.w700,
                  //           color: AppColors.whiteColor,
                  //         ),
                  //       ),
                  //     ),
                  //   ),
                ],
              ),

            SizedBox(height: 24.h),

            // Title
            if (offer.title != null)
              offer.title!.toText24(
                isBold: true,
                fontWeight: FontWeight.w700,
              ),

            SizedBox(height: 16.h),

            // Chips Row (Valid Till and Status)
            Wrap(
              spacing: 12.w,
              runSpacing: 12.h,
              children: [
                // Valid till chip
                if (offer.endDate != null)
                  AppCustomChipWidget(
                    labelText: "Valid till ${_formatDateString(offer.endDate)}",
                    backgroundColor: AppColors.chipBgColor,
                    textColor: AppColors.textColor,
                    icon: AppAssets.calendar,
                    iconSize: 14.h,
                    iconColor: AppColors.textColor,
                    isEnglishOnly: true,
                  ),
                // Status chip
                AppCustomChipWidget(
                  labelText: status,
                  backgroundColor: _getStatusBgColor(status),
                  textColor: _getStatusTextColor(status),
                ),
                // Facility type chip
                if (offer.facilityType != null)
                  AppCustomChipWidget(
                    labelText: offer.facilityType!,
                    backgroundColor: AppColors.chipBgColor,
                    textColor: AppColors.textColor,
                  ),
              ],
            ),

            SizedBox(height: 32.h),

            // Description Heading
            LocaleKeys.description.tr(context: context).toText18(
                  isBold: true,
                  weight: FontWeight.w700,
                ),

            SizedBox(height: 12.h),

            // Description Content
            if (offer.description != null)
              offer.description!.toText16(
                color: AppColors.greyTextColor,
                height: 1.5,
              ),

            SizedBox(height: 32.h),

            // Buy Now Button
            CustomButton(
              text: "Buy Now",
              icon: AppAssets.shoppingCart,
              iconColor: AppColors.whiteColor,
              onPressed: () {
                // Handle buy now action
              },
              backgroundColor: AppColors.primaryRedColor,
              borderColor: AppColors.primaryRedColor,
              textColor: AppColors.whiteColor,
              fontSize: 16.f,
              isBold: true,
              borderRadius: 12.r,
              height: 56.h,
            ),

            SizedBox(height: 24.h),
          ],
        ).paddingSymmetrical(24.h, 0.h),
      ),
    );
  }
}
