import '../../db/database.dart';

/// The current version of the interchange JSON format (envelope field
/// `formatVersion`). Bump only with additive, documented changes — readers
/// must reject files with a *newer* version and tolerate unknown fields in
/// the same version (data-model §5.2, §7).
const int inventoryFormatVersion = 1;

/// Identifies files produced by this app (envelope field `appId`).
const String inventoryAppId = 'tane';

/// An in-memory snapshot of the user's inventory, decoupled from any file
/// format. Produced by `VarietyRepository.exportInventory()` and consumed by
/// `importInventory()`; the JSON/CSV codecs turn it into bytes and back.
///
/// Rows are the plain Drift data classes (immutable). Tombstones
/// (`isDeleted = true`) are never part of a snapshot: this is a user-facing
/// export, not the future sync wire format. The bundled species catalog is
/// not included either — [speciesNamesById] carries just the scientific
/// names needed to re-link varieties on import (species ids are generated
/// per install and are not portable).
class InventorySnapshot {
  const InventorySnapshot({
    this.varieties = const [],
    this.lots = const [],
    this.vernacularNames = const [],
    this.externalLinks = const [],
    this.germinationTests = const [],
    this.conditionChecks = const [],
    this.movements = const [],
    this.parties = const [],
    this.attachments = const [],
    this.plantares = const [],
    this.sales = const [],
    this.speciesNamesById = const {},
  });

  final List<Variety> varieties;
  final List<Lot> lots;
  final List<VarietyVernacularName> vernacularNames;
  final List<ExternalLink> externalLinks;
  final List<GerminationTest> germinationTests;
  final List<ConditionCheck> conditionChecks;
  final List<Movement> movements;
  final List<Party> parties;
  final List<Attachment> attachments;
  final List<Plantare> plantares;
  final List<Sale> sales;

  /// speciesId → scientificName, for the species referenced by [varieties].
  final Map<String, String> speciesNamesById;
}

/// What an import did, for user feedback: rows added, rows overwritten by a
/// newer incoming version (LWW), and rows skipped (already present and at
/// least as recent, or not decodable).
class ImportSummary {
  const ImportSummary({this.inserted = 0, this.updated = 0, this.skipped = 0});

  final int inserted;
  final int updated;
  final int skipped;

  ImportSummary operator +(ImportSummary other) => ImportSummary(
    inserted: inserted + other.inserted,
    updated: updated + other.updated,
    skipped: skipped + other.skipped,
  );
}
