import Mathlib
import Development.AbelianVarietyTheory.PublishedTheorems.Divisors₂.KawamataViehweg
import Development.AbelianVarietyTheory.PublishedTheorems.Divisors₂.IsLogCanonicalMultipleCanonical
import Development.AbelianVarietyTheory.Proofs.DivisorProofs.IsAffineOpenPreimageOfIsPullback
import Development.AbelianVarietyTheory.Definitions.Divisors.IsEtCoord
import Development.AbelianVarietyTheory.Definitions.Divisors.SameDiv
import Development.AbelianVarietyTheory.Proofs.CoherentCohomologyProofs.CechModulesFunctorShortExactOfIsAffineOpen
import Development.AbelianVarietyTheory.Proofs.CoherentCohomologyProofs.BijectiveToPushforwardComp
open CategoryTheory AlgebraicGeometry TopologicalSpace Limits
open ReciprocityTheory.ChartRestriction.G ReciprocityTheory.ChartRestriction.G.Semi ReciprocityTheory.ChartRestriction.G.Geo.Tor
open GlobalTheory GlobalTheory.CechPic GlobalTheory.QCoh GlobalTheory.LogPair
open scoped TensorProduct

namespace GlobalTheory

theorem coh_subsingleton_of_disjoint_cover : ∀ (X : Scheme.{0}) (M : LB X) (κ : Type) (U : κ → X.Opens), M.IsLB → ⨆ c, U c = ⊤ → (Pairwise fun a b => Disjoint (U a) (U b)) → ∀ q : ℕ, (∀ c, Subsingleton (coh (LB.toMod (M.pull (U c).ι)) q)) → Subsingleton (coh (LB.toMod M) q) := by
  intro X M κ U hM hc hd q h
  have hj : ∀ (i : κ) (r : ℤ), (U i).ι.appTop (algebraMap ℤ Γ(X, ⊤) r) =
      algebraMap ℤ Γ((U i).toScheme, ⊤) r := fun i r => by simp
  have hs : ∀ i, Subsingleton (coh ((LB.toMod M).restrict (U i).ι) q) := fun i =>
    (cohOn.mapIso ℤ ⊤ q ((Scheme.Modules.restrictFunctorIsoPullback (U i).ι).app _ ≪≫
      LB.pullbackIso hM (U i).ι)).toEquiv.subsingleton_congr.2 (h i)
  exact (coh.disjointRestrictEquiv ℤ (fun i => (U i).ι) hj (by simpa using hc)
    (by simpa using hd) (LB.toMod M) q).toEquiv.subsingleton

end GlobalTheory
