import Development.AbelianVarietyTheory.Proofs.DivisorProofs.CosetVecEq
import Formalism.LocalToGlobal.GlobalTheory.GL1.CompactFixesLattice.AdeleIntA

noncomputable section

open IsDedekindDomain NumberField
open scoped TensorProduct

namespace GlobalTheory.AdeleTensorModule

theorem adele_mod (F : Type) [Field F] [NumberField F] (N : ℕ) (hN : 1 ≤ N)
    (y : FiniteAdeleRing (𝓞 F) F) (hy : y ∈ Formalism.UG.intAd F) :
    ∃ a : 𝓞 F, ∃ w ∈ Formalism.UG.intAd F,
      y = algebraMap F (FiniteAdeleRing (𝓞 F) F) (a : F) + (N : FiniteAdeleRing (𝓞 F) F) * w := by
  have hN0 : (N : 𝓞 F) ≠ 0 := Nat.cast_ne_zero.mpr (by omega)
  have hNF : (N : F) ≠ 0 := Nat.cast_ne_zero.mpr (by omega)
  obtain ⟨a, ha⟩ := Formalism.L2.GlobalTheoryTheory.FiltrationBoundaryCriterion.CompactFixesLatticeTheory.AdeleIntA.exists_approx (R := 𝓞 F) (K := F) y hy (N : 𝓞 F) hN0
  refine ⟨a, algebraMap F (FiniteAdeleRing (𝓞 F) F) ((N : F)⁻¹) *
    (y - algebraMap F (FiniteAdeleRing (𝓞 F) F) (a : F)), fun v => ?_, ?_⟩
  · show algebraMap F (v.adicCompletion F) ((N : F)⁻¹) *
      (y v - algebraMap F (v.adicCompletion F) (algebraMap (𝓞 F) F a)) ∈ v.adicCompletionIntegers F
    rw [HeightOneSpectrum.mem_adicCompletionIntegers, Valuation.map_mul, map_inv₀, map_inv₀]
    have hv : Valued.v (algebraMap F (v.adicCompletion F) (N : F)) = v.intValuation (N : 𝓞 F) := by
      have := Formalism.L2.GlobalTheoryTheory.FiltrationBoundaryCriterion.CompactFixesLatticeTheory.AdeleIntA.val_R (K := F) v (N : 𝓞 F)
      rwa [map_natCast (algebraMap (𝓞 F) F)] at this
    have h0 : v.intValuation (N : 𝓞 F) ≠ 0 := v.intValuation_ne_zero _ hN0
    rw [hv]
    calc (v.intValuation (N : 𝓞 F))⁻¹ * Valued.v (y v - algebraMap F (v.adicCompletion F) (algebraMap (𝓞 F) F a))
        ≤ (v.intValuation (N : 𝓞 F))⁻¹ * v.intValuation (N : 𝓞 F) := by
          gcongr
          exact ha v
      _ = 1 := inv_mul_cancel₀ h0
  · rw [← mul_assoc, ← map_natCast (algebraMap F (FiniteAdeleRing (𝓞 F) F)) N, ← map_mul,
      mul_inv_cancel₀ hNF, map_one, one_mul]
    ring

