// SPDX-FileCopyrightText: © 2025 Nøkken.io <nokken.io@proton.me>
// SPDX-License-Identifier: AGPL-3.0
//
//  medical_providers_repository.dart
//  Repository for accessing doctor and pharmacy autocomplete data
//
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:nokken/src/core/services/database/database_service.dart';
import 'package:nokken/src/features/medication_tracker/providers/medication_state.dart';

/// Repository class for accessing and managing medical provider data
class MedicalProvidersRepository {
  final DatabaseService _databaseService;

  MedicalProvidersRepository({required DatabaseService databaseService})
      : _databaseService = databaseService;

  /// Get all unique doctor names from medications and bloodwork records
  Future<List<String>> getAllDoctors() async {
    final Set<String> doctors = {};

    try {
      // Get doctors from medications
      final medications = await _databaseService.getAllMedications();
      for (final med in medications) {
        if (med.doctor != null && med.doctor!.trim().isNotEmpty) {
          doctors.add(med.doctor!);
        }
      }

      // Get doctors from bloodwork
      final bloodworks = await _databaseService.getAllBloodwork();
      for (final bloodwork in bloodworks) {
        if (bloodwork.doctor != null && bloodwork.doctor!.trim().isNotEmpty) {
          doctors.add(bloodwork.doctor!);
        }
      }

      // Sort alphabetically
      final sortedDoctors = doctors.toList()..sort();
      return sortedDoctors;
    } catch (e) {
      return [];
    }
  }

  /// Get all unique pharmacy names from medications
  Future<List<String>> getAllPharmacies() async {
    final Set<String> pharmacies = {};

    try {
      // Get pharmacies from medications
      final medications = await _databaseService.getAllMedications();
      for (final med in medications) {
        if (med.pharmacy != null && med.pharmacy!.trim().isNotEmpty) {
          pharmacies.add(med.pharmacy!);
        }
      }

      // Sort alphabetically
      final sortedPharmacies = pharmacies.toList()..sort();
      return sortedPharmacies;
    } catch (e) {
      return [];
    }
  }

  /// Get all unique locations from bloodwork records
  Future<List<String>> getAllLocations() async {
    final Set<String> locations = {};

    try {
      // Get locations from bloodwork
      final bloodworks = await _databaseService.getAllBloodwork();
      for (final bloodwork in bloodworks) {
        if (bloodwork.location != null &&
            bloodwork.location!.trim().isNotEmpty) {
          locations.add(bloodwork.location!);
        }
      }

      // Sort alphabetically
      final sortedLocations = locations.toList()..sort();
      return sortedLocations;
    } catch (e) {
      return [];
    }
  }
}

/// Provider for the medical providers repository
final medicalProvidersRepositoryProvider =
    Provider<MedicalProvidersRepository>((ref) {
  final databaseService = ref.watch(databaseServiceProvider);
  return MedicalProvidersRepository(databaseService: databaseService);
});

/// Provider for getting all unique doctor names
final doctorsProvider = FutureProvider<List<String>>((ref) async {
  final repository = ref.watch(medicalProvidersRepositoryProvider);
  return repository.getAllDoctors();
});

/// Provider for getting all unique pharmacy names
final pharmaciesProvider = FutureProvider<List<String>>((ref) async {
  final repository = ref.watch(medicalProvidersRepositoryProvider);
  return repository.getAllPharmacies();
});

/// Provider for getting all unique locations
final locationsProvider = FutureProvider<List<String>>((ref) async {
  final repository = ref.watch(medicalProvidersRepositoryProvider);
  return repository.getAllLocations();
});
