// lib/state/booster_inventory_notifier.dart
import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../data/save_repository.dart';
import '../game/models/booster.dart';
import 'providers.dart';

/// Live booster counts backed by [SaveRepository]. State is an immutable map.
class BoosterInventoryNotifier extends Notifier<Map<BoosterType, int>> {
  SaveRepository get _save => ref.read(saveRepositoryProvider);

  @override
  Map<BoosterType, int> build() => _snapshot();

  Map<BoosterType, int> _snapshot() =>
      {for (final t in BoosterType.values) t: _save.boosterCount(t)};

  Future<void> grant(BoosterType type, [int n = 1]) async {
    await _save.grantBooster(type, n);
    state = _snapshot();
  }

  Future<bool> consume(BoosterType type) async {
    final ok = await _save.consumeBooster(type);
    if (ok) state = _snapshot();
    return ok;
  }
}
