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/dependencies.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/features/lab/lab_view_model.dart';
import 'package:hmg_patient_app_new/features/lab/models/resp_models/patient_lab_order_response_by_ai_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/buttons/custom_button.dart';

class AiAnalysisWidget extends StatelessWidget {
  final LabOrderResponseByAi data;

  const AiAnalysisWidget({super.key, required this.data});

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(borderRadius: BorderRadius.circular(24.r), gradient: AppColors.aiLinearGradient),
      padding: EdgeInsets.all(1.5.r), // Border thickness
      child: Container(
        decoration: BoxDecoration(
          color: AppColors.whiteColor,
          borderRadius: BorderRadius.circular(22.r),
        ),
        padding: EdgeInsets.all(20.r),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                LocaleKeys.aiAnalysis.tr().toText18(weight: FontWeight.w700, color: AppColors.blackColor),
                Container(
                  padding: EdgeInsets.all(8.r),
                  decoration: BoxDecoration(gradient: AppColors.aiLinearGradient, borderRadius: BorderRadius.circular(8.r)),
                  child: Utils.buildSvgWithAssets(
                    icon: AppAssets.aiOverView,
                    iconColor: Colors.white,
                    height: 18.r,
                    width: 18.r,
                    applyThemeColor: false,
                  ),
                ),
              ],
            ),
            SizedBox(height: 16.h),
            if (data.summary != null && data.summary!.isNotEmpty) ...[
              LocaleKeys.suggestion.tr().toText16(weight: FontWeight.w700, color: AppColors.blackColor),
              SizedBox(height: 8.h),
              data.summary!.toText14(color: AppColors.textColorLight, height: 1.5, weight: FontWeight.w400),
              SizedBox(height: 16.h),
              Divider(color: AppColors.dividerColor, thickness: 1),
              SizedBox(height: 16.h),
            ],
            if (data.aiPredictions != null && data.aiPredictions!.isNotEmpty) ...[
              LocaleKeys.prediction.tr().toText16(weight: FontWeight.w700, color: AppColors.blackColor),
              SizedBox(height: 12.h),
              ...data.aiPredictions!.map((item) => _buildBulletItem(item)),
              SizedBox(height: 16.h),
              Divider(color: AppColors.dividerColor, thickness: 1),
              SizedBox(height: 16.h),
            ],
            if (data.prevention != null && data.prevention!.isNotEmpty) ...[
              LocaleKeys.prevention.tr().toText16(weight: FontWeight.w700, color: AppColors.blackColor),
              SizedBox(height: 12.h),
              ...data.prevention!.map((item) => _buildBulletItem(item)),
              SizedBox(height: 20.h),
            ],
            Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Icon(Icons.info_outline, color: AppColors.greyTextColor, size: 20.r),
                SizedBox(width: 8.w),
                Expanded(
                  child: ("${LocaleKeys.disclaimer.tr()}:${LocaleKeys.thisAboveInfo.tr()}").toText12(color: AppColors.greyTextColor, fontWeight: FontWeight.w400),
                ),
              ],
            ),
            SizedBox(height: 16.h),
            CustomButton(
              height: 50.h,
              text: LocaleKeys.close.tr(context: context),
              backgroundColor: AppColors.primaryRedColor,
              borderColor: AppColors.primaryRedColor,
              onPressed: () {
                getIt.get<LabViewModel>().closeAILabResultAnalysis();
              },
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildBulletItem(String text) {
    return Padding(
      padding: EdgeInsets.only(bottom: 12.h),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Padding(
            padding: EdgeInsets.only(top: 8.h, right: 12.w, left: 4.w),
            child: Container(
              width: 5.r,
              height: 5.r,
              decoration: BoxDecoration(color: AppColors.blackColor, shape: BoxShape.circle),
            ),
          ),
          Expanded(
            child: text.toText14(color: AppColors.textColorLight, height: 1.5, weight: FontWeight.w400),
          ),
        ],
      ),
    );
  }
}
