// lib/models/river_manual_triennial_sampling_data.dart

import 'dart:io';
import 'dart:convert';

class RiverManualTriennialSamplingData {
  // --- 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? eventRemarks;
  String? labRemarks;

  File? backgroundStationImage;
  File? upstreamRiverImage;
  File? downstreamRiverImage;

  // --- Step 4: Additional Photos ---
  File? sampleTurbidityImage;

  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? ammonia;
  double? batteryVoltage;

  // --- ADDED: Missing flowrate properties ---
  String? flowrateMethod;
  double? flowrateSurfaceDrifterHeight;
  double? flowrateSurfaceDrifterDistance;
  String? flowrateSurfaceDrifterTimeFirst;
  String? flowrateSurfaceDrifterTimeLast;
  double? flowrateValue;

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

  RiverManualTriennialSamplingData({
    this.samplingDate,
    this.samplingTime,
  });

  Map<String, String> toApiFormData() {
    final Map<String, String> map = {};

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

    add('first_sampler_user_id', firstSamplerUserId);
    add('r_tri_second_sampler_id', secondSampler?['user_id']);
    add('r_tri_date', samplingDate);
    add('r_tri_time', samplingTime);
    add('r_tri_type', samplingType);
    add('r_tri_sample_id_code', sampleIdCode);
    add('station_id', selectedStation?['station_id']);
    add('r_tri_current_latitude', currentLatitude);
    add('r_tri_current_longitude', currentLongitude);
    add('r_tri_distance_difference', distanceDifferenceInKm);
    add('r_tri_distance_difference_remarks', distanceDifferenceRemarks);
    add('r_tri_weather', weather);
    add('r_tri_event_remark', eventRemarks);
    add('r_tri_lab_remark', labRemarks);
    add('r_tri_optional_photo_01_remarks', optionalRemark1);
    add('r_tri_optional_photo_02_remarks', optionalRemark2);
    add('r_tri_optional_photo_03_remarks', optionalRemark3);
    add('r_tri_optional_photo_04_remarks', optionalRemark4);
    add('r_tri_sondeID', sondeId);
    add('data_capture_date', dataCaptureDate);
    add('data_capture_time', dataCaptureTime);
    add('r_tri_oxygen_conc', oxygenConcentration);
    add('r_tri_oxygen_sat', oxygenSaturation);
    add('r_tri_ph', ph);
    add('r_tri_salinity', salinity);
    add('r_tri_conductivity', electricalConductivity);
    add('r_tri_temperature', temperature);
    add('r_tri_tds', tds);
    add('r_tri_turbidity', turbidity);
    add('r_tri_ammonia', ammonia);
    add('r_tri_battery_volt', batteryVoltage);
    add('r_tri_flowrate_method', flowrateMethod);
    add('r_tri_flowrate_sd_height', flowrateSurfaceDrifterHeight);
    add('r_tri_flowrate_sd_distance', flowrateSurfaceDrifterDistance);
    add('r_tri_flowrate_sd_time_first', flowrateSurfaceDrifterTimeFirst);
    add('r_tri_flowrate_sd_time_last', flowrateSurfaceDrifterTimeLast);
    add('r_tri_flowrate_value', flowrateValue);
    add('first_sampler_name', firstSamplerName);
    add('r_tri_station_code', selectedStation?['sampling_station_code']);
    add('r_tri_station_name', selectedStation?['sampling_river']);

    return map;
  }

  Map<String, File?> toApiImageFiles() {
    return {
      'r_tri_background_station': backgroundStationImage,
      'r_tri_upstream_river': upstreamRiverImage,
      'r_tri_downstream_river': downstreamRiverImage,
      'r_tri_sample_turbidity': sampleTurbidityImage,
      'r_tri_optional_photo_01': optionalImage1,
      'r_tri_optional_photo_02': optionalImage2,
      'r_tri_optional_photo_03': optionalImage3,
      'r_tri_optional_photo_04': optionalImage4,
    };
  }

  // --- ADDED: Missing toMap() method ---
  Map<String, dynamic> toMap() {
    return {
      'firstSamplerName': firstSamplerName,
      'firstSamplerUserId': firstSamplerUserId,
      'secondSampler': secondSampler,
      'samplingDate': samplingDate,
      'samplingTime': samplingTime,
      'samplingType': samplingType,
      'sampleIdCode': sampleIdCode,
      'selectedStateName': selectedStateName,
      'selectedCategoryName': selectedCategoryName,
      'selectedStation': selectedStation,
      'stationLatitude': stationLatitude,
      'stationLongitude': stationLongitude,
      'currentLatitude': currentLatitude,
      'currentLongitude': currentLongitude,
      'distanceDifferenceInKm': distanceDifferenceInKm,
      'distanceDifferenceRemarks': distanceDifferenceRemarks,
      'weather': weather,
      'eventRemarks': eventRemarks,
      'labRemarks': labRemarks,
      'backgroundStationImage': backgroundStationImage?.path,
      'upstreamRiverImage': upstreamRiverImage?.path,
      'downstreamRiverImage': downstreamRiverImage?.path,
      'sampleTurbidityImage': sampleTurbidityImage?.path,
      'optionalImage1': optionalImage1?.path,
      'optionalRemark1': optionalRemark1,
      'optionalImage2': optionalImage2?.path,
      'optionalRemark2': optionalRemark2,
      'optionalImage3': optionalImage3?.path,
      'optionalRemark3': optionalRemark3,
      'optionalImage4': optionalImage4?.path,
      'optionalRemark4': optionalRemark4,
      'sondeId': sondeId,
      'dataCaptureDate': dataCaptureDate,
      'dataCaptureTime': dataCaptureTime,
      'oxygenConcentration': oxygenConcentration,
      'oxygenSaturation': oxygenSaturation,
      'ph': ph,
      'salinity': salinity,
      'electricalConductivity': electricalConductivity,
      'temperature': temperature,
      'tds': tds,
      'turbidity': turbidity,
      'ammonia': ammonia,
      'batteryVoltage': batteryVoltage,
      'flowrateMethod': flowrateMethod,
      'flowrateSurfaceDrifterHeight': flowrateSurfaceDrifterHeight,
      'flowrateSurfaceDrifterDistance': flowrateSurfaceDrifterDistance,
      'flowrateSurfaceDrifterTimeFirst': flowrateSurfaceDrifterTimeFirst,
      'flowrateSurfaceDrifterTimeLast': flowrateSurfaceDrifterTimeLast,
      'flowrateValue': flowrateValue,
      'submissionStatus': submissionStatus,
      'submissionMessage': submissionMessage,
      'reportId': reportId,
    };
  }
}