import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/app_assets.dart';
import 'package:hmg_patient_app_new/core/app_state.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/extensions/widget_extensions.dart';
import 'package:hmg_patient_app_new/features/book_appointments/book_appointments_view_model.dart';
import 'package:hmg_patient_app_new/features/book_appointments/models/resp_models/get_clinic_list_response_model.dart';
import 'package:hmg_patient_app_new/features/book_appointments/models/resp_models/get_livecare_clinics_response_model.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';

class ClinicCard extends StatelessWidget {
  ClinicCard({super.key, required this.clinicsListResponseModel, required this.liveCareClinicsResponseModel, required this.isLoading, required this.bookAppointmentsVM});

  GetClinicsListResponseModel clinicsListResponseModel;
  GetLiveCareClinicsResponseModel liveCareClinicsResponseModel;
  bool isLoading;
  BookAppointmentsViewModel bookAppointmentsVM;

  @override
  Widget build(BuildContext context) {
    AppState appState = getIt.get<AppState>();
    
    // Get clinic icon path with fallback
    String getClinicIconPath() {
      final clinicId = clinicsListResponseModel.clinicID ?? 1;
      // Try to use specific clinic icon, fallback to default if not found
      return "assets/images/clinicIcons/$clinicId.svg";
    }
    
    return Container(
      padding: EdgeInsets.all(16.h),
      decoration: RoundedRectangleBorder().toSmoothCornerDecoration(
        color: AppColors.whiteColor,
        borderRadius: 24.h,
        hasShadow: false,
      ),
      child: Column(
        children: [
          Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
            // Use Image.asset with error builder for fallback
            SizedBox(
              width: 24.w,
              height: 24.h,
              child: Utils.buildSvgWithAssets(
                icon: getClinicIconPath(), 
                width: 24.w, 
                height: 24.h, 
                fit: BoxFit.contain,
                errorAsset: "assets/images/clinicIcons/1.svg"
                // Note: Add error handling in the Utils.buildSvgWithAssets if possible
                // or use a try-catch wrapper
              ),
            ).toShimmer2(isShow: isLoading),
            (clinicsListResponseModel.isLiveCareClinicAndOnline ?? true)
                ? Utils.buildSvgWithAssets(icon: AppAssets.livecare_clinic_icon, width: 32.w, height: 32.h, fit: BoxFit.contain, applyThemeColor: false).toShimmer2(isShow: isLoading)
                : SizedBox.shrink(),
            ],
          ),
          SizedBox(height: 16.h),
          Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
            Expanded(
                child: (isLoading ? "Cardiology" : (bookAppointmentsVM.isLiveCareSchedule ? liveCareClinicsResponseModel.clinicDesc! : clinicsListResponseModel.clinicDescription!))
                    .toText16(isBold: true)
                    .toShimmer2(isShow: isLoading)),
            Transform.flip(
                flipX: appState.isArabic(),
                child: Utils.buildSvgWithAssets(icon: AppAssets.forward_arrow_icon, width: 40.h, height: 40.h, fit: BoxFit.contain, iconColor: AppColors.textColor).toShimmer2(isShow: isLoading)),
          ]),
        ],
      ),
    );
  }
}