theorem tens_mod (F E : Type) [Field F] [NumberField F] [Field E] [NumberField E] [Algebra F E]
    (N : ℕ) (hN : 1 ≤ N) (x : E ⊗[F] FiniteAdeleRing (𝓞 F) F)
    (hx : x ∈ Formalism.UG.tensInt (F := F) (FiniteAdeleRing (𝓞 F) F) (Formalism.UG.intOE E) (Formalism.UG.intAd F)) :
    ∃ a ∈ Formalism.UG.intOE E,
      ∃ z ∈ Formalism.UG.tensInt (F := F) (FiniteAdeleRing (𝓞 F) F) (Formalism.UG.intOE E) (Formalism.UG.intAd F),
        x = Formalism.UG.inclE (F := F) (FiniteAdeleRing (𝓞 F) F) a +
          (N : E ⊗[F] FiniteAdeleRing (𝓞 F) F) * z := by
  set S := Formalism.UG.tensInt (F := F) (FiniteAdeleRing (𝓞 F) F) (Formalism.UG.intOE E) (Formalism.UG.intAd F) with hS
  set ι := Formalism.UG.inclE (F := F) (E := E) (FiniteAdeleRing (𝓞 F) F) with hι
  have hl : ∀ a ∈ Formalism.UG.intOE E, ι a ∈ S := fun a ha =>
    Subring.subset_closure (Or.inl ⟨⟨a, ha⟩, rfl⟩)
  have hr : ∀ w ∈ Formalism.UG.intAd F, (1 : E) ⊗ₜ[F] w ∈ S := fun w hw =>
    Subring.subset_closure (Or.inr ⟨⟨w, hw⟩, rfl⟩)
  induction hx using Subring.closure_induction with
  | mem x hx =>
    rcases hx with ⟨a, rfl⟩ | ⟨y, rfl⟩
    · exact ⟨a, a.2, 0, zero_mem _, by rw [mul_zero, add_zero]; rfl⟩
    · obtain ⟨a, w, hw, e⟩ := adele_mod F N hN y y.2
      refine ⟨algebraMap F E (a : F), ⟨algebraMap (𝓞 F) (𝓞 E) a, rfl⟩, (1 : E) ⊗ₜ[F] w, hr w hw, ?_⟩
      have e2 := congrArg (Algebra.TensorProduct.includeRight (R := F) (A := E)
        (B := FiniteAdeleRing (𝓞 F) F)) e
      rw [map_add, map_mul, map_natCast, AlgHom.commutes] at e2
      exact e2
  | zero => exact ⟨0, zero_mem _, 0, zero_mem _, by rw [map_zero, mul_zero, add_zero]⟩
  | one => exact ⟨1, one_mem _, 0, zero_mem _, by rw [map_one, mul_zero, add_zero]⟩
  | add x y _ _ ihx ihy =>
    obtain ⟨a, ha, z, hz, rfl⟩ := ihx
    obtain ⟨b, hb, w, hw, rfl⟩ := ihy
    exact ⟨a + b, add_mem ha hb, z + w, add_mem hz hw, by rw [map_add]; ring⟩
  | neg x _ ihx =>
    obtain ⟨a, ha, z, hz, rfl⟩ := ihx
    exact ⟨-a, neg_mem ha, -z, neg_mem hz, by rw [map_neg]; ring⟩
  | mul x y _ _ ihx ihy =>
    obtain ⟨a, ha, z, hz, rfl⟩ := ihx
    obtain ⟨b, hb, w, hw, rfl⟩ := ihy
    refine ⟨a * b, mul_mem ha hb, ι a * w + z * ι b + (N : E ⊗[F] FiniteAdeleRing (𝓞 F) F) * (z * w),
      add_mem (add_mem (mul_mem (hl a ha) hw) (mul_mem hz (hl b hb)))
        (mul_mem (natCast_mem S N) (mul_mem hz hw)), ?_⟩
    rw [map_mul]; ring

theorem main : ∀ {D : Formalism.BasicNotions.D7} (N : ℕ), (1 : ℕ) ≤ N → ∀ x ∈ Formalism.MainNotions.StandardLatticesGL3.OhE D,
    ∃ a ∈ Formalism.UG.intOE ((Formalism.BasicNotions.D7.host D).E), ∃ z ∈ Formalism.MainNotions.StandardLatticesGL3.OhE D,
      x = (Formalism.MainNotions.StandardLatticesGL3.inc D : (Formalism.BasicNotions.D7.host D).E → Formalism.MainNotions.StandardLatticesGL3.AE D) a +
        (↑N : Formalism.MainNotions.StandardLatticesGL3.AE D) * z :=
  fun {D} N hN x hx => tens_mod ℚ D.host.E N hN x hx

end GlobalTheory.AdeleTensorModule

end
