import Mathlib
import Development.AbelianVarietyTheory.Definitions.Divisors.FormalEnd
import Development.AbelianVarietyTheory.Proofs.DivisorProofs.SecRatioEqRefl
import Development.AbelianVarietyTheory.PublishedTheorems.AutomorphicForms₂.MumfordConstruction
import Development.AbelianVarietyTheory.Definitions.Degenerations.AdmissibleBaseOfCompleteDvr
import Development.AbelianVarietyTheory.Proofs.DivisorProofs.GermRatioAgreeSelf
import Development.AbelianVarietyTheory.Proofs.DivisorProofs.AppInjectiveOfIsDominant
import Development.AbelianVarietyTheory.Proofs.DivisorProofs.ExistsThetaImmersionTorusExtensionProjectiveStatement
import Development.AbelianVarietyTheory.Proofs.DivisorProofs.ExistsIntegralThetaFamilyPrescribedLeadingTermStatement
import Development.AbelianVarietyTheory.Proofs.DivisorProofs.ExistsExpandsAtSectionUniformised
import Development.AbelianVarietyTheory.Proofs.ThetaShiftProofs.ThickenedPointToTorusExtensionGenerator
import Development.AbelianVarietyTheory.Proofs.DivisorProofs.ClosedFibreSectionValueEqUnitMulFourierTermStatement
import Development.AbelianVarietyTheory.Proofs.CovolumeBounds.IsProjOverMorphismRestrictOfRelAmple
import Development.AbelianVarietyTheory.Proofs.DivisorProofs.ExistsEtaleCoordsLiftImmersion
open AlgebraicGeometry CategoryTheory Limits MonoidalCategory CartesianMonoidalCategory MonObj IsLocalRing NumberField
open ReciprocityTheory.ChartRestriction.G ReciprocityTheory.ChartRestriction.G.Semi ReciprocityTheory.ChartRestriction.G.Out ReciprocityTheory.ChartRestriction.G.Geo.Tor
open scoped CategoryTheory.Obj
attribute [local instance] Over.cartesianMonoidalCategory
open GlobalTheory.Mum

namespace GlobalTheory.Mum.ClosedFibreImm

theorem main : ∀ (R : Type) [CommRing R] [IsDomain R] [IsLocalRing R] (D : DegData R (maximalIdeal R)) (A : PolAV (Sp (FractionRing R))) (G : Over (Sp R)) [GrpObj G] [IsCommMonObj G] (U : UnifPre (maximalIdeal R) D A G) (Y : Ext D.toPre) (ψ : Over.mk ((thickObj R (maximalIdeal R) 0 G).hom ≫ thick R (maximalIdeal R) 0) ⟶ Y.H), GlobalTheory.TorusCoord.IsPointOf (U.gt 0) (𝟙 (thickObj R (maximalIdeal R) 0 G).left) (Y.Φ _ ψ) → IsImmersion ψ.left := by
  intro R _ _ _ D A G _ _ U Y ψ h
  set g := U.gt 0 with hg
  let G₀ := thickObj R (maximalIdeal R) 0 G
  let T : Over (Sp R) := Over.mk (G₀.hom ≫ thick R (maximalIdeal R) 0)
  have hcl : IsClosedImmersion (thick R (maximalIdeal R) 0) :=
    IsClosedImmersion.spec_of_surjective _ Ideal.Quotient.mk_surjective
  let T₀ : Over (Sp R) := Over.mk (pullback.fst Y.H.hom (thick R (maximalIdeal R) 0) ≫ Y.H.hom)
  let ξ := Y.Φ T₀ (Over.homMk (pullback.fst Y.H.hom (thick R (maximalIdeal R) 0)) rfl)
  obtain ⟨χ, hχ⟩ := GlobalTheory.TorusCoord.exists_point g (T := T₀) (pullback.snd Y.H.hom (thick R (maximalIdeal R) 0))
    pullback.condition.symm ξ
  let ψ' : G₀.left ⟶ pullback Y.H.hom (thick R (maximalIdeal R) 0) := pullback.lift ψ.left G₀.hom (Over.w ψ)
  have hψ' : ψ' ≫ pullback.fst _ _ = ψ.left := pullback.lift_fst _ _ _

  have hw1 : ψ' ≫ pullback.fst Y.H.hom (thick R (maximalIdeal R) 0) ≫ Y.H.hom = T.hom := by
    rw [← Category.assoc, hψ']; exact Over.w ψ
  let k : T ⟶ T₀ := Over.homMk ψ' hw1
  have hkx : k ≫ (Over.homMk (pullback.fst Y.H.hom (thick R (maximalIdeal R) 0)) rfl : T₀ ⟶ Y.H) = ψ :=
    Over.OverMorphism.ext (by simpa [k] using hψ')
  have hpull := Y.rep.1 T T₀ k (Over.homMk (pullback.fst Y.H.hom (thick R (maximalIdeal R) 0)) rfl)
  rw [hkx] at hpull
  have h1 : GlobalTheory.TorusCoord.IsPointOf g (ψ' ≫ χ) (Y.Φ T ψ) := hχ.pull g hpull
  have e1 : ψ' ≫ χ = 𝟙 _ := h1.unique_left g h
  have hc := hχ.comp_hom g
  have hw2 : χ ≫ T.hom = T₀.hom := hc
  let k' : T₀ ⟶ T := Over.homMk χ hw2
  have hpull' := Y.rep.1 T₀ T k' ψ
  have h2 : GlobalTheory.TorusCoord.IsPointOf g (χ ≫ 𝟙 _) (Y.Φ T₀ (k' ≫ ψ)) := h.pull g hpull'
  rw [Category.comp_id] at h2
  have e2 : Y.Φ T₀ (k' ≫ ψ) = ξ := h2.unique_right g hχ
  have e3 : k' ≫ ψ = Over.homMk (pullback.fst Y.H.hom (thick R (maximalIdeal R) 0)) rfl := (Y.Φ T₀).injective e2
  have e4 : χ ≫ ψ.left = pullback.fst Y.H.hom (thick R (maximalIdeal R) 0) := congrArg (·.left) e3
  have e5 : χ ≫ G₀.hom = pullback.snd Y.H.hom (thick R (maximalIdeal R) 0) := by
    rw [← cancel_mono (thick R (maximalIdeal R) 0), Category.assoc, ← pullback.condition]
    exact hc
  have e6 : χ ≫ ψ' = 𝟙 _ := by
    apply pullback.hom_ext
    · rw [Category.assoc, hψ', e4, Category.id_comp]
    · rw [Category.assoc, pullback.lift_snd, e5, Category.id_comp]
  have : IsIso ψ' := ⟨⟨χ, e1, e6⟩⟩
  rw [← hψ']
  infer_instance

end GlobalTheory.Mum.ClosedFibreImm
