import Mathlib
import Development.AbelianVarietyTheory.Definitions.Divisors.ThetaSetupStructureMapComp
import Development.AbelianVarietyTheory.Proofs.DivisorProofs.CongModOfSpan
import Development.AbelianVarietyTheory.Proofs.DivisorProofs.ClassicalTraitsFourierTermsAllLevelsStatement
import Development.AbelianVarietyTheory.Proofs.DivisorProofs.ExistsClassicalTraitSystemAtClosedPointStatement
import Development.AbelianVarietyTheory.Proofs.DivisorProofs.PosDefMapIntOfPos
import Development.AbelianVarietyTheory.Proofs.DivisorProofs.ExistsCommonModule
import Development.AbelianVarietyTheory.Proofs.DivisorProofs.SqLeOfForallNonneg
import Development.AbelianVarietyTheory.Proofs.IwasawaDecomposition.ExistsUniformStrictnessAndFaceCutBounds
import Development.AbelianVarietyTheory.Proofs.DegenerationProofs.IsMaximalComapMaximalIdealOO
open AlgebraicGeometry CategoryTheory Limits MonoidalCategory CartesianMonoidalCategory IsLocalRing
open ReciprocityTheory.ChartRestriction.G ReciprocityTheory.ChartRestriction.G.Semi ReciprocityTheory.ChartRestriction.G.Out ReciprocityTheory.ChartRestriction.G.Geo.Tor ReciprocityTheory.AnForm.R
open GlobalTheory.Toroidal GlobalTheory.LocM GlobalTheory.PolFan GlobalTheory.ZM GlobalTheory.SiegelTor GlobalTheory.ZH
open scoped CategoryTheory.Obj
open GlobalTheory.Poinc

noncomputable section

namespace GlobalTheory.Poinc.GApprox

variable {g n : ℕ} {F : SFam g n (zetaAway n)} {W : Matrix (Fin 8) (Fin 8) ℤ} {Z : Scheme.{0}} {i : F.X ⟶ Z}

theorem sc_pos {X₁ : Setup F W i} {p : ℕ} [Fact p.Prime] {z₁ : X₁.M.Z} (c : CTrait X₁ p z₁) : 0 < c.sc := by
  set u := algebraMap c.T.V (FractionRing c.T.V) c.T.ϖ with hu
  have h0 : c.T.ϖ ≠ 0 := c.T.irr.ne_zero
  have hu0 : u ≠ 0 := by
    rw [hu]; exact (IsFractionRing.injective c.T.V (FractionRing c.T.V)).ne_iff' (map_zero _) |>.2 h0
  have hι0 : c.ι u ≠ 0 := (map_ne_zero c.ι).2 hu0
  have hle : ‖c.ι u‖ ≤ 1 := (c.int u).2 ⟨_, rfl⟩
  have hlt : ‖c.ι u‖ < 1 := by
    refine lt_of_le_of_ne hle fun h1 => ?_
    have : ‖c.ι u⁻¹‖ ≤ 1 := by rw [map_inv₀, norm_inv, h1, inv_one]
    obtain ⟨v, hv⟩ := (c.int _).1 this
    apply c.T.irr.not_isUnit
    refine IsUnit.of_mul_eq_one v ?_
    apply IsFractionRing.injective c.T.V (FractionRing c.T.V)
    rw [map_mul, hv, ← hu, mul_inv_cancel₀ hu0, map_one]
  have hpos : 0 < ‖c.ι u‖ := norm_pos_iff.2 hι0
  unfold CTrait.sc
  rw [← hu]
  linarith [Real.log_neg hpos hlt]

