import Mathlib
import Development.AbelianVarietyTheory.Definitions.Divisors.PtAlgSpOver
import Development.AbelianVarietyTheory.PublishedTheorems.Divisors₂.AbelianHodgeDeRham
import Development.AbelianVarietyTheory.PublishedTheorems.Divisors₂.PoincareDeRhamDuality
import Development.AbelianVarietyTheory.PublishedTheorems.Divisors₂.CrysToDRBijective
import Development.AbelianVarietyTheory.Proofs.DivisorProofs.ExistsUniqueSectionOfNaturalPointwiseFunctionStatement
import Development.AbelianVarietyTheory.Proofs.DivisorProofs.DualNumberPointPreimageTopOfReduction
open CategoryTheory AlgebraicGeometry MonoidalCategory CartesianMonoidalCategory MonObj TensorProduct
open ReciprocityTheory.ChartRestriction.G.Geo.Tor (Sp spMap epsR IsTangent IsGrpPullback)
open ReciprocityTheory.ChartRestriction.G.Semi (IsAbelianScheme LB)
open GlobalTheory GlobalTheory.CatCoh GlobalTheory.Cris GlobalTheory.DeRham GlobalTheory.PDiv GlobalTheory.PtExt
attribute [local instance] Over.cartesianMonoidalCategory

set_option autoImplicit false
noncomputable section
namespace GlobalTheory.PtExt

