import 'package:cloud_firestore/cloud_firestore.dart';

class VigorManager {
  static const int maxVigor = 3;
  static const int rechargeHours = 3;

  static Future<int> getAvailableVigor(Map<String, dynamic> userData) async {
    int currentVigor = userData['vigor'] ?? maxVigor;
    Timestamp? lastVigorRefill = userData['lastVigorRefill'];

    if (lastVigorRefill == null) return currentVigor;

    // Calculate time since last refill
    DateTime lastTime = lastVigorRefill.toDate();
    Duration diff = DateTime.now().difference(lastTime);

    // If more than 3 hours have passed, full refill
    if (diff.inHours >= rechargeHours) {
      return maxVigor;
    }

    return currentVigor;
  }

  static Future<void> consumeVigor(String uid, int currentVigor) async {
    await FirebaseFirestore.instance.collection('users').doc(uid).update({
      'vigor': currentVigor - 1,
      'lastVigorRefill': FieldValue.serverTimestamp(),
    });
  }
}