import Mathlib
import Development.AbelianVarietyTheory.Proofs.DivisorProofs.ZetaAwayPowSelf
import Development.AbelianVarietyTheory.Proofs.DivisorProofs.ExistsLevelGeometricFibre
import Development.AbelianVarietyTheory.Proofs.DivisorProofs.ExistsRingHomCyclotomicAwayToFieldPrimitiveRootStatement
import Development.AbelianVarietyTheory.Proofs.DivisorProofs.ExistsLatticeMapModOfPerfectAlternatingStatement

noncomputable section

namespace GlobalTheory.HTSympCover

open AlgebraicGeometry CategoryTheory Limits MonoidalCategory CartesianMonoidalCategory MonObj NumberField
open ReciprocityTheory.ChartRestriction.G ReciprocityTheory.ChartRestriction.G.Semi ReciprocityTheory.ChartRestriction.G.Geo.Tor GlobalTheory.UPel.HT GlobalTheory.SiegelTor GlobalTheory.HTSympCover

theorem exists_sympCond_geom : ∀ (p : ℕ) [Fact p.Prime] (D : Datum p) (R : Type) [CommRing R] (b : Base D R) (N M e : ℕ),
  3 ≤ M → Odd M → M.Coprime p → M.Coprime e → (∀ f : D.La →ₗ[ℤ] ℤ, ∃ x, D.EF x = e • f) →
  ∀ (S : Scheme.{0}) (t : S ⟶ Sp R) (o : Obj b N t) (K : Type) [Field K] [IsAlgClosed K] (x : Sp K ⟶ S),
    ∃ (t₁ : Sp K ⟶ Sp (cycAway M))
      (lv : (Fin (Module.finrank ℤ D.La / 2) ⊕ Fin (Module.finrank ℤ D.La / 2) → ℤ) → (Over.mk x ⟶ o.A)),
      SympCond o (Module.finrank ℤ D.La / 2) M (Over.mk x) t₁ lv := by
  intro p _ D R _ b N M e hM3 hModd hMp hMe hperf S t o K _ _ x
  have hM0 : 0 < M := by omega
  obtain ⟨β, hβ⟩ : ∃ β : R →+* K, x ≫ t = spMap β :=
    ⟨(Spec.preimage (x ≫ t)).hom, (Spec.map_preimage _).symm⟩
  obtain ⟨I, -⟩ := o.geom K x β hβ
  haveI := D.freeLa
  haveI := D.finLa
  obtain ⟨L, hLker, hLonto, hLω⟩ := GlobalTheory.T.exists_lattice_map_mod_of_perfect_alternating D.La D.EF D.alt D.nondeg e M hperf hMe
  have hMK : IsUnit (M : K) := by simpa using (b.unit M hMp).map β
  have hprim : IsPrimitiveRoot ((I.ζ M : Kˣ) : K) M :=
    IsPrimitiveRoot.coe_units_iff.2 (I.ζ_prim M hM0 hMp)
  obtain ⟨β₀, hβ₀⟩ := GlobalTheory.T.exists_ringHom_cyclotomicAway_to_field_primitiveRoot M (by omega) K _ hprim hMK
  refine ⟨spMap β₀, fun a => I.ev M (L a), fun a c => by show I.ev M (L (a + c)) = I.ev M (L a) * I.ev M (L c); rw [map_add, I.add],
    fun a => (I.onto M hM0 hMp _).2 ⟨L a, rfl⟩, ?_⟩
  intro k _ _ x' β' z hx' hz
  obtain ⟨φ, rfl⟩ : ∃ φ : K →+* k, x' = spMap φ := ⟨(Spec.preimage x').hom, (Spec.map_preimage _).symm⟩
  have hβ' : β' = φ.comp β₀ := by
    have e2 : CommRingCat.ofHom β' = CommRingCat.ofHom (φ.comp β₀) :=
      Spec.map_injective (hx'.symm.trans (by simp [spMap, ← Spec.map_comp, CommRingCat.ofHom_comp]))
    exact congrArg CommRingCat.Hom.hom e2
  subst hβ'
  have hz' : z = Units.map φ.toMonoidHom (I.ζ M) := Units.ext (by simpa [hβ₀] using hz)
  subst hz'
  obtain ⟨β'', hβ''⟩ : ∃ β'' : R →+* k, (spMap φ ≫ x) ≫ t = spMap β'' :=
    ⟨(Spec.preimage ((spMap φ ≫ x) ≫ t)).hom, (Spec.map_preimage _).symm⟩
  obtain ⟨I', -⟩ := o.geom k (spMap φ ≫ x) β'' hβ''
  let h : Over.mk (spMap φ ≫ (Over.mk x).hom) ⟶ Over.mk x := Over.homMk (spMap φ) rfl
  have : Epi h.left := GlobalTheory.UPel.GeometricLevelExistence.epi_spMap_field φ
  have : Epi h := Over.epi_of_epi_left h
  change (∀ a, h ≫ I.ev M (L a) = 1 ↔ _) ∧ (∀ g : Over.mk (spMap φ ≫ (Over.mk x).hom) ⟶ o.A, g ^ M = 1 →
      ∃ a, h ≫ I.ev M (L a) = g) ∧ ∀ a c, Out.WeilIs o.A (o.P.pull (lift (𝟙 o.A) (𝟙 o.A)).left) M
        (h ≫ I.ev M (L a)) (h ≫ I.ev M (L c)) (unitOn (Units.map φ.toMonoidHom (I.ζ M) ^ (2 * omegaStd a c)))
  have hPa : ∀ a b, h ≫ I.ev M (a + b) = (h ≫ I.ev M a) * (h ≫ I.ev M b) := fun a b => by
    rw [I.add, MonObj.comp_mul]
  have hP : ∀ a, h ≫ I.ev M a = 1 ↔ ∃ b, a = M • b := fun a => by
    rw [← I.ker M hM0 hMp a]
    constructor
    · intro e
      rw [← cancel_epi h, e, MonObj.comp_one]
    · intro e
      rw [e, MonObj.comp_one]
  have hQ : ∀ a, I'.ev M a = 1 ↔ ∃ b, a = M • b := I'.ker M hM0 hMp
  have hPQ : ∀ a, ∃ c, h ≫ I.ev M a = I'.ev M c := fun a => by
    have e : (h ≫ I.ev M a) ^ M = 1 := by
      rw [← MonObj.comp_pow, (I.onto M hM0 hMp (I.ev M a)).2 ⟨a, rfl⟩, MonObj.comp_one]
    obtain ⟨c, hc⟩ := (I'.onto M hM0 hMp _).1 e
    exact ⟨c, hc.symm⟩
  refine ⟨fun a => (hP (L a)).trans (hLker a), fun g hg => ?_, fun a c => ?_⟩
  · obtain ⟨c, rfl⟩ := (I'.onto M hM0 hMp g).1 hg
    obtain ⟨x₀, hx₀⟩ := GlobalTheory.UPel.GeometricLevelExistence.exists_eq_of_count M hM0 _ _ hPa (I'.add M) hP hQ hPQ c
    obtain ⟨a, y, rfl⟩ := hLonto x₀
    refine ⟨a, ?_⟩
    rw [← hx₀, hPa, (hP _).2 ⟨y, rfl⟩, _root_.mul_one]
  · have H := CommPairing.weilIs_pull globalFns_holds o.ab (LB.isLB_pull o.isLB _) h (I.isom M hM0 hMp (L a) (L c))
    have e : Units.map h.left.appTop.hom.toMonoidHom (unitOn (I.ζ M ^ (2 * D.EF (L a) (L c)))) =
        unitOn (Units.map φ.toMonoidHom (I.ζ M) ^ (2 * omegaStd a c)) := by
      obtain ⟨k₀, hk₀⟩ := hLω a c
      have hζM : I.ζ M ^ (M : ℤ) = 1 := by
        rw [zpow_natCast]
        exact (I.ζ_prim M hM0 hMp).pow_eq_one
      have hK : I.ζ M ^ (2 * D.EF (L a) (L c)) = I.ζ M ^ (2 * omegaStd a c) := by
        rw [hk₀, show (2 : ℤ) * (omegaStd a c + M * k₀) = 2 * omegaStd a c + M * (2 * k₀) by ring, zpow_add,
          zpow_mul (I.ζ M) (M : ℤ), hζM, one_zpow, _root_.mul_one]
      rw [hK, ← map_zpow]
      exact GlobalTheory.UPel.GeometricLevelExistence.unitOn_pull_spMap φ _
    rw [e] at H
    exact H

end GlobalTheory.HTSympCover
