"""The Deck — a shared-state equilibrium summary across Play and Lab modes. A 'current view of everything' built from the session catalog: counts, generations, stage/type breakdowns, trait averages, color gamut, recent crosses. """ from collections import defaultdict import lab def _mean(vals): vals = [v for v in vals if isinstance(v, (int, float))] return round(sum(vals) / len(vals), 1) if vals else 0.0 def summary(seeds): records = lab.germplasm_records(seeds) by_id = {r["germplasmDbId"]: r for r in records} by_stage = defaultdict(int) by_type = defaultdict(int) by_gen = defaultdict(int) by_family = defaultdict(int) for r in records: by_stage[r["stage"]] += 1 by_type[r["germplasmType"]] += 1 by_gen[r["generation"]] += 1 by_family[r["budFamily"]] += 1 breedable = [r for r in records if r["canAttempt"] and r["stage"] == "MATURE"] growing = [r for r in records if r["stage"] != "MATURE"] # recent crosses = newest offspring (have two real parents), best-effort order crosses = [r for r in records if all(r["parents"]) ] recent = [ { "id": r["germplasmDbId"], "name": r["germplasmName"], "generation": r["generation"], "pedigree": r["pedigree"], "budColor": r["budColor"], "leafColor": r["leafColor"], "THC": r["THC"], } for r in crosses[-6:][::-1] ] return { "accessions": len(records), "byStage": dict(by_stage), "byType": dict(sorted(by_type.items())), "byGeneration": {f"F{k}": v for k, v in sorted(by_gen.items())}, "byBudFamily": dict(sorted(by_family.items())), "maxGeneration": max(by_gen) if by_gen else 0, "breedableCount": len(breedable), "growingCount": len(growing), "traitAverages": { "THC": _mean([r["THC"] for r in records]), "CBD": _mean([r["CBD"] for r in records]), "Yield": _mean([r["Yield"] for r in records]), "GrowTime": _mean([r["GrowTime"] for r in records]), "Stability": _mean([r["Stability"] for r in records]), }, "gamut": [r["budColor"] for r in records], "recentCrosses": recent, "breedable": [{"id": r["germplasmDbId"], "name": r["germplasmName"]} for r in breedable], }