import Mathlib
import Development.AbelianVarietyTheory.Proofs.DivisorProofs.IsUSplittingInl
import Development.AbelianVarietyTheory.Proofs.DecoratedLatticeProofs.ImportsBis
import Development.AbelianVarietyTheory.Definitions.Foundations.T2SpaceAdeleRing
import Development.AbelianVarietyTheory.Proofs.DivisorProofs.ContinuousUgReindex
import Development.AbelianVarietyTheory.Proofs.DivisorProofs.SeesawOneSourceOrthogonalSumTargets
import Development.AbelianVarietyTheory.Proofs.DecoratedLatticeProofs.ThetaHeisenbergConjugationNegHermitianSpace
import Development.AbelianVarietyTheory.Proofs.TwistedOperators.GridHeckeCharactersExist

open Formalism.BasicNotions Formalism.BasicNotions.TH Formalism.MainNotions.GlobalTheoryTheory.GenusMassVolumes Formalism.L2.Theta
open IsDedekindDomain NumberField
open scoped TensorProduct GlobalTheory.IndefiniteRowSideFullHecke.WeilTensorModels Matrix
open GlobalTheory.IndefiniteRowSideFullHecke.WeilTensorModels (Base Spc Model Chr incM)

attribute [local instance] Formalism.BasicNotions.Host.cmStar

noncomputable section

namespace GlobalTheory.DblRestrictChar

open Formalism.IndefR.Datum

set_option maxHeartbeats 1000000 in

theorem dbl_diag_box : ∀ (D : D7) (ψ : AddChar (AF D.host.F) ℂ), IsStdChar D ψ (-1) →
    ∀ (S T : HSp D) (χS χT : (AE D)ˣ →* Circle)
      (ω : GA D S × GA D T →* Module.End ℂ (Formalism.IndefR.Datum.WO S T → ℂ)),
      Formalism.IndefR.Datum.IsWeilDatumO ψ S T χS χT ω →
      ∀ ωd : GA D S × GA D (GlobalTheory.Doubling.dblSp T) →* Module.End ℂ (Formalism.IndefR.Datum.WO S (GlobalTheory.Doubling.dblSp T) → ℂ),
        Formalism.IndefR.Datum.IsWeilDatumO ψ S (GlobalTheory.Doubling.dblSp T) χS 1 ωd →
        (∀ (h : GA D T) (hd : GA D (GlobalTheory.Doubling.dblSp T)),
          Formalism.L2.Theta.mt (GlobalTheory.Doubling.dblSp T) hd =
            Matrix.reindex finSumFinEquiv finSumFinEquiv (Matrix.fromBlocks (Formalism.L2.Theta.mt T h) 0 0 1) →
          ∃ c : ℂ, ∀ f₁ f₂ : Formalism.IndefR.Datum.WO S T → ℂ,
            Formalism.IndefR.Datum.IsThetaFunO ψ S T f₁ → Formalism.IndefR.Datum.IsThetaFunO ψ S T f₂ →
              ωd (1, hd) (fun w : Formalism.IndefR.Datum.WO S (GlobalTheory.Doubling.dblSp T) => f₁ (w.submatrix (finSumFinEquiv ∘ Sum.inl) id) * star (f₂ (w.submatrix (finSumFinEquiv ∘ Sum.inr) id))) = c • (fun w : Formalism.IndefR.Datum.WO S (GlobalTheory.Doubling.dblSp T) => ω (1, h) f₁ (w.submatrix (finSumFinEquiv ∘ Sum.inl) id) * star (f₂ (w.submatrix (finSumFinEquiv ∘ Sum.inr) id)))) →
        ∀ (h : GA D T) (hd : GA D (GlobalTheory.Doubling.dblSp T)),
          Formalism.L2.Theta.mt (GlobalTheory.Doubling.dblSp T) hd =
            Matrix.reindex finSumFinEquiv finSumFinEquiv (Matrix.fromBlocks (Formalism.L2.Theta.mt T h) 0 0 1) →
          ∀ f₁ f₂ : Formalism.IndefR.Datum.WO S T → ℂ,
            Formalism.IndefR.Datum.IsThetaFunO ψ S T f₁ → Formalism.IndefR.Datum.IsThetaFunO ψ S T f₂ →
              ωd (1, hd) (fun w : Formalism.IndefR.Datum.WO S (GlobalTheory.Doubling.dblSp T) => f₁ (w.submatrix (finSumFinEquiv ∘ Sum.inl) id) * star (f₂ (w.submatrix (finSumFinEquiv ∘ Sum.inr) id))) = (fun w : Formalism.IndefR.Datum.WO S (GlobalTheory.Doubling.dblSp T) => ω (1, h) f₁ (w.submatrix (finSumFinEquiv ∘ Sum.inl) id) * star (f₂ (w.submatrix (finSumFinEquiv ∘ Sum.inr) id))) := by
  intro D ψ hψ S T χS χT ω hω ωd hωd _ h hd hhd f₁ f₂ hf₁ hf₂
  obtain ⟨ωB, hB⟩ := GlobalTheory.WeilExist.exists_isWeilDatumO D ψ hψ S (GlobalTheory.ChiGrid.negSp T) χS χT⁻¹
    (GlobalTheory.WeilParts.exists_isEtaTilde_unramified D) hω.hecS (GlobalTheory.HeckeCharsOfSquare.isHeckeChar_inv hω.hecT) hω.resS
    (resEta_inv hω.resT)
  let d := GlobalTheory.BiTransfer.sumDec T (GlobalTheory.ChiGrid.negSp T)
  have hωd' : IsWeilDatumO ψ S (GlobalTheory.Doubling.hsum T (GlobalTheory.ChiGrid.negSp T)) χS (χT * χT⁻¹) ωd := by
    rw [mul_inv_cancel]; exact hωd
  have key := (GlobalTheory.TensorTargetRule.tensorTarget D ψ hψ _ T (GlobalTheory.ChiGrid.negSp T) d S).om_box χS χT χT⁻¹ ωd ω ωB
    hωd' hω hB 1 h 1 f₁ (GlobalTheory.IndefiniteRowSideFullHecke.ConjugateAdjointDagger.cj id f₂) hf₁ (GlobalTheory.CjNeg.theta_negT hψ hf₂)
  have e1 : d.emb (h, 1) = hd := by
    apply GlobalTheory.MixModel.mt_injective (GlobalTheory.Doubling.hsum T (GlobalTheory.ChiGrid.negSp T))
    rw [GlobalTheory.SeesawSumTarget.emb_sumDec, GlobalTheory.SquareComm.mt_blk]
    refine Eq.trans ?_ hhd.symm
    rw [GlobalTheory.Doubling.blkMat, Matrix.reindex_apply, GlobalTheory.AdelicLatMove.mt_one]
  have e2 : ωB ((1 : GA D S), (1 : GA D (GlobalTheory.ChiGrid.negSp T))) = 1 := map_one ωB
  rw [e1, e2, Module.End.one_apply, GlobalTheory.SeesawSumTarget.boxT_sumDec, GlobalTheory.SeesawSumTarget.boxT_sumDec] at key
  exact key

end GlobalTheory.DblRestrictChar
