// lib/services/air_api_service.dart

import 'dart:io';
import 'package:environment_monitoring_app/models/air_collection_data.dart';
import 'package:environment_monitoring_app/models/air_installation_data.dart';
import 'package:environment_monitoring_app/services/base_api_service.dart';
import 'package:environment_monitoring_app/services/server_config_service.dart';
import 'package:environment_monitoring_app/services/telegram_service.dart';

class AirApiService {
  final BaseApiService _baseService;
  final TelegramService? _telegramService; // Kept optional for now
  final ServerConfigService _serverConfigService;

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

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

  Future<Map<String, dynamic>> getClients() async {
    final baseUrl = await _serverConfigService.getActiveApiUrl();
    return _baseService.get(baseUrl, 'air/clients');
  }

  // NOTE: Air submission logic is likely in AirSamplingService and might use generic services.
  // These specific methods might be legacy or used differently. Keep them for now.
  Future<Map<String, dynamic>> submitInstallation(AirInstallationData data) async {
    final baseUrl = await _serverConfigService.getActiveApiUrl();
    return _baseService.post(baseUrl, 'air/manual/installation', data.toJsonForApi());
  }

  Future<Map<String, dynamic>> submitCollection(AirCollectionData data) async {
    final baseUrl = await _serverConfigService.getActiveApiUrl();
    return _baseService.post(baseUrl, 'air/manual/collection', data.toJson());
  }

  Future<Map<String, dynamic>> uploadInstallationImages({
    required String airManId,
    required Map<String, File> files,
  }) async {
    final baseUrl = await _serverConfigService.getActiveApiUrl();
    return _baseService.postMultipart(
      baseUrl: baseUrl,
      endpoint: 'air/manual/installation-images',
      fields: {'air_man_id': airManId},
      files: files,
    );
  }

  Future<Map<String, dynamic>> uploadCollectionImages({
    required String airManId,
    required Map<String, File> files,
  }) async {
    final baseUrl = await _serverConfigService.getActiveApiUrl();
    return _baseService.postMultipart(
      baseUrl: baseUrl,
      endpoint: 'air/manual/collection-images',
      fields: {'air_man_id': airManId},
      files: files,
    );
  }
}