// lib/models/marine_manual_sonde_calibration_data.dart

class MarineManualSondeCalibrationData {
  int? calibratedByUserId;
  String? calibratedByUserName;

  // Header fields from PDF
  String? sondeSerialNumber;
  String? firmwareVersion;
  String? korVersion;
  String? location;
  String? startDateTime;
  String? endDateTime;

  // pH values (with Mv)
  double? ph7Mv;
  double? ph7Before;
  double? ph7After;
  double? ph10Mv;
  double? ph10Before;
  double? ph10After;

  // Other parameters
  double? condBefore;
  double? condAfter;
  double? doBefore;
  double? doAfter;
  double? turbidity0Before;
  double? turbidity0After;
  double? turbidity124Before;
  double? turbidity124After;

  String? calibrationStatus;
  String? remarks;

  String? submissionStatus;
  String? submissionMessage;
  String? reportId;

  Map<String, dynamic> toApiFormData() {
    return {
      'calibrated_by_user_id': calibratedByUserId.toString(),
      'sonde_serial_number': sondeSerialNumber,
      'firmware_version': firmwareVersion,
      'kor_version': korVersion,
      'location': location,
      'start_datetime': startDateTime,
      'end_datetime': endDateTime,
      'ph_7_mv': ph7Mv?.toString(),
      'ph_7_before': ph7Before?.toString(),
      'ph_7_after': ph7After?.toString(),
      'ph_10_mv': ph10Mv?.toString(),
      'ph_10_before': ph10Before?.toString(),
      'ph_10_after': ph10After?.toString(),
      'cond_before': condBefore?.toString(),
      'cond_after': condAfter?.toString(),
      'do_before': doBefore?.toString(),
      'do_after': doAfter?.toString(),
      'turbidity_0_before': turbidity0Before?.toString(),
      'turbidity_0_after': turbidity0After?.toString(),
      'turbidity_124_before': turbidity124Before?.toString(),
      'turbidity_124_after': turbidity124After?.toString(),
      'calibration_status': calibrationStatus,
      'remarks': remarks,
    };
  }

  /// Creates a JSON object for offline database storage.
  Map<String, dynamic> toDbJson() {
    return {
      'calibratedByUserId': calibratedByUserId,
      'calibratedByUserName': calibratedByUserName,
      'sondeSerialNumber': sondeSerialNumber,
      'firmwareVersion': firmwareVersion,
      'korVersion': korVersion,
      'location': location,
      'startDateTime': startDateTime,
      'endDateTime': endDateTime,
      'ph_7_mv': ph7Mv,
      'ph_7_before': ph7Before,
      'ph_7_after': ph7After,
      'ph_10_mv': ph10Mv,
      'ph_10_before': ph10Before,
      'ph_10_after': ph10After,
      'cond_before': condBefore,
      'cond_after': condAfter,
      'do_before': doBefore,
      'do_after': doAfter,
      'turbidity_0_before': turbidity0Before,
      'turbidity_0_after': turbidity0After,
      'turbidity_124_before': turbidity124Before,
      'turbidity_124_after': turbidity124After,
      'calibration_status': calibrationStatus,
      'remarks': remarks,
      'submissionStatus': submissionStatus,
      'submissionMessage': submissionMessage,
      'reportId': reportId,
    };
  }

  // --- START: ADDED TELEGRAM METHOD ---
  String generateTelegramAlertMessage() {
    final buffer = StringBuffer()
      ..writeln('⚖️ *Sonde Calibration Report Submitted*')
      ..writeln()
      ..writeln('*Calibrated By:* ${calibratedByUserName ?? "N/A"}')
      ..writeln('*Location:* ${location ?? "N/A"}')
      ..writeln('*Sonde Serial:* ${sondeSerialNumber ?? "N/A"}')
      ..writeln('*Start:* ${startDateTime ?? "N/A"}')
      ..writeln('*End:* ${endDateTime ?? "N/A"}');

    if (calibrationStatus != null && calibrationStatus!.isNotEmpty) {
      buffer.writeln('*Result:* $calibrationStatus');
    }

    if (remarks != null && remarks!.isNotEmpty) {
      buffer.writeln('\n*Remarks:* $remarks');
    }

    buffer
      ..writeln()
      ..writeln('*Status of Submission:* Successful');

    return buffer.toString();
  }
// --- END: ADDED TELEGRAM METHOD ---
}