import 'dart:io';

/// A data model class to hold all information for the multi-step
/// In-Situ Sampling form.
class InSituSamplingData {
  // --- Step 1: Sampling & Station Info ---
  String? firstSamplerName;
  int? firstSamplerUserId;
  Map<String, dynamic>? secondSampler;
  String? samplingDate;
  String? samplingTime;
  String? samplingType;
  String? sampleIdCode;

  String? selectedStateName;
  String? selectedCategoryName;
  Map<String, dynamic>? selectedStation;

  String? stationLatitude;
  String? stationLongitude;
  String? currentLatitude;
  String? currentLongitude;
  double? distanceDifferenceInKm;
  String? distanceDifferenceRemarks;

  // --- Step 2: Site Info & Photos ---
  String? weather;
  String? tideLevel;
  String? seaCondition;
  String? eventRemarks;
  String? labRemarks;

  File? leftLandViewImage;
  File? rightLandViewImage;
  File? waterFillingImage;
  File? seawaterColorImage;
  File? phPaperImage;

  File? optionalImage1;
  String? optionalRemark1;
  File? optionalImage2;
  String? optionalRemark2;
  File? optionalImage3;
  String? optionalRemark3;
  File? optionalImage4;
  String? optionalRemark4;

  // --- Step 3: Data Capture ---
  String? sondeId;
  String? dataCaptureDate;
  String? dataCaptureTime;
  double? oxygenConcentration;
  double? oxygenSaturation;
  double? ph;
  double? salinity;
  double? electricalConductivity;
  double? temperature;
  double? tds;
  double? turbidity;
  double? tss;
  double? batteryVoltage;

  // --- Post-Submission Status ---
  String? submissionStatus;
  String? submissionMessage;
  String? reportId;

  // REPAIRED: Added a constructor to accept initial values.
  InSituSamplingData({
    this.samplingDate,
    this.samplingTime,
  });

  /// Converts the data model into a Map<String, String> for the API form data.
  Map<String, String> toApiFormData() {
    final Map<String, String> map = {};

    void add(String key, dynamic value) {
      if (value != null) {
        map[key] = value.toString();
      }
    }

    // Step 1 Data
    add('first_sampler_user_id', firstSamplerUserId);
    add('man_second_sampler_id', secondSampler?['user_id']);
    add('man_date', samplingDate);
    add('man_time', samplingTime);
    add('man_type', samplingType);
    add('man_sample_id_code', sampleIdCode);
    add('station_id', selectedStation?['station_id']);
    add('man_current_latitude', currentLatitude);
    add('man_current_longitude', currentLongitude);
    add('man_distance_difference', distanceDifferenceInKm);
    add('man_distance_difference_remarks', distanceDifferenceRemarks);

    // Step 2 Data
    add('man_weather', weather);
    add('man_tide_level', tideLevel);
    add('man_sea_condition', seaCondition);
    add('man_event_remark', eventRemarks);
    add('man_lab_remark', labRemarks);
    add('man_optional_photo_01_remarks', optionalRemark1);
    add('man_optional_photo_02_remarks', optionalRemark2);
    add('man_optional_photo_03_remarks', optionalRemark3);
    add('man_optional_photo_04_remarks', optionalRemark4);

    // Step 3 Data
    add('man_sondeID', sondeId);
    add('data_capture_date', dataCaptureDate);
    add('data_capture_time', dataCaptureTime);
    add('man_oxygen_conc', oxygenConcentration);
    add('man_oxygen_sat', oxygenSaturation);
    add('man_ph', ph);
    add('man_salinity', salinity);
    add('man_conductivity', electricalConductivity);
    add('man_temperature', temperature);
    add('man_tds', tds);
    add('man_turbidity', turbidity);
    add('man_tss', tss);
    add('man_battery_volt', batteryVoltage);

    add('first_sampler_name', firstSamplerName);
    add('man_station_code', selectedStation?['man_station_code']);
    add('man_station_name', selectedStation?['man_station_name']);

    return map;
  }

  /// Converts the image properties into a Map<String, File?> for the multipart API request.
  Map<String, File?> toApiImageFiles() {
    return {
      'man_left_side_land_view': leftLandViewImage,
      'man_right_side_land_view': rightLandViewImage,
      'man_filling_water_into_sample_bottle': waterFillingImage,
      'man_seawater_in_clear_glass_bottle': seawaterColorImage,
      'man_examine_preservative_ph_paper': phPaperImage,
      'man_optional_photo_01': optionalImage1,
      'man_optional_photo_02': optionalImage2,
      'man_optional_photo_03': optionalImage3,
      'man_optional_photo_04': optionalImage4,
    };
  }
}