import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/extensions/string_extensions.dart';
import 'package:hmg_patient_app_new/features/hmg_services/models/ui_models/e_referral_form_model.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';

class ReferralValidator {
  static FormValidationErrors validateStep1(ReferralFormData formData, BuildContext context) {
    final errors = FormValidationErrors();

    if (formData.requesterName.trim().isEmpty) {
      errors.requesterName = LocaleKeys.requesterNameRequired.tr(context: context);
    }

    if (formData.relationship == null) {
      errors.relationship = LocaleKeys.selectRelationshipRequired.tr(context: context);
    }

    if (formData.relationship != null &&
        formData.relationship?.iD == 5 &&
        formData.otherRelationshipName.trim().isEmpty) {
      errors.otherRelationshipName = LocaleKeys.otherRelationshipNameRequired.tr(context: context);
    }

    return errors;
  }

  static FormValidationErrors validateStep2(ReferralFormData formData, BuildContext context) {
    final errors = FormValidationErrors();

    if (formData.patientIdentification.trim().isEmpty) {
      errors.patientIdentification = LocaleKeys.identificationNumberRequired.tr(context: context);
    }

    if (formData.patientName.trim().isEmpty) {
      errors.patientName = LocaleKeys.patientNameRequired.tr(context: context);
    }

    if (formData.patientPhone == null) {
      errors.patientPhone = LocaleKeys.enterPatientPhoneRequired.tr(context: context);
    }

    if (formData.patientCity == null) {
      errors.patientCity = LocaleKeys.selectPatientCityRequired.tr(context: context);
    }

    return errors;
  }

  static FormValidationErrors validateStep3(ReferralFormData formData, BuildContext context) {
    final errors = FormValidationErrors();

    if (formData.medicalReportImages.isEmpty) {
      errors.medicalReport = LocaleKeys.medicalReportRequired.tr(context: context);
    }

    if (formData.branch == null) {
      errors.branch = LocaleKeys.selectBranchRequired.tr(context: context);
    }

    if (formData.isPatientInsured && formData.insuredPatientImages.isEmpty) {
      errors.insuredDocument = LocaleKeys.insuranceDocumentRequired.tr(context: context);
    }

    return errors;
  }

  static bool _isValidSaudiPhone(String phone) {
    final regex = RegExp(r'^5\d{8}$');
    return regex.hasMatch(phone);
  }

  static bool hasErrors(FormValidationErrors errors) {
    return errors.requesterName != null ||
        errors.requesterPhone != null ||
        errors.relationship != null ||
        errors.otherRelationshipName != null ||
        errors.patientIdentification != null ||
        errors.patientName != null ||
        errors.patientPhone != null ||
        errors.patientCity != null ||
        errors.medicalReport != null ||
        errors.branch != null ||
        errors.insuredDocument != null;
  }
}