// lib/services/river_api_service.dart

import 'dart:convert'; // <-- ADDED for jsonEncode
import 'package:intl/intl.dart';
import 'package:flutter/foundation.dart';
import 'package:environment_monitoring_app/services/base_api_service.dart';
import 'package:environment_monitoring_app/services/telegram_service.dart';
import 'package:environment_monitoring_app/services/server_config_service.dart';

class RiverApiService {
  final BaseApiService _baseService;
  final TelegramService _telegramService;
  final ServerConfigService _serverConfigService;

  RiverApiService(this._baseService, this._telegramService, this._serverConfigService);

  Future<Map<String, dynamic>> getManualStations() async {
    final baseUrl = await _serverConfigService.getActiveApiUrl();
    return _baseService.get(baseUrl, 'river/manual-stations');
  }

  Future<Map<String, dynamic>> getTriennialStations() async {
    final baseUrl = await _serverConfigService.getActiveApiUrl();
    return _baseService.get(baseUrl, 'river/triennial-stations');
  }

  // --- START: MODIFIED METHOD ---
  Future<Map<String, dynamic>> getRiverSamplingImages({
    required int stationId,
    required DateTime samplingDate,
    required String samplingType, // This parameter is now USED
  }) async {
    final baseUrl = await _serverConfigService.getActiveApiUrl();
    final String dateStr = DateFormat('yyyy-MM-dd').format(samplingDate);

    // Dynamically determine the API path based on sampling type
    String apiPath;
    if (samplingType == 'In-Situ Sampling') {
      apiPath = 'river/manual/images-by-station';
    } else if (samplingType == 'Triennial Sampling') {
      apiPath = 'river/triennial/images-by-station';
    } else if (samplingType == 'Investigative Sampling') { // <-- ADDED
      apiPath = 'river-investigative/images-by-station'; // <-- ADDED (Points to new controller)
    } else {
      // Fallback or error
      debugPrint("Unknown samplingType for image request: $samplingType");
      apiPath = 'river/manual/images-by-station'; // Default fallback
    }

    // Build the final endpoint with the correct path
    final String endpoint = '$apiPath?station_id=$stationId&date=$dateStr';

    debugPrint("ApiService: Calling river image request API endpoint: $endpoint");

    final response = await _baseService.get(baseUrl, endpoint);
    return response;
  }
  // --- END: MODIFIED METHOD ---

  Future<Map<String, dynamic>> sendImageRequestEmail({
    required String recipientEmail,
    required List<String> imageUrls,
    required String stationName,
    required String samplingDate,
  }) async {
    final baseUrl = await _serverConfigService.getActiveApiUrl();
    final Map<String, String> fields = {
      'recipientEmail': recipientEmail,
      'imageUrls': jsonEncode(imageUrls),
      'stationName': stationName,
      'samplingDate': samplingDate,
    };

    return _baseService.postMultipart(
      baseUrl: baseUrl,
      endpoint: 'river/images/send-email', // Endpoint for river email requests
      fields: fields,
      files: {},
    );
  }
// --- END: ADDED MISSING METHODS ---
}