theorem sc_evalZ_eq_of_equiv {φ : InvFun ℤ} {X Y : Type} [AddCommGroup X] [AddCommGroup Y] [Module.Finite ℤ X]
    [Module.Finite ℤ Y] (hP : IsPolFanOn id φ X) {β : LinearMap.BilinForm ℤ X} (hs : ∀ a b, β a b = β b a)
    (hp : ∀ x, 0 ≤ β x x) {β' : LinearMap.BilinForm ℤ Y} {s s' : ℝ} (hs0 : 0 ≤ s) (hs' : 0 < s') (e : X ≃ₗ[ℤ] Y)
    (he : ∀ a b, s * β a b = s' * β' (e a) (e b)) : s * φ.evalZ β = s' * φ.evalZ β' := by
  have hmem : toReal β ∈ psdCone (id : ℤ → ℤ) X :=
    StrictData.toReal_mem_psdCone (mem_balForms_int.2 hs) hp
  have hform : toReal (pull e.toLinearMap β') = (s / s') • toReal β := by
    refine LinearMap.ext₂ fun a b => ?_
    have := he a b
    simp only [toReal_apply, pull_apply, LinearMap.smul_apply, smul_eq_mul, LinearEquiv.coe_coe]
    field_simp
    linarith
  have h1 : φ.evalZ (pull e.toLinearMap β') = φ.evalZ β' := evalZ_pull φ e.surjective _
  rw [← h1, ← evalR_toReal φ (pull _ _), hform, hP.homog _ hmem _ (div_nonneg hs0 hs'.le), evalR_toReal]
  field_simp

theorem sc_evalZ_eq {X₁ : Setup F W i} {p : ℕ} [Fact p.Prime] {z₁ : X₁.M.Z} {φ : InvFun ℤ} (hpol : IsPolFan id g φ)
    (Φ : FPre (id : ℤ → ℤ) g p (X₁.M.Z.presheaf.stalk z₁)) (y : Φ.Y) (c : CTrait X₁ p z₁)
    (e : Φ.X y ≃ₗ[ℤ] c.T.D.toPre.X) (he : ∀ a b, Φ.sc y * Φ.β y a b = c.sc * c.T.β (e a) (e b)) :
    Φ.sc y * @InvFun.evalZ ℤ _ φ (Φ.X y) _ (Φ.mod y) (Φ.fin y) (Φ.β y) = c.sc * φ.evalZ c.T.β := by
  have hm : Φ.mod y = AddCommGroup.toIntModule _ := Subsingleton.elim _ _
  have hrk : Module.finrank ℤ (Φ.X y) ≤ g := by
    convert Φ.rank_le y <;> first | rfl | exact hm.symm
  have hP : IsPolFanOn id φ (Φ.X y) := hpol _ hrk
  convert sc_evalZ_eq_of_equiv hP (Φ.bal y).1 (fun x => by
      by_cases hx : x = 0
      · simp [hx]
      · exact (Φ.pos y x hx).le) (Φ.sc_pos y).le (sc_pos c) e he

end GlobalTheory.Poinc.GApprox

namespace GlobalTheory.Poinc.GApprox

theorem exists_hasSupports_int (q : ℕ) (φ : InvFun ℤ) (hpol : IsPolFan id q φ) :
    ∃ d, 1 ≤ d ∧ HasSupports (id : ℤ → ℤ) q φ d := by
  refine exists_hasSupports ℤ id q φ hpol fun X _ iM _ _ hX r γ hγ => ?_
  obtain rfl : iM = AddCommGroup.toIntModule X := Subsingleton.elim _ _
  have : Module.Free ℤ X := inferInstance
  exact exists_supports_int φ X (hpol X hX) r γ hγ

end GlobalTheory.Poinc.GApprox

open GlobalTheory.Poinc.GApprox in
theorem GlobalTheory.Poinc.exists_approxAt : ∀ (g n : ℕ) (F : SFam g n (zetaAway n)) (W : Matrix (Fin 8) (Fin 8) ℤ) (Z : Scheme.{0})
    (i : F.X ⟶ Z) (X₁ : Setup F W i), IsE8 W → ThetaInt F W i → 1 ≤ g → ∀ φ : InvFun ℤ, IsPolFan id g φ →
    ∃ d : ℕ, 1 ≤ d ∧ ApproxAt X₁ φ d := by
  intro g n F W Z i X₁ hE hT hg φ hpol
  obtain ⟨d₀, hd₀, hsup⟩ := exists_hasSupports_int g φ hpol
  have hW : 0 < W 0 0 := by
    have := hE.1.diag_pos (i := 0)
    simp only [Matrix.map_apply, Matrix.add_apply, Matrix.transpose_apply] at this
    have h2 : (0 : ℤ) < W 0 0 + W 0 0 := by exact_mod_cast this
    omega
  have hN : 3 ≤ X₁.S.r * X₁.S.N := by
    have h1 := X₁.big.2.2
    have h2 : 1 ≤ X₁.S.N ^ X₁.S.g * X₁.S.g.factorial :=
      Nat.one_le_iff_ne_zero.2 (Nat.mul_ne_zero (pow_ne_zero _ (by have := X₁.S.N_ge; omega))
        (Nat.factorial_ne_zero _))
    omega
  refine ⟨(W 0 0).toNat * d₀, Nat.one_le_iff_ne_zero.2 (Nat.mul_ne_zero (by omega) (by omega)), ?_⟩
  intro p _ z₁ c₀ e
  have hcl : IsClosed ({z₁} : Set X₁.M.Z) := c₀.cen ▸ isClosed_singleton_cPt c₀.y
  obtain ⟨Φ, hM, hL, hR⟩ := GlobalTheory.T.exists_classicalTrait_system_at_closedPoint g n F W Z i hE hT X₁ p z₁ hcl
  have hF := GlobalTheory.T.classical_traits_fourier_terms_all_levels g n F W Z i X₁ p z₁ Φ hE.1 hR
  haveI : IsDedekindDomain X₁.o₁ := X₁.ar.ded
  haveI := X₁.M.lft
  haveI : IsLocallyNoetherian X₁.M.Z := LocallyOfFiniteType.isLocallyNoetherian X₁.M.s
  have hA := approx_of_fpre ℤ id bddAct_int_and_cm.1 g φ hpol d₀ hsup 7 W hE.1 (X₁.S.r * X₁.S.N) p hN _ Φ hL hF
  obtain ⟨y₀, hev₀, e₀, he₀⟩ := hM c₀
  obtain ⟨f, hf1, hf2⟩ := hA y₀ e
  have key : ∀ (y : Φ.Y) (c : CTrait X₁ p z₁) (e' : Φ.X y ≃ₗ[ℤ] c.T.D.toPre.X),
      (∀ a b, Φ.sc y * Φ.β y a b = c.sc * c.T.β (e' a) (e' b)) →
      Φ.sc y * (((W 0 0).toNat * d₀ : ℕ) * @InvFun.evalZ ℤ _ φ (Φ.X y) _ (Φ.mod y) (Φ.fin y) (Φ.β y)) = c.sc * (((W 0 0).toNat * d₀ : ℕ) * φ.evalZ c.T.β) := by
    intro y c e' h
    have := sc_evalZ_eq hpol Φ y c e' h
    calc Φ.sc y * (((W 0 0).toNat * d₀ : ℕ) * @InvFun.evalZ ℤ _ φ (Φ.X y) _ (Φ.mod y) (Φ.fin y) (Φ.β y))
        = ((W 0 0).toNat * d₀ : ℕ) * (Φ.sc y * @InvFun.evalZ ℤ _ φ (Φ.X y) _ (Φ.mod y) (Φ.fin y) (Φ.β y)) := by ring
      _ = c.sc * (((W 0 0).toNat * d₀ : ℕ) * φ.evalZ c.T.β) := by rw [this]; ring
  refine ⟨f, ?_, fun c => ?_⟩
  · rw [← hev₀ f, hf1, key y₀ c₀ e₀ he₀]
  · obtain ⟨y, hev, e', he'⟩ := hM c
    rcases hf2 y with h | ⟨h, hh, hle⟩
    · left; rw [← hev f, ← key y c e' he']; exact h
    · right; exact ⟨h, hh, by rw [← hev f, ← hev h]; exact hle⟩

end
