import 'package:hmg_patient_app_new/core/enums.dart';

class TriageDataDetails {
  final TriageQuestion? question;
  final List<TriageCondition>? conditions;
  final bool? hasEmergencyEvidence;
  final bool? shouldStop;
  final String? interviewToken;
  final String? message;
  final List<String>? errorList;
  final int? id;
  final String? language;
  final String? generalId;
  final String? createDate;
  final String? lastEditDate;
  final String? createdBy;
  final String? lastEditBy;
  final bool? active;
  final int? sortOrder;
  final int? userType;
  final String? userId;

  TriageDataDetails({
    this.question,
    this.conditions,
    this.hasEmergencyEvidence,
    this.shouldStop,
    this.interviewToken,
    this.message,
    this.errorList,
    this.id,
    this.language,
    this.generalId,
    this.createDate,
    this.lastEditDate,
    this.createdBy,
    this.lastEditBy,
    this.active,
    this.sortOrder,
    this.userType,
    this.userId,
  });

  factory TriageDataDetails.fromJson(Map<String, dynamic> json) {
    return TriageDataDetails(
      question: json['question'] != null ? TriageQuestion.fromJson(json['question']) : null,
      conditions: json['conditions'] != null ? (json['conditions'] as List).map((item) => TriageCondition.fromJson(item)).toList() : null,
      hasEmergencyEvidence: json['has_emergency_evidence'],
      shouldStop: json['should_stop'],
      interviewToken: json['interview_token'],
      message: json['Message'],
      errorList: json['ErrorList'] != null ? List<String>.from(json['ErrorList']) : null,
      id: json['Id'],
      language: json['language'],
      generalId: json['generalId'],
      createDate: json['CreateDate'],
      lastEditDate: json['LastEditDate'],
      createdBy: json['CreatedBy'],
      lastEditBy: json['LastEditBy'],
      active: json['Active'],
      sortOrder: json['SortOrder'],
      userType: json['userType'],
      userId: json['userId'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'question': question?.toJson(),
      'conditions': conditions?.map((item) => item.toJson()).toList(),
      'has_emergency_evidence': hasEmergencyEvidence,
      'should_stop': shouldStop,
      'interview_token': interviewToken,
      'Message': message,
      'ErrorList': errorList,
      'Id': id,
      'language': language,
      'generalId': generalId,
      'CreateDate': createDate,
      'LastEditDate': lastEditDate,
      'CreatedBy': createdBy,
      'LastEditBy': lastEditBy,
      'Active': active,
      'SortOrder': sortOrder,
      'userType': userType,
      'userId': userId,
    };
  }
}

class TriageQuestion {
  final int? type;
  final String? text;
  final String? explication;
  final List<String>? instruction;
  final List<TriageQuestionItem>? items;

  TriageQuestion({
    this.type,
    this.text,
    this.explication,
    this.instruction,
    this.items,
  });

  factory TriageQuestion.fromJson(Map<String, dynamic> json) {
    // TESTING: Add dummy instructions to every question for testing UI

    return TriageQuestion(
      type: json['type'],
      text: json['text'],
      explication: json['explication'],
      // Use dummy if null
      instruction: json['instruction'] != null ? List<String>.from(json['instruction']) : [],
      // Use dummy if null
      items: json['items'] != null ? (json['items'] as List).map((item) => TriageQuestionItem.fromJson(item)).toList() : null,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'type': type,
      'text': text,
      'explication': explication,
      'instruction': instruction,
      'items': items?.map((item) => item.toJson()).toList(),
    };
  }

  /// Check if instruction OR explication is available
  bool get hasInstructions {
    final hasInstruction = instruction != null && instruction!.isNotEmpty;
    final hasExplication = explication != null && explication!.isNotEmpty;
    return hasInstruction || hasExplication;
  }
}

class TriageQuestionItem {
  final String? id;
  final String? name;
  final List<TriageChoice>? choices;

