import Mathlib
import Development.AbelianVarietyTheory.Definitions.TwistedInnerProducts.RealLowestKTypeCoeffSquareIntegrable
import Development.AbelianVarietyTheory.Proofs.InertiaEigenvalues.FockCoefficientExtremalEqDiscreteSeries
import Development.AbelianVarietyTheory.BlockTheory.TwistedDiscreteSeries.HolomorphicLowestType
import Formalism.FormalDegreeBessel.FormalDegreeRatio.DSLowK
open Formalism.BasicNotions Formalism.BasicNotions.TH Formalism.MainNotions.GlobalTheoryTheory.GenusMassVolumes Formalism.MainNotions.StandardLattice Formalism.MainNotions.S MeasureTheory IsDedekindDomain NumberField Matrix
open Formalism.L2.Theta Formalism.L2.DIV Formalism.L2.Indef Formalism.DivisionValues
open Formalism.IndefR Formalism.IndefR.Links Formalism.IndefR.Lev Formalism.IndefR.Mult Formalism.IndefR.Datum Formalism.IndefR.Cx
open scoped TensorProduct InnerProductSpace
attribute [local instance] Formalism.BasicNotions.Host.cmStar
open GlobalTheory.AdelicScalar GlobalTheory.IndefiniteRowSideFullHecke.TwistedLevelValues

open Formalism.IndefR.FWT GlobalTheory.HolDict

namespace GlobalTheory.HolLabel

theorem eq_self_of_strictMono {n : ℕ} {F K : Fin n → Fin n} (hK : StrictMono K) (hF : StrictMono F)
    (hFK : ∀ x, F (K x) = x) (x : Fin n) : K x = x :=
  le_antisymm ((hF.le_apply (x := K x)).trans_eq (hFK x)) hK.le_apply

