import 'package:tugas_akhir_supabase/domain/entities/diagnosis_result.dart';

class DiagnosisResultModel extends DiagnosisResult {
  const DiagnosisResultModel({
    required String plantSpecies,
    required bool isHealthy,
    required String diseaseName,
    required String scientificName,
    required double confidenceValue,
    required String symptoms,
    required String causes,
    required List<String> preventionMeasures,
    required String organicTreatment,
    required String chemicalTreatment,
    required AdditionalInfoModel additionalInfo,
    Map<String, dynamic> environmentalData = const {},
    Map<String, dynamic> plantData = const {},
    Map<String, dynamic> treatmentSchedule = const {},
    Map<String, dynamic> economicImpact = const {},
    List<Map<String, dynamic>> alternativeVarieties = const [],
  }) : super(
          plantSpecies: plantSpecies,
          isHealthy: isHealthy,
          diseaseName: diseaseName,
          scientificName: scientificName,
          confidenceValue: confidenceValue,
          symptoms: symptoms,
          causes: causes,
          preventionMeasures: preventionMeasures,
          organicTreatment: organicTreatment,
          chemicalTreatment: chemicalTreatment,
          additionalInfo: additionalInfo,
          environmentalData: environmentalData,
          plantData: plantData,
          treatmentSchedule: treatmentSchedule,
          economicImpact: economicImpact,
          alternativeVarieties: alternativeVarieties,
        );

  factory DiagnosisResultModel.fromJson(Map<String, dynamic> json) {
    return DiagnosisResultModel(
      plantSpecies: json['plant_species'] ?? 'Unknown Plant',
      isHealthy: json['is_healthy'] ?? true,
      diseaseName: json['disease_name'] ?? '',
      scientificName: json['scientific_name'] ?? '',
      confidenceValue: (json['confidence_value'] ?? 0.0).toDouble(),
      symptoms: (json['symptoms'] is List)
          ? (json['symptoms'] as List).join(', ')
          : (json['symptoms'] ?? 'Tidak ada gejala terdeteksi'),
      causes: (json['causes'] is List)
          ? (json['causes'] as List).join(', ')
          : (json['causes'] ?? 'Tidak ada penyebab teridentifikasi'),
      preventionMeasures: (json['prevention_measures'] is List)
          ? List<String>.from(json['prevention_measures'])
          : (json['prevention_measures'] is String)
              ? [json['prevention_measures']]
              : [],
      organicTreatment: (json['organic_treatment'] is List)
          ? (json['organic_treatment'] as List).join(', ')
          : (json['organic_treatment'] ?? 'Tidak ada pengobatan organik'),
      chemicalTreatment: (json['chemical_treatment'] is List)
          ? (json['chemical_treatment'] as List).join(', ')
          : (json['chemical_treatment'] ?? 'Tidak ada pengobatan kimia'),
      additionalInfo: AdditionalInfoModel.fromJson(
          json['additional_info'] ?? {}),
      environmentalData: json['environmental_data'] ?? {},
      plantData: json['plant_data'] ?? {},
      treatmentSchedule: json['treatment_schedule'] ?? {},
      economicImpact: json['economic_impact'] ?? {},
      alternativeVarieties: (json['alternative_varieties'] is List)
          ? List<Map<String, dynamic>>.from(json['alternative_varieties'])
          : [],
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'plant_species': plantSpecies,
      'is_healthy': isHealthy,
      'disease_name': diseaseName,
      'scientific_name': scientificName,
      'confidence_value': confidenceValue,
      'symptoms': symptoms,
      'causes': causes,
      'prevention_measures': preventionMeasures,
      'organic_treatment': organicTreatment,
      'chemical_treatment': chemicalTreatment,
      'additional_info': (additionalInfo as AdditionalInfoModel).toJson(),
      'environmental_data': environmentalData,
      'plant_data': plantData,
      'treatment_schedule': treatmentSchedule,
      'economic_impact': economicImpact,
      'alternative_varieties': alternativeVarieties,
    };
  }

  factory DiagnosisResultModel.fromEntity(DiagnosisResult entity) {
    return DiagnosisResultModel(
      plantSpecies: entity.plantSpecies,
      isHealthy: entity.isHealthy,
      diseaseName: entity.diseaseName,
      scientificName: entity.scientificName,
      confidenceValue: entity.confidenceValue,
      symptoms: entity.symptoms,
      causes: entity.causes,
      preventionMeasures: entity.preventionMeasures,
      organicTreatment: entity.organicTreatment,
      chemicalTreatment: entity.chemicalTreatment,
      additionalInfo: AdditionalInfoModel.fromEntity(entity.additionalInfo),
      environmentalData: entity.environmentalData,
      plantData: entity.plantData,
      treatmentSchedule: entity.treatmentSchedule,
      economicImpact: entity.economicImpact,
      alternativeVarieties: entity.alternativeVarieties,
    );
  }
}

class AdditionalInfoModel extends AdditionalInfo {
  const AdditionalInfoModel({
    required String severity,
    required String spreadRate,
    required List<String> affectedParts,
    required String environmentalConditions,
  }) : super(
          severity: severity,
          spreadRate: spreadRate,
          affectedParts: affectedParts,
          environmentalConditions: environmentalConditions,
        );

  factory AdditionalInfoModel.fromJson(Map<String, dynamic> json) {
    return AdditionalInfoModel(
      severity: json['severity'] ?? 'Tidak diketahui',
      spreadRate: json['spread_rate'] ?? 'Tidak diketahui',
      affectedParts: (json['affected_parts'] is List)
          ? List<String>.from(json['affected_parts'])
          : (json['affected_parts'] is String)
              ? [json['affected_parts']]
              : [],
      environmentalConditions:
          json['environmental_conditions'] ?? 'Tidak diketahui',
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'severity': severity,
      'spread_rate': spreadRate,
      'affected_parts': affectedParts,
      'environmental_conditions': environmentalConditions,
    };
  }

  factory AdditionalInfoModel.fromEntity(AdditionalInfo entity) {
    return AdditionalInfoModel(
      severity: entity.severity,
      spreadRate: entity.spreadRate,
      affectedParts: entity.affectedParts,
      environmentalConditions: entity.environmentalConditions,
    );
  }
} 