  TriageQuestionItem({
    this.id,
    this.name,
    this.choices,
  });

  factory TriageQuestionItem.fromJson(Map<String, dynamic> json) {
    return TriageQuestionItem(
      id: json['id'],
      name: json['name'],
      choices: json['choices'] != null ? (json['choices'] as List).map((item) => TriageChoice.fromJson(item)).toList() : null,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'choices': choices?.map((item) => item.toJson()).toList(),
    };
  }
}

class TriageChoice {
  final String? id;
  final String? label;

  TriageChoice({
    this.id,
    this.label,
  });

  factory TriageChoice.fromJson(Map<String, dynamic> json) {
    return TriageChoice(
      id: json['id'],
      label: json['label'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'label': label,
    };
  }
}

class TriageCondition {
  final String? id;
  final String? name;
  final String? commonName;
  final String? nameAr;
  final String? commonNameAr;
  final double? probability;
  final ConditionDetails? conditionDetails;

  TriageCondition({
    this.id,
    this.name,
    this.commonName,
    this.nameAr,
    this.commonNameAr,
    this.probability,
    this.conditionDetails,
  });

  factory TriageCondition.fromJson(Map<String, dynamic> json) {
    return TriageCondition(
      id: json['id'],
      name: json['name'],
      commonName: json['common_name'],
      nameAr: json['nameAr'],
      commonNameAr: json['common_nameAr'],
      probability: json['probability']?.toDouble(),
      conditionDetails: json['condition_details'] != null ? ConditionDetails.fromJson(json['condition_details']) : null,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'common_name': commonName,
      'nameAr': nameAr,
      'common_nameAr': commonNameAr,
      'probability': probability,
      'condition_details': conditionDetails?.toJson(),
    };
  }

  /// Get display name based on locale
  String getDisplayName({bool isArabic = false}) {
    if (isArabic) {
      return commonNameAr ?? nameAr ?? commonName ?? name ?? '';
    }
    return commonName ?? name ?? '';
  }

  /// Get probability as percentage
  String getProbabilityPercentage() {
    if (probability == null) return '0%';
    return '${(probability! * 100).toStringAsFixed(1)}%';
  }
}

class ConditionDetails {
  final String? icd10Code;
  final ConditionCategory? category;
  final SeverityEnum? severity;
  final TriageLevelEnum? triageLevel;
  final String? hint;
  final bool? hasPatientEducation;
  final String? prevalence;
  final String? acuteness;

  ConditionDetails({
    this.icd10Code,
    this.category,
    this.severity,
    this.triageLevel,
    this.hint,
    this.hasPatientEducation,
    this.prevalence,
    this.acuteness,
  });

  factory ConditionDetails.fromJson(Map<String, dynamic> json) {
    return ConditionDetails(
      icd10Code: json['icd10_code'],
      category: json['category'] != null ? ConditionCategory.fromJson(json['category']) : null,
      severity: json['severity'] != null ? SeverityEnumExtension.fromInt(json['severity']) : null,
      triageLevel: json['triage_level'] != null ? TriageLevelEnumExtension.fromInt(json['triage_level']) : null,
      hint: json['hint'],
      hasPatientEducation: json['has_patient_education'],
      prevalence: json['prevalence'],
      acuteness: json['acuteness'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'icd10_code': icd10Code,
      'category': category?.toJson(),
      'severity': severity?.toInt,
      'triage_level': triageLevel?.toInt,
      'hint': hint,
      'has_patient_education': hasPatientEducation,
      'prevalence': prevalence,
      'acuteness': acuteness,
    };
  }
}

class ConditionCategory {
  final String? id;
  final String? name;

  ConditionCategory({
    this.id,
    this.name,
  });

  factory ConditionCategory.fromJson(Map<String, dynamic> json) {
    return ConditionCategory(
      id: json['id'],
      name: json['name'],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
    };
  }
}