theorem holShape_signs_first : ∀ (D : D7) (σ : D.what₂.LL →+* ℂ), (∀ ρ, Formalism.IndefR.Links.labE σ (Formalism.IndefR.Links.lab σ ρ) = tauE D ρ) → ∀ (i : Fin D.R.m) (M : DSModel (Wc D i) (Formalism.IndefR.Links.parAt (Wc D i) σ)), GlobalTheory.HolDict.HolShape M ∧ (∃ wt, GlobalTheory.HolDict.WtIs M wt) ∧ ∀ ρ a, M.ε ρ a = decide ((a : ℕ) < pW D i ρ) := by
  intro D σ hσ i M
  have hrk : (Wc D i).rank = D.R.r := D.sp.W_rank _ (Nat.le_add_left _ _) i.2
  have hpq : ∀ τ, ((Wc D i).sig (tauE D τ)).1 + ((Wc D i).sig (tauE D τ)).2 = (Wc D i).rank :=
    fun τ => GlobalTheory.IndefiniteRowSideFullHecke.HolomorphicLowestType.pW_add_qW i τ
  have hp : ∀ ρ, Formalism.IndefR.Links.parAt (Wc D i) σ ρ =
      Formalism.L2.NTheta.holPar _ _ (Formalism.IndefR.Links.kapAt (Wc D i) σ ρ) := fun ρ => by
    unfold Formalism.IndefR.Links.parAt; rw [if_pos (hσ ρ)]
  have ho : ∀ ρ, Odd (((Wc D i).sig (tauE D ρ)).1 + ((Wc D i).sig (tauE D ρ)).2) := fun ρ => by
    rw [hpq ρ, hrk]; exact Formalism.L2.NTheta.odd_r _
  have hentL : ∀ ρ (c : Fin ((Wc D i).sig (tauE D ρ)).1),
      Formalism.L2.NTheta.holEnt _ _ (Formalism.IndefR.Links.kapAt (Wc D i) σ ρ) (Sum.inl c) =
        Formalism.L2.NTheta.lamAt (D.hc (Formalism.IndefR.Links.lab σ ρ)) (c : ℕ) := fun ρ c => by
    unfold Formalism.IndefR.Links.kapAt
    rw [Formalism.L2.NTheta.holEnt_kapW _ _ _ (ho ρ)]
    rfl
  have hentR : ∀ ρ (c : Fin ((Wc D i).sig (tauE D ρ)).2),
      Formalism.L2.NTheta.holEnt _ _ (Formalism.IndefR.Links.kapAt (Wc D i) σ ρ) (Sum.inr c) =
        Formalism.L2.NTheta.lamAt (D.hc (Formalism.IndefR.Links.lab σ ρ)) (((Wc D i).sig (tauE D ρ)).1 + (c : ℕ)) := fun ρ c => by
    unfold Formalism.IndefR.Links.kapAt
    rw [Formalism.L2.NTheta.holEnt_kapW _ _ _ (ho ρ)]
    rfl
  have heps : ∀ ρ a, M.ε ρ a = decide ((a : ℕ) < pW D i ρ) := by
    intro ρ
    obtain ⟨hanti, f, hf, hS⟩ := M.enum ρ
    rw [hp ρ] at hS
    have hpqρ := hpq ρ
    have hL := hentL ρ
    have hR := hentR ρ
    have hlt : ∀ m n : ℕ, m < n → n < (Wc D i).rank → Formalism.L2.NTheta.lamAt (D.hc (Formalism.IndefR.Links.lab σ ρ)) n < Formalism.L2.NTheta.lamAt (D.hc (Formalism.IndefR.Links.lab σ ρ)) m := by
      intro m n hmn hn
      have h1 := Formalism.L2.NTheta.lamAt_add_add_le (D.hc (Formalism.IndefR.Links.lab σ ρ)) (n - m) m (by omega)
      rw [show m + (n - m) = n by omega] at h1
      have h2 : (1 : ℚ) ≤ ((n - m : ℕ) : ℚ) := by exact_mod_cast (by omega : 1 ≤ n - m)
      linarith
    have mem1 : ∀ a : Fin (Wc D i).rank, ∃ n : Fin (Wc D i).rank,
        f a = Formalism.L2.NTheta.lamAt (D.hc (Formalism.IndefR.Links.lab σ ρ)) n ∧ M.ε ρ a = decide ((n : ℕ) < ((Wc D i).sig (tauE D ρ)).1) := by
      intro a
      have hm : (f a, M.ε ρ a) ∈ Formalism.L2.NTheta.holPar ((Wc D i).sig (tauE D ρ)).1 ((Wc D i).sig (tauE D ρ)).2 (Formalism.IndefR.Links.kapAt (Wc D i) σ ρ) := by
        rw [hS]; exact Finset.mem_image_of_mem _ (Finset.mem_univ a)
      obtain ⟨x, -, hx⟩ := Finset.mem_image.1 hm
      rcases x with c | c
      · simp only [hL, Sum.elim_inl, Prod.mk.injEq] at hx
        exact ⟨⟨c, by omega⟩, hx.1.symm, by rw [← hx.2]; exact (decide_eq_true c.2).symm⟩
      · simp only [hR, Sum.elim_inr, Prod.mk.injEq] at hx
        exact ⟨⟨((Wc D i).sig (tauE D ρ)).1 + c, by omega⟩, hx.1.symm, by rw [← hx.2]; exact (decide_eq_false (by simp)).symm⟩
    have mem2 : ∀ n : Fin (Wc D i).rank, ∃ a : Fin (Wc D i).rank,
        f a = Formalism.L2.NTheta.lamAt (D.hc (Formalism.IndefR.Links.lab σ ρ)) n ∧ M.ε ρ a = decide ((n : ℕ) < ((Wc D i).sig (tauE D ρ)).1) := by
      intro n
      have hm : ∀ x, (Formalism.L2.NTheta.holEnt ((Wc D i).sig (tauE D ρ)).1 ((Wc D i).sig (tauE D ρ)).2 (Formalism.IndefR.Links.kapAt (Wc D i) σ ρ) x,
          Sum.elim (fun _ => true) (fun _ => false) x) ∈ Finset.univ.image (fun a => (f a, M.ε ρ a)) := fun x => by
        rw [← hS]; unfold Formalism.L2.NTheta.holPar; exact Finset.mem_image_of_mem _ (Finset.mem_univ x)
      by_cases hn : (n : ℕ) < ((Wc D i).sig (tauE D ρ)).1
      · obtain ⟨a, -, ha⟩ := Finset.mem_image.1 (hm (Sum.inl ⟨n, hn⟩))
        simp only [hL, Sum.elim_inl, Prod.mk.injEq] at ha
        exact ⟨a, ha.1, by rw [ha.2]; exact (decide_eq_true hn).symm⟩
      · obtain ⟨a, -, ha⟩ := Finset.mem_image.1 (hm (Sum.inr ⟨n - ((Wc D i).sig (tauE D ρ)).1, by omega⟩))
        simp only [hR, Sum.elim_inr, Prod.mk.injEq] at ha
        rw [show ((Wc D i).sig (tauE D ρ)).1 + (n - ((Wc D i).sig (tauE D ρ)).1) = n by omega] at ha
        exact ⟨a, ha.1, by rw [ha.2]; exact (decide_eq_false hn).symm⟩
    choose F hF1 hF2 using mem1
    choose K hK1 hK2 using mem2
    have fanti : ∀ a b : Fin (Wc D i).rank, a < b → f b < f a := fun a b hab => by
      have h1 := hanti hab
      rw [hf, hf] at h1
      exact_mod_cast h1
    have hFm : StrictMono F := fun a b hab => by
      have h1 := fanti a b hab
      rw [hF1 a, hF1 b] at h1
      by_contra hc
      rcases (not_lt.1 hc).lt_or_eq with hc | hc
      · exact lt_asymm h1 (hlt _ _ hc (F a).2)
      · rw [hc] at h1; exact lt_irrefl _ h1
    have hKm : StrictMono K := fun n m hnm => by
      have h1 := hlt _ _ hnm m.2
      rw [← hK1 n, ← hK1 m] at h1
      by_contra hc
      rcases (not_lt.1 hc).lt_or_eq with hc | hc
      · exact lt_asymm h1 (fanti _ _ hc)
      · rw [hc] at h1; exact lt_irrefl _ h1
    have hFK : ∀ n, F (K n) = n := fun n => by
      have h1 : Formalism.L2.NTheta.lamAt (D.hc (Formalism.IndefR.Links.lab σ ρ)) (F (K n)) = Formalism.L2.NTheta.lamAt (D.hc (Formalism.IndefR.Links.lab σ ρ)) n := by rw [← hF1, hK1]
      rcases lt_trichotomy (F (K n)) n with hc | hc | hc
      · exact absurd h1 (hlt _ _ hc n.2).ne'
      · exact hc
      · exact absurd h1 (hlt _ _ hc (F (K n)).2).ne
    intro a
    have h1 := hK2 a
    rw [eq_self_of_strictMono hKm hFm hFK a] at h1
    exact h1
  have hc : ∀ ρ, Formalism.QuantumDeterminants.IsHCParam (M.lam ρ) := fun ρ => by
    have he := M.enum ρ
    refine ⟨he.1, fun a => ?_⟩
    obtain ⟨-, f, hf, hS⟩ := he
    rw [hp ρ] at hS
    have hm : (f a, M.ε ρ a) ∈ Formalism.L2.NTheta.holPar _ _ (Formalism.IndefR.Links.kapAt (Wc D i) σ ρ) := by
      rw [hS]; exact Finset.mem_image_of_mem _ (Finset.mem_univ a)
    obtain ⟨x, -, hx⟩ := Finset.mem_image.1 hm
    have hx1 := congrArg Prod.fst hx
    have hn : (((Wc D i).rank : ℕ) : ℝ) = (((Wc D i).sig (tauE D ρ)).1 : ℝ) + ((Wc D i).sig (tauE D ρ)).2 := by
      rw [← hpq ρ]; push_cast; rfl
    dsimp only at hx1
    rw [hf a, ← hx1, hn]
    rcases x with c | c
    · refine ⟨Formalism.IndefR.Links.kapAt (Wc D i) σ ρ (Sum.inl c) - ((Wc D i).sig (tauE D ρ)).2 - (c : ℕ), ?_⟩
      simp only [Formalism.L2.NTheta.holEnt, Sum.elim_inl]
      push_cast
      ring
    · refine ⟨Formalism.IndefR.Links.kapAt (Wc D i) σ ρ (Sum.inr c) - (c : ℕ), ?_⟩
      simp only [Formalism.L2.NTheta.holEnt, Sum.elim_inr]
      push_cast
      ring
  refine ⟨fun ρ => ⟨hc ρ, _, (Nat.le_add_right _ _).trans (hpq ρ).le, heps ρ⟩, ?_, heps⟩
  choose w hw using fun ρ a => Formalism.QuantumDeterminants.low_k_integrable_holds _ (M.ε ρ) _ (hc ρ).2 a
  exact ⟨fun ρ c => w ρ (revNeg (M.ε ρ) (arr (nNeg (M.ε ρ)) c)), fun ρ c => (hw ρ _).symm⟩

end GlobalTheory.HolLabel
