import Mathlib
import Development.AbelianVarietyTheory.Definitions.CMLattices.LamLetters
import Development.AbelianVarietyTheory.Definitions.CMLattices.ExistsIsThetaDegOfIsLocalModel
import Development.AbelianVarietyTheory.Proofs.ThetaLawProofs.IsLieBasisIffIsUnitDet
import Development.AbelianVarietyTheory.Proofs.CubicalLineBundles.ExistsUniqueDifferentialMonHomLieBasisStatement
import Development.AbelianVarietyTheory.Proofs.CubicalLineBundles.ExistsLieRepresentationEigenLatticesStatement

set_option autoImplicit false

open AlgebraicGeometry CategoryTheory Limits MonoidalCategory CartesianMonoidalCategory MonObj NumberField IsDedekindDomain
open ReciprocityTheory.ChartRestriction.G ReciprocityTheory.ChartRestriction.G.Semi ReciprocityTheory.ChartRestriction.G.Geo.Tor ReciprocityTheory.GeometryOfNumbersTheory Formalism.L2.LamCM
open scoped CategoryTheory.Obj ComplexOrder
open GlobalTheory GlobalTheory.CM GlobalTheory.LamF

namespace GlobalTheory.LamF.EigLine

theorem ringHom_ext_ringOfIntegers {E K : Type} [Field E] [NumberField E] [Field K] {f g : E →+* K}
    (h : ∀ a : 𝓞 E, f (a : E) = g (a : E)) : f = g :=
  IsLocalization.ringHom_ext (nonZeroDivisors (𝓞 E)) (RingHom.ext h)

theorem forall_apply_eq_smul_iff {K O : Type} [Field K] {n : ℕ} (r : O → Module.End K (Fin n → K)) (χ : Fin n → O → K)
    (hχ : Function.Injective χ) (hr : ∀ a j, r a (Pi.single j 1) = Pi.single j (χ j a)) (i : Fin n) (c : Fin n → K) :
    (∀ a, r a c = χ i a • c) ↔ ∃ s : K, c = Pi.single i s := by
  have hd : ∀ a (d : Fin n → K) j, r a d j = χ j a * d j := fun a d j => by
    conv_lhs => rw [pi_eq_sum_univ' d, map_sum]
    simp only [map_smul, hr, Finset.sum_apply, Pi.smul_apply, smul_eq_mul]
    rw [Finset.sum_eq_single j (fun k _ hk => by simp [Pi.single_eq_of_ne hk.symm]) (by simp), Pi.single_eq_same,
      mul_comm]
  constructor
  · intro h
    refine ⟨c i, funext fun j => ?_⟩
    by_cases hj : j = i
    · rw [hj, Pi.single_eq_same]
    · rw [Pi.single_eq_of_ne hj]
      by_contra hc
      refine hj (hχ (funext fun a => mul_right_cancel₀ hc ?_))
      rw [← hd, h a, Pi.smul_apply, smul_eq_mul]
  · rintro ⟨s, rfl⟩ a
    funext j
    rw [hd, Pi.smul_apply, smul_eq_mul]
    by_cases hj : j = i
    · rw [hj]
    · rw [Pi.single_eq_of_ne hj, mul_zero, mul_zero]

theorem mem_eigLat_iff : ∀ (E : Type) [Field E] [NumberField E] [StarRing (𝓞 E)] (K : Type) [Field K] [NumberField K] (A : Out.PolAV (Sp (𝓞 K))) (x : KeyIn K) (φ : x.P.A.left ⟶ A.A.left) (hf : IsPolFibre (algebraMap (𝓞 K) K) A x.P φ) (oa : A.OAct (𝓞 E)) (act : OAct (𝓞 E) A.A) (S : Set (E →+* ℂ)) (τ : Fin x.P.g ≃ S) (lab : (E →+* ℂ) → (E →+* K)), act.act = oa.act → Function.Injective lab → IsLieType A oa x φ τ lab → ∀ (i : Fin x.P.g) (t : 𝓞 E →+* 𝓞 K), (∀ a : 𝓞 E, (t a : K) = lab (τ i) (a : E)) → ∀ c, c ∈ (GenFib.mk x.P φ hf x.v x.basis : GenFib A).eigLat act t ↔ c ∈ (GenFib.mk x.P φ hf x.v x.basis : GenFib A).lieLat ∧ ∃ r : K, c = Pi.single i r := by
  intro E _ _ _ K _ _ A x φ hf oa act S τ lab hact hlab hty i t ht c
  obtain ⟨r, hr, he⟩ := GlobalTheory.T.exists_lieRepresentation_eigenLattices K A (GenFib.mk x.P φ hf x.v x.basis) (𝓞 E) act
  have := x.P.isAb.comm
  have halg := LieComb.lieComb_algebra K x.P.A x.P.g x.v x.basis.1
  have hdiag : ∀ (a : 𝓞 E) (j : Fin x.P.g), r a (Pi.single j 1) = Pi.single j (lab (τ j) (a : E)) := fun a j => by
    obtain ⟨-, f₀, -, e, h⟩ := GlobalTheory.T.existsUnique_differential_monHom_lieBasis K A (GenFib.mk x.P φ hf x.v x.basis) A (GenFib.mk x.P φ hf x.v x.basis)
      (act.act a) (act.hom a)
    obtain ⟨w, hw, hw'⟩ := hty a j
    have h₁ : IsLieComb (r a (Pi.single j 1)) x.v (x.v j ≫ f₀) := h _ (hr a) _ _ (halg.2.2.2.1 j)
    have e' : f₀.left ≫ φ = φ ≫ (oa.act a).left := by rw [← hact]; exact e
    obtain rfl : x.v j ≫ f₀ = w := Over.OverMorphism.ext (hf.2.1.1.hom_ext
      (by rw [Over.comp_left, Category.assoc, e', hw']) ((Over.w _).trans (Over.w _).symm))
    exact (x.basis.2 _ (halg.2.1 _ _ hw)).unique h₁ hw
  have hχ : Function.Injective fun (j : Fin x.P.g) (a : 𝓞 E) => lab (τ j) (a : E) := fun j k h =>
    τ.injective (Subtype.ext (hlab (ringHom_ext_ringOfIntegers fun a => congrFun h a)))
  refine (he t c).1.trans (and_congr_right fun _ => ?_)
  rw [← forall_apply_eq_smul_iff (fun a => r a) _ hχ hdiag i c]
  refine forall_congr' fun a => ?_
  rw [← ht a, ← algebraMap_smul K (t a) c]

end GlobalTheory.LamF.EigLine
