import Mathlib
import Development.AbelianVarietyTheory.Proofs.DivisorProofs.DescCocycle
open CategoryTheory CategoryTheory.Limits AlgebraicGeometry MonoidalCategory CartesianMonoidalCategory MonObj
open ReciprocityTheory.ChartRestriction.G ReciprocityTheory.ChartRestriction.G.Semi ReciprocityTheory.ChartRestriction.G.Geo.Tor GlobalTheory.AbLaw GlobalTheory.CechPic GlobalTheory.AbDual GlobalTheory.DualCon
open scoped CategoryTheory.Obj
attribute [local instance] Over.cartesianMonoidalCategory
open GlobalTheory.LBDescent

set_option autoImplicit false

namespace GlobalTheory.LBDescent

private theorem alg {G : Type*} [CommGroup G] (a P b G₁ G₃ : G) :
    a * P * b⁻¹ = G₁ * a * (G₁⁻¹ * P * G₃) * (G₃⁻¹ * b⁻¹) := by
  rw [mul_comm G₁ a]; group

theorem frame_cocycle {X Y : Scheme.{0}} (f : X ⟶ Y) (L : LB X) (hL : L.IsLB)
    (φ : ∀ i j, Γ(pullback f f, chart f L i j)ˣ) (hC : DescCompat f L φ) (hK : DescCocycle f L φ)
    (V : Y.Opens) (t : ∀ i, Γ(X, f ⁻¹ᵁ V ⊓ L.U i)ˣ) (ht : ∀ (i j : L.ι) (x : X), f x ∈ V → x ∈ L.U i → x ∈ L.U j →
      gm (𝟙 X) x _ (t i) = gm (𝟙 X) x _ (L.g i j) * gm (𝟙 X) x _ (t j)) :
    ∃ c : Γ(pullback f f, pullback.fst f f ⁻¹ᵁ f ⁻¹ᵁ V)ˣ,
      (∀ (z : ↥(pullback f f)) (i j : L.ι), f (pullback.fst f f z) ∈ V → pullback.fst f f z ∈ L.U i →
        pullback.snd f f z ∈ L.U j →
        gm (𝟙 _) z _ c = gm (pullback.fst f f) z _ (t i) * gm (𝟙 _) z _ (φ i j) * (gm (pullback.snd f f) z _ (t j))⁻¹) ∧
      ∀ w : ↥(Triple f), f (pullback.fst f f (pullback.fst (pullback.snd f f) (pullback.fst f f) w)) ∈ V →
        gm (pullback.fst (pullback.snd f f) (pullback.fst f f)) w _ c *
          gm (pullback.snd (pullback.snd f f) (pullback.fst f f)) w _ c = gm (pr13 f) w _ c := by

  have hf : ∀ z : ↥(pullback f f), f (pullback.snd f f z) = f (pullback.fst f f z) := fun z => by
    change (pullback.snd f f ≫ f) z = (pullback.fst f f ≫ f) z
    rw [pullback.condition]
  set V' : (pullback f f).Opens := pullback.fst f f ⁻¹ᵁ f ⁻¹ᵁ V with hV'
  let W : L.ι × L.ι → (pullback f f).Opens := fun p => chart f L p.1 p.2 ⊓ V'
  have le1 : ∀ p : L.ι × L.ι, W p ≤ pullback.fst f f ⁻¹ᵁ (f ⁻¹ᵁ V ⊓ L.U p.1) :=
    fun p z hz => ⟨hz.2, hz.1.1⟩
  have le2 : ∀ p : L.ι × L.ι, W p ≤ chart f L p.1 p.2 := fun p => inf_le_left
  have le3 : ∀ p : L.ι × L.ι, W p ≤ pullback.snd f f ⁻¹ᵁ (f ⁻¹ᵁ V ⊓ L.U p.2) :=
    fun p z hz => ⟨show f (pullback.snd f f z) ∈ V by rw [hf]; exact hz.2, hz.1.2⟩
  let u : ∀ p, Γ(pullback f f, W p)ˣ := fun p =>
    res (le1 p) (pullU (pullback.fst f f) _ (t p.1)) * res (le2 p) (φ p.1 p.2) *
      (res (le3 p) (pullU (pullback.snd f f) _ (t p.2)))⁻¹
  have key : ∀ {T : Scheme.{0}} (h : T ⟶ pullback f f) (y : T) (p : L.ι × L.ι) (hy : h y ∈ W p),
      gm h y (W p) (u p) = gm (h ≫ pullback.fst f f) y _ (t p.1) * gm h y _ (φ p.1 p.2) *
        (gm (h ≫ pullback.snd f f) y _ (t p.2))⁻¹ := by
    intro T h y p hy
    simp only [u, map_mul, map_inv]
    rw [gm_res h y (le1 p) hy, gm_res h y (le2 p) hy, gm_res h y (le3 p) hy,
      gm_pull h (pullback.fst f f) y _ (le1 p hy), gm_pull h (pullback.snd f f) y _ (le3 p hy)]

  have tpull : ∀ {T : Scheme.{0}} (h : T ⟶ X) (y : T) (i j : L.ι), f (h y) ∈ V → h y ∈ L.U i → h y ∈ L.U j →
      gm h y _ (t i) = gm h y _ (L.g i j) * gm h y _ (t j) := by
    intro T h y i j hV hi hj
    have hle1 : f ⁻¹ᵁ V ⊓ (L.U i ⊓ L.U j) ≤ f ⁻¹ᵁ V ⊓ L.U i := inf_le_inf_left _ inf_le_left
    have hle2 : f ⁻¹ᵁ V ⊓ (L.U i ⊓ L.U j) ≤ L.U i ⊓ L.U j := inf_le_right
    have hle3 : f ⁻¹ᵁ V ⊓ (L.U i ⊓ L.U j) ≤ f ⁻¹ᵁ V ⊓ L.U j := inf_le_inf_left _ inf_le_right
    have e : res hle1 (t i) = res hle2 (L.g i j) * res hle3 (t j) := by
      apply unit_ext
      intro x hx
      rw [map_mul, gm_res _ x hle1 hx, gm_res _ x hle2 hx, gm_res _ x hle3 hx]
      exact ht i j x hx.1 hx.2.1 hx.2.2
    have hm : h y ∈ f ⁻¹ᵁ V ⊓ (L.U i ⊓ L.U j) := ⟨hV, hi, hj⟩
    have := congrArg (gm h y _) e
    rwa [map_mul, gm_res h y hle1 hm, gm_res h y hle2 hm, gm_res h y hle3 hm] at this
  have hW : ∀ p, W p ≤ V' := fun p => inf_le_right
  have hcov : V' ≤ iSup W := by
    intro z hz
    obtain ⟨i, hi⟩ := exists_chart hL (pullback.fst f f z)
    obtain ⟨j, hj⟩ := exists_chart hL (pullback.snd f f z)
    exact TopologicalSpace.Opens.mem_iSup.2 ⟨(i, j), ⟨hi, hj⟩, hz⟩
  have hagree : ∀ p q, ∀ x ∈ W p ⊓ W q, gm (𝟙 _) x (W p) (u p) = gm (𝟙 _) x (W q) (u q) := by
    rintro ⟨i, j⟩ ⟨i', j'⟩ x ⟨hp, hq⟩
    rw [key (𝟙 _) x _ hp, key (𝟙 _) x _ hq]
    simp only [Category.id_comp]
    have hx1 : f (pullback.fst f f x) ∈ V := hp.2
    have hx2 : f (pullback.snd f f x) ∈ V := by rw [hf]; exact hx1
    rw [tpull (pullback.fst f f) x i' i hx1 hq.1.1 hp.1.1, tpull (pullback.snd f f) x j' j hx2 hq.1.2 hp.1.2,
      hC x i i' j j' hp.1.1 hq.1.1 hp.1.2 hq.1.2]
    have hg : gm (pullback.fst f f) x _ (L.g i' i) * gm (pullback.fst f f) x _ (L.g i i') = 1 := by
      rw [gm_cocycle hL _ x hq.1.1 hp.1.1 hq.1.1, gm_self hL _ x hq.1.1]
    rw [eq_inv_of_mul_eq_one_right hg, mul_inv]
    exact alg _ _ _ _ _
  obtain ⟨c, hc⟩ := glue_unit V' W hW hcov u hagree
  have cgerm : ∀ {T : Scheme.{0}} (h : T ⟶ pullback f f) (y : T) (p : L.ι × L.ι) (hy : h y ∈ W p),
      gm h y V' c = gm (h ≫ pullback.fst f f) y _ (t p.1) * gm h y _ (φ p.1 p.2) *
        (gm (h ≫ pullback.snd f f) y _ (t p.2))⁻¹ := by
    intro T h y p hy
    rw [← key h y p hy, ← hc p, gm_res h y (hW p) hy]
  refine ⟨c, ?_, ?_⟩
  · intro z i j hV hi hj
    have := cgerm (𝟙 _) z (i, j) ⟨⟨hi, hj⟩, hV⟩
    simpa only [Category.id_comp] using this
  · intro w hV
    set q₁ := pullback.fst (pullback.snd f f) (pullback.fst f f)
    set q₂ := pullback.snd (pullback.snd f f) (pullback.fst f f)
    have hmid : q₁ ≫ pullback.snd f f = q₂ ≫ pullback.fst f f := pullback.condition
    have h13a : pr13 f ≫ pullback.fst f f = q₁ ≫ pullback.fst f f := by unfold pr13; exact pullback.lift_fst _ _ _
    have h13b : pr13 f ≫ pullback.snd f f = q₂ ≫ pullback.snd f f := by unfold pr13; exact pullback.lift_snd _ _ _
    have pmid : pullback.snd f f (q₁ w) = pullback.fst f f (q₂ w) := by
      change (q₁ ≫ pullback.snd f f) w = (q₂ ≫ pullback.fst f f) w
      rw [hmid]
    have p13a : pullback.fst f f (pr13 f w) = pullback.fst f f (q₁ w) := by
      change (pr13 f ≫ pullback.fst f f) w = (q₁ ≫ pullback.fst f f) w
      rw [h13a]
    have p13b : pullback.snd f f (pr13 f w) = pullback.snd f f (q₂ w) := by
      change (pr13 f ≫ pullback.snd f f) w = (q₂ ≫ pullback.snd f f) w
      rw [h13b]
    obtain ⟨i, hi⟩ := exists_chart hL (pullback.fst f f (q₁ w))
    obtain ⟨j, hj⟩ := exists_chart hL (pullback.snd f f (q₁ w))
    obtain ⟨k, hk⟩ := exists_chart hL (pullback.snd f f (q₂ w))
    have hV2 : f (pullback.fst f f (q₂ w)) ∈ V := by rw [← pmid, hf]; exact hV
    have hV3 : f (pullback.fst f f (pr13 f w)) ∈ V := by rw [p13a]; exact hV
    have m1 : q₁ w ∈ W (i, j) := ⟨⟨hi, hj⟩, hV⟩
    have m2 : q₂ w ∈ W (j, k) := ⟨⟨show pullback.fst f f (q₂ w) ∈ L.U j by rw [← pmid]; exact hj, hk⟩, hV2⟩
    have m3 : pr13 f w ∈ W (i, k) := ⟨⟨show pullback.fst f f (pr13 f w) ∈ L.U i by rw [p13a]; exact hi,
      show pullback.snd f f (pr13 f w) ∈ L.U k by rw [p13b]; exact hk⟩, hV3⟩
    rw [cgerm q₁ w (i, j) m1, cgerm q₂ w (j, k) m2, cgerm (pr13 f) w (i, k) m3, hmid, h13a, h13b,
      ← hK w i j k hi hj hk]
    dsimp only
    simp only [q₁, q₂]
    group

end GlobalTheory.LBDescent
