import 'package:flutter/foundation.dart';
import '../../data/models/session.dart';
import '../../data/models/target_analysis.dart';
import '../../data/models/weapon.dart';

class SessionProvider extends ChangeNotifier {
  DateTime? _sessionDate;
  DateTime? get sessionDate => _sessionDate;
  String? _currentWeaponName;
  String? _currentWeaponId;
  int _shotsPerTarget = 5;
  int _distance = 25;
  String? _activeSessionId;
  final List<TargetAnalysis> _currentAnalyses = [];
  bool _isSessionActive = false;

  String? get currentWeapon => _currentWeaponName;
  String? get currentWeaponId => _currentWeaponId;
  int get shotsPerTarget => _shotsPerTarget;
  int get distance => _distance;
  bool get isSessionActive => _isSessionActive;
  String? get activeSessionId => _activeSessionId;
  List<TargetAnalysis> get currentAnalyses => List.unmodifiable(_currentAnalyses);
  
  int get totalSessionScore => _currentAnalyses.fold(0, (sum, a) => sum + a.totalScore);
  int get targetCount => _currentAnalyses.length;

  void startSession(String weaponName, int shots, String sessionId, {String? weaponId, int distance = 25, DateTime? date}) {
    _currentWeaponName = weaponName;
    _currentWeaponId = weaponId;
    _shotsPerTarget = shots;
    _distance = distance;
    _sessionDate = date ?? DateTime.now();
    _activeSessionId = sessionId;
    _currentAnalyses.clear();
    _isSessionActive = true;
    notifyListeners();
  }

  void addAnalysis(TargetAnalysis analysis) {
    _currentAnalyses.add(analysis);
    notifyListeners();
  }

  void endSession() {
    _isSessionActive = false;
    _activeSessionId = null;
    _currentWeaponId = null;
    _currentWeaponName = null;
    _currentAnalyses.clear();
    _distance = 25;
    notifyListeners();
  }
}
