import Mathlib
import Development.AbelianVarietyTheory.Definitions.Divisors.AxesVanishingStructureSheafDegOne
import Development.AbelianVarietyTheory.Definitions.AbelianSchemeLaws.IsScalarTowerOfAlgebraMapEq
import Development.AbelianVarietyTheory.PublishedTheorems.Divisors₂.AbelianHodgeDeRham
open CategoryTheory CategoryTheory.Limits AlgebraicGeometry KaehlerDifferential MonoidalCategory CartesianMonoidalCategory MonObj TensorProduct
open ReciprocityTheory.ChartRestriction.G ReciprocityTheory.ChartRestriction.G.Geo.Tor ReciprocityTheory.ChartRestriction.G.Semi GlobalTheory.CatCoh GlobalTheory.Cris GlobalTheory.CechDR GlobalTheory.AbCoh
attribute [local instance] Over.cartesianMonoidalCategory
set_option autoImplicit false

namespace GlobalTheory

theorem artinianLocal_finiteLength_and_socle_element :
    ∀ (R : Type) [CommRing R] [IsArtinianRing R] [IsLocalRing R],
      (∃ n : ℕ, Module.length R R = n) ∧
      (¬ IsField R → ∃ t : R, t ≠ 0 ∧ (∀ a ∈ IsLocalRing.maximalIdeal R, a * t = 0) ∧
        Module.length (R ⧸ Ideal.span {t}) (R ⧸ Ideal.span {t}) < Module.length R R) := by
  intro R _ _ _
  have hfin : IsFiniteLength R R :=
    isFiniteLength_iff_isNoetherian_isArtinian.2 ⟨inferInstance, inferInstance⟩
  have hne : Module.length R R ≠ ⊤ := Module.length_ne_top_iff.2 hfin
  refine ⟨⟨(Module.length R R).toNat, (ENat.natCast_toNat hne).symm⟩, ?_⟩
  intro hnf
  classical
  set m := IsLocalRing.maximalIdeal R with hmdef
  obtain ⟨k, hk⟩ := IsArtinianRing.isNilpotent_jacobson_bot (R := R)
  rw [IsLocalRing.jacobson_eq_maximalIdeal ⊥ bot_ne_top] at hk
  have hex : ∃ n : ℕ, m ^ n = ⊥ := ⟨k, hk⟩
  have hn : m ^ Nat.find hex = ⊥ := Nat.find_spec hex
  have hn0 : Nat.find hex ≠ 0 := by
    intro h
    rw [h, pow_zero, Ideal.one_eq_top] at hn
    exact top_ne_bot hn
  obtain ⟨j, hj⟩ := Nat.exists_eq_succ_of_ne_zero hn0
  have hjne : m ^ j ≠ ⊥ := Nat.find_min hex (by omega)
  obtain ⟨t, htm, ht0⟩ := Submodule.exists_mem_ne_zero_of_ne_bot hjne
  have hkill : ∀ a ∈ m, a * t = 0 := by
    intro a ha
    have h1 : a * t ∈ m ^ (j + 1) := by
      rw [pow_succ']
      exact Ideal.mul_mem_mul ha htm
    have hj' : Nat.find hex = j + 1 := hj
    rw [← hj', hn] at h1
    exact (Submodule.mem_bot R).1 h1
  refine ⟨t, ht0, hkill, ?_⟩
  set I : Ideal R := Ideal.span {t}
  rw [Module.length_eq_of_surjective (S := R) (R := R ⧸ I) (M := R ⧸ I)
    Ideal.Quotient.mk_surjective |>.symm]
  have hadd := Module.length_eq_add_of_exact (R := R) I.subtype I.mkQ I.injective_subtype
    I.mkQ_surjective (LinearMap.exact_subtype_mkQ I)
  have hI : Module.length R I ≠ 0 := by
    rw [Ne, Module.length_eq_zero_iff]
    intro hs
    have : (⟨t, Ideal.subset_span rfl⟩ : I) = 0 := Subsingleton.elim _ _
    exact ht0 (congrArg Subtype.val this)
  have hQ : Module.length R (R ⧸ I) ≠ ⊤ := by
    intro h
    rw [hadd, h] at hne
    simp at hne
  rw [hadd]
  lift Module.length R (R ⧸ I) to ℕ using hQ with q
  lift Module.length R I to ℕ using (by
    intro h; rw [hadd, h] at hne; simp at hne) with i
  norm_cast
  norm_cast at hI
  omega

end GlobalTheory