theorem triv_pull_of_isPtGen : ∀ (R : Type) [CommRing R] (X Y : Over (Sp R)) (L : LB X.left), L.IsLB →
    ∀ (q : Y ⟶ X) (t : ∀ (E : Type) [CommRing E] [Algebra R E], Pt Y E → L.ι → E), IsPtGen L q t →
    GlobalTheory.AbLaw.Triv (L.pull q.left) := by
  intro R _ X Y L _ q t ht
  obtain ⟨hu, hn, hg⟩ := ht
  have hmul : ∀ {D : Type} [CommRing D] (x : Sp D ⟶ Y.left) (U : Y.left.Opens), ⊤ ≤ x ⁻¹ᵁ U →
      ∀ a b : Γ(Y.left, U), evU x U (a * b) = evU x U a * evU x U b := by
    intro D _ x U h a b; unfold evU; rw [dif_pos h, dif_pos h, dif_pos h, map_mul]
  have hone : ∀ {D : Type} [CommRing D] (x : Sp D ⟶ Y.left) (U : Y.left.Opens), ⊤ ≤ x ⁻¹ᵁ U →
      evU x U (1 : Γ(Y.left, U)) = 1 := by
    intro D _ x U h; unfold evU; rw [dif_pos h, map_one]
  have hch : ∀ (j : PUnit → L.ι) {E : Type} [CommRing E] [Algebra R E] (z : Pt Y E),
      ⊤ ≤ z.left ⁻¹ᵁ (L.pull q.left).U j → ⊤ ≤ (z ≫ q).left ⁻¹ᵁ L.U (j PUnit.unit) :=
    fun j E _ _ z h p hp ↦ LB.wchart_le L (K := PUnit) (fun _ ↦ q.left) j PUnit.unit (h hp)
  have huniq : ∀ (U : Y.left.Opens) (f : Fn0 Y), (∀ (E : Type) [CommRing E] [Algebra R E] (E' : Type) [CommRing E']
      [Algebra R E'] (ψ : E →ₐ[R] E') (z : Pt Y E), ⊤ ≤ z.left ⁻¹ᵁ U → f E' (spOver ψ ≫ z) = ψ (f E z)) →
      ∀ c c' : Γ(Y.left, U), (∀ (E : Type) [CommRing E] [Algebra R E] (z : Pt Y E), ⊤ ≤ z.left ⁻¹ᵁ U →
        evU z.left U c = f E z) → (∀ (E : Type) [CommRing E] [Algebra R E] (z : Pt Y E), ⊤ ≤ z.left ⁻¹ᵁ U →
        evU z.left U c' = f E z) → c = c' :=
    fun U f hf c c' hc hc' ↦ (GlobalTheory.T.exists_unique_section_of_natural_pointwise_function R Y U f hf).unique hc hc'
  have hnat : ∀ j : PUnit → L.ι, ∀ (E : Type) [CommRing E] [Algebra R E] (E' : Type) [CommRing E'] [Algebra R E']
      (ψ : E →ₐ[R] E') (z : Pt Y E), ⊤ ≤ z.left ⁻¹ᵁ (L.pull q.left).U j →
      t E' (spOver ψ ≫ z) (j PUnit.unit) = ψ (t E z (j PUnit.unit)) :=
    fun j E _ _ E' _ _ ψ z h ↦ hn E E' ψ z _ (hch j z h)
  have hnatI : ∀ j : PUnit → L.ι, ∀ (E : Type) [CommRing E] [Algebra R E] (E' : Type) [CommRing E'] [Algebra R E']
      (ψ : E →ₐ[R] E') (z : Pt Y E), ⊤ ≤ z.left ⁻¹ᵁ (L.pull q.left).U j →
      Ring.inverse (t E' (spOver ψ ≫ z) (j PUnit.unit)) = ψ (Ring.inverse (t E z (j PUnit.unit))) := by
    intro j E _ _ E' _ _ ψ z h
    rw [hnat j E E' ψ z h]
    obtain ⟨u, hu'⟩ := hu E z _ (hch j z h)
    rw [← hu', Ring.inverse_unit]
    have e : ψ (u : E) = ((Units.map (ψ : E →* E') u : E'ˣ) : E') := rfl
    rw [e, Ring.inverse_unit]
    rfl
  have ex := fun j : PUnit → L.ι ↦
    (GlobalTheory.T.exists_unique_section_of_natural_pointwise_function R Y ((L.pull q.left).U j) (fun E _ _ z ↦ t E z (j PUnit.unit)) (hnat j)).exists
  have exI := fun j : PUnit → L.ι ↦
    (GlobalTheory.T.exists_unique_section_of_natural_pointwise_function R Y ((L.pull q.left).U j) (fun E _ _ z ↦ Ring.inverse (t E z (j PUnit.unit))) (hnatI j)).exists
  choose a ha using ex
  choose b hb using exI
  have hab : ∀ j, a j * b j = 1 := by
    intro j
    refine huniq _ (fun _ _ _ _ ↦ 1) (fun E _ _ E' _ _ ψ z _ ↦ (map_one ψ).symm) _ _ ?_ ?_
    · intro E _ _ z h
      rw [hmul _ _ h, ha j E z h, hb j E z h]
      exact Ring.mul_inverse_cancel _ (hu E z _ (hch j z h))
    · intro E _ _ z h
      exact hone _ _ h
  refine ⟨⟨fun j ↦ ⟨a j, b j, hab j, by rw [mul_comm]; exact hab j⟩⟩, ?_⟩
  intro i j
  apply Units.ext
  have hVi : (L.pull q.left).U i ⊓ (L.pull q.left).U j ≤ (L.pull q.left).U i := inf_le_left
  have hVj : (L.pull q.left).U i ⊓ (L.pull q.left).U j ≤ (L.pull q.left).U j := inf_le_right
  refine huniq ((L.pull q.left).U i ⊓ (L.pull q.left).U j) (fun E _ _ z ↦ t E z (i PUnit.unit))
    (fun E _ _ E' _ _ ψ z h ↦ hnat i E E' ψ z (fun p hp ↦ hVi (h hp))) _ _ ?_ ?_
  · intro E _ _ z h
    have e1 := evalSectionAtPointOrZero_restriction Y.left E z.left _ _ hVi (a i) h
    refine Eq.trans ?_ ((e1.trans (ha i E z (fun p hp ↦ hVi (h hp)))))
    rfl
  · intro E _ _ z h
    have hzi : ⊤ ≤ z.left ⁻¹ᵁ (L.pull q.left).U i := fun p hp ↦ hVi (h hp)
    have hzj : ⊤ ≤ z.left ⁻¹ᵁ (L.pull q.left).U j := fun p hp ↦ hVj (h hp)
    have hij : ⊤ ≤ (z.left ≫ q.left) ⁻¹ᵁ (L.U (i PUnit.unit) ⊓ L.U (j PUnit.unit)) :=
      fun p hp ↦ ⟨hch i z hzi hp, hch j z hzj hp⟩
    rw [Units.val_mul, hmul _ _ h]
    have e2 := evalSectionAtPointOrZero_restriction Y.left E z.left _ _ hVj (a j) h
    have e2' : evU z.left _ (ReciprocityTheory.ChartRestriction.G.Semi.res hVj (⟨a j, b j, hab j, by rw [mul_comm]; exact hab j⟩ :
        Γ(Y.left, (L.pull q.left).U j)ˣ)).val = t E z (j PUnit.unit) :=
      e2.trans (ha j E z hzj)
    have e3 : evU z.left _ ((L.pull q.left).g i j).val =
        evU (z ≫ q).left (L.U (i PUnit.unit) ⊓ L.U (j PUnit.unit)) (L.g (i PUnit.unit) (j PUnit.unit)).val := by
      unfold evU
      rw [dif_pos h]
      have hij' : ⊤ ≤ (z ≫ q).left ⁻¹ᵁ (L.U (i PUnit.unit) ⊓ L.U (j PUnit.unit)) := hij
      rw [dif_pos hij']
      exact (evalAt_pull_g L q.left z.left i j hij h).symm
    rw [e3, e2']
    exact (hg E z (i PUnit.unit) (j PUnit.unit) (hch i z hzi) (hch j z hzj)).symm

end GlobalTheory.PtExt
