import Mathlib
import Development.AbelianVarietyTheory.Statements.PlaceShiftStatements.NormSmall
import Development.AbelianVarietyTheory.BlockTheory.DivisorBase.WeilTensorModels
import Development.AbelianVarietyTheory.Definitions.Divisors.DiscreteSeriesHermitianUnique
import Development.AbelianVarietyTheory.Proofs.TwistedOperators.RatInOfUnit
import Development.AbelianVarietyTheory.Statements.HeckeThetaIntegrals.ThetaIntegralsAgree
import Development.AbelianVarietyTheory.PublishedTheorems.Divisors₂.HoweDualityReal
import Development.AbelianVarietyTheory.Proofs.TwistedOperators.ExistsPlacewiseSeesawTripleOverHermitianStatement
import Development.AbelianVarietyTheory.Proofs.TwistedOperators.KerPIffPolMatchZero
import Development.AbelianVarietyTheory.Proofs.TwistedOperators.PPrimeEqZeroIffPolMatch
import Development.AbelianVarietyTheory.Proofs.TwistedOperators.RankPiLeAleph0
import Development.AbelianVarietyTheory.Proofs.TwistedOperators.DixmierCommutantScalar
open Formalism.BasicNotions Formalism.BasicNotions.TH Formalism.MainNotions Formalism.MainNotions.GlobalTheoryTheory.GenusMassVolumes Formalism.MainNotions.StandardLattice Formalism.IndefR Formalism.IndefR.Datum Formalism.IndefR.Links Formalism.L2.Theta IsDedekindDomain NumberField Finset MeasureTheory
open GlobalTheory.ChiGrid GlobalTheory.FunObj GlobalTheory.FunObj.SqNum GlobalTheory.RowTest GlobalTheory.SqTop GlobalTheory.SquareComm
open GlobalTheory.IndefiniteRowSideFullHecke.UnitCharTriangle (Tri)
open scoped TensorProduct
attribute [local instance] Formalism.BasicNotions.Host.cmStar

noncomputable section

namespace GlobalTheory.BotUniq

theorem exists_equiv_of_ker {V P₀ P : Type} [AddCommGroup V] [Module ℂ V] [AddCommGroup P₀] [Module ℂ P₀]
    [AddCommGroup P] [Module ℂ P] (S : Submodule ℂ V) (p₀ : V →ₗ[ℂ] P₀) (p : V →ₗ[ℂ] P)
    (h₀ : S.map p₀ = ⊤) (h : S.map p = ⊤) (hk : ∀ x ∈ S, p₀ x = 0 ↔ p x = 0) :
    ∃ E : P₀ ≃ₗ[ℂ] P, ∀ x ∈ S, p x = E (p₀ x) := by
  have s₀ : Function.Surjective (p₀.domRestrict S) := by
    intro y
    have hy : y ∈ S.map p₀ := h₀ ▸ Submodule.mem_top
    obtain ⟨x, hx, rfl⟩ := hy
    exact ⟨⟨x, hx⟩, rfl⟩
  have s : Function.Surjective (p.domRestrict S) := by
    intro y
    have hy : y ∈ S.map p := h ▸ Submodule.mem_top
    obtain ⟨x, hx, rfl⟩ := hy
    exact ⟨⟨x, hx⟩, rfl⟩
  have hker : LinearMap.ker (p₀.domRestrict S) = LinearMap.ker (p.domRestrict S) := by
    ext x
    simp only [LinearMap.mem_ker, LinearMap.domRestrict_apply]
    exact hk x.1 x.2
  let E : P₀ ≃ₗ[ℂ] P := ((p₀.domRestrict S).quotKerEquivOfSurjective s₀).symm.trans
    ((Submodule.quotEquivOfEq _ _ hker).trans ((p.domRestrict S).quotKerEquivOfSurjective s))
  refine ⟨E, fun x hx => ?_⟩
  have e1 : ((p₀.domRestrict S).quotKerEquivOfSurjective s₀).symm (p₀ x) = Submodule.Quotient.mk ⟨x, hx⟩ := by
    rw [LinearEquiv.symm_apply_eq]; rfl
  show p x = ((p.domRestrict S).quotKerEquivOfSurjective s) ((Submodule.quotEquivOfEq _ _ hker)
    (((p₀.domRestrict S).quotKerEquivOfSurjective s₀).symm (p₀ x)))
  rw [e1]
  rfl

theorem intertwine {V P₀ P : Type} [AddCommGroup V] [Module ℂ V] [AddCommGroup P₀] [Module ℂ P₀]
    [AddCommGroup P] [Module ℂ P] (S : Submodule ℂ V) (p₀ : V →ₗ[ℂ] P₀) (p : V →ₗ[ℂ] P) (E : P₀ ≃ₗ[ℂ] P)
    (hE : ∀ x ∈ S, p x = E (p₀ x)) (h₀ : S.map p₀ = ⊤) (a : Module.End ℂ P₀) (b : Module.End ℂ P)
    (H : ∀ Φ ∈ S, ∃ Φ' ∈ S, p₀ Φ' = a (p₀ Φ) ∧ p Φ' = b (p Φ)) (x : P₀) : E (a x) = b (E x) := by
  have hx : x ∈ S.map p₀ := h₀ ▸ Submodule.mem_top
  obtain ⟨Φ, hΦ, rfl⟩ := hx
  obtain ⟨Φ', hΦ', h1, h2⟩ := H Φ hΦ
  rw [← h1, ← hE Φ' hΦ', h2, hE Φ hΦ]

theorem comm_of_intertwine {P₀ P : Type} [AddCommGroup P₀] [Module ℂ P₀] [AddCommGroup P] [Module ℂ P]
    (E E' : P₀ ≃ₗ[ℂ] P) (a : Module.End ℂ P₀) (b : Module.End ℂ P) (hE : ∀ x, E (a x) = b (E x))
    (hE' : ∀ x, E' (a x) = b (E' x)) :
    ((E.trans E'.symm : P₀ ≃ₗ[ℂ] P₀) : Module.End ℂ P₀) * a = a * ((E.trans E'.symm : P₀ ≃ₗ[ℂ] P₀) : Module.End ℂ P₀) := by
  ext x
  simp only [Module.End.mul_apply, LinearEquiv.coe_coe, LinearEquiv.trans_apply]
  rw [hE, LinearEquiv.symm_apply_eq, hE', LinearEquiv.apply_symm_apply]

end GlobalTheory.BotUniq

end
