import 'package:flutter/services.dart' show rootBundle;

import '../domain/seed_saving.dart';

const _asset = 'assets/catalog/seed_saving.json';

/// Loads and parses the bundled seed-saving catalog asset.
Future<SeedSavingData> loadBundledSeedSaving() async {
  return parseSeedSaving(await rootBundle.loadString(_asset));
}

/// App-wide access to the bundled seed-saving guidance. It's small, read-only
/// reference data (not per-user, never synced), so it lives as a single
/// in-memory instance set once at startup — no DB table, no migration. Tests
/// inject a parsed [SeedSavingData] directly via [data].
class SeedSavingCatalog {
  SeedSavingCatalog._();

  /// The loaded dataset, or null before [ensureLoaded] runs (then lookups just
  /// return null and the UI hides the section).
  static SeedSavingData? data;

  /// Loads the asset once. Safe to call repeatedly.
  static Future<void> ensureLoaded() async {
    data ??= await loadBundledSeedSaving();
  }

  /// The guide for a crop, or null when the catalog isn't loaded or nothing
  /// matches. See [SeedSavingData.guideFor].
  static SeedSavingGuide? guideFor({String? scientificName, String? family}) =>
      data?.guideFor(scientificName: scientificName, family: family);
}
