# Medication Tracker
The medication tracker feature allows users to manage their medications, track adherence, and receive refill alerts.


## Models
- `Medication`: Core model representing a medication with properties for name, dosage, schedule, inventory, and type-specific details.
- `MedicationDose`: Represents a specific instance of a medication taken at a particular date and time slot.
```dart
class MedicationDose {
  final String medicationId;
  final DateTime date;
  final String timeSlot;
  
  // Methods for generating unique keys
  String toKey() {...}
  static MedicationDose fromKey(String key) {...}
}
```
- `InjectionDetails`: Extends medication information with injection-specific details like needle types, site rotation, and supplies tracking.
- `InjectionSite`: Manages individual injection locations on the body.
- `InjectionSiteRotation`: Handles the automatic rotation of injection sites.


## Providers
- `medicationStateProvider`: Manages medication CRUD operations with the database.
- `medicationTakenProvider`: Tracks which medications have been taken on specific dates.
- Various derived providers:

```dart
// Main state providers
final medicationStateProvider = StateNotifierProvider<MedicationNotifier, MedicationState>
final medicationTakenProvider = StateNotifierProvider<MedicationTakenNotifier, Set<String>>

// Derived providers
final medicationsProvider = Provider<List<Medication>>
final medicationsLoadingProvider = Provider<bool>
final medicationsErrorProvider = Provider<String?>
final medicationsByNeedRefillProvider = Provider<List<Medication>>
final sortedMedicationsProvider = Provider<List<Medication>>
final groupedMedicationTypeProvider = Provider<Map<String, List<Medication>>>
```

### State Notifiers
```dart
class MedicationNotifier extends StateNotifier<MedicationState> {
  // Notifier methods
  Future<void> loadMedications() async {...}
  Future<void> addMedication(Medication medication) async {...}
  Future<void> updateMedication(Medication medication) async {...}
  Future<void> updateMedicationQuantity(Medication medication, bool taken) async {...}
  Future<void> deleteMedication(String id) async {...}
}
```
```dart
class MedicationTakenNotifier extends StateNotifier<Set<String>> {
  // Notifier methods
  Future<void> loadTakenMedicationsForDate(DateTime date) async {...}
  Future<void> setMedicationTaken(MedicationDose dose, bool taken, {String? customKey}) async {...}
  Future<void> clearOldData(int olderThanDays) async {...}
}
```

## Screens
- `MedicationListScreen`: Displays all medications with grouping by type and refill alerts.
- `MedicationDetailScreen`: Detailed view of medication information with adherence tracking.
- `AddEditMedicationScreen`: Form for adding and editing medications with type-specific fields.
- `InjectionSiteTrackerScreen`: Interface for managing and visualizing injection site rotation.
- `InjectionSiteViewerScreen`: Visualization of the current injection site on a body diagram.

## Interactions
- Uses `DatabaseService` for persistent storage of medication data
- Uses `NotificationService` for medication reminders
- Interacts with Scheduler feature to display medications on calendar and daily views
- Uses `ValidationService` to ensure data integrity