import Mathlib
open NumberField IsDedekindDomain NumberField.mixedEmbedding MeasureTheory Complex Classical
open scoped ContDiff nonZeroDivisors

namespace GlobalTheory

open NumberField IsDedekindDomain NumberField.mixedEmbedding MeasureTheory Complex Classical in
open scoped ContDiff nonZeroDivisors in

theorem _root_.GlobalTheory.existsUnique_isPrincipal_mul_classRep {K : Type} [Field K] [NumberField K]
    (b : ClassGroup (𝓞 K) → (Ideal (𝓞 K))⁰)
    (hb : Function.Bijective (fun c => ClassGroup.mk0 (b c))) (I : Ideal (𝓞 K)) (hI : I ≠ ⊥) :
    ∃! c, (I * (b c : Ideal (𝓞 K))).IsPrincipal := by
  have hI0 : I ∈ (Ideal (𝓞 K))⁰ := mem_nonZeroDivisors_of_ne_zero hI
  have key : ∀ c, (I * (b c : Ideal (𝓞 K))).IsPrincipal ↔
      ClassGroup.mk0 (b c) = (ClassGroup.mk0 ⟨I, hI0⟩)⁻¹ := by
    intro c
    rw [eq_inv_iff_mul_eq_one, mul_comm, ← map_mul]
    rw [ClassGroup.mk0_eq_one_iff]
    rfl
  obtain ⟨c, hc⟩ := hb.2 (ClassGroup.mk0 ⟨I, hI0⟩)⁻¹
  refine ⟨c, (key c).2 hc, fun c' hc' => hb.1 ?_⟩
  simp only
  rw [(key c').1 hc']
  exact hc.symm

open NumberField IsDedekindDomain NumberField.mixedEmbedding MeasureTheory Complex Classical in
open scoped ContDiff nonZeroDivisors in

theorem _root_.GlobalTheory.tsum_isPrincipal_mul_eq {K : Type} [Field K] [NumberField K]
    (B : Ideal (𝓞 K)) (hB : B ≠ ⊥) (F : Ideal (𝓞 K) → ℂ) :
    ∑' I : {I : Ideal (𝓞 K) // I ≠ ⊥ ∧ (I * B).IsPrincipal}, F (I * B) =
      ∑' J : {J : Ideal (𝓞 K) // J ≠ ⊥ ∧ J.IsPrincipal ∧ J ≤ B}, F J := by
  let e : {I : Ideal (𝓞 K) // I ≠ ⊥ ∧ (I * B).IsPrincipal} →
      {J : Ideal (𝓞 K) // J ≠ ⊥ ∧ J.IsPrincipal ∧ J ≤ B} :=
    fun I => ⟨I.1 * B, mul_ne_zero I.2.1 hB, I.2.2, Ideal.mul_le_inf.trans inf_le_right⟩
  have he : Function.Bijective e := by
    constructor
    · intro I I' h
      have h' : I.1 * B = I'.1 * B := congrArg Subtype.val h
      exact Subtype.ext (mul_right_cancel₀ hB h')
    · intro J
      obtain ⟨I, hI⟩ := Ideal.dvd_iff_le.2 J.2.2.2
      have hI0 : I ≠ ⊥ := by
        rintro rfl
        exact J.2.1 (by rw [hI, Ideal.mul_bot])
      refine ⟨⟨I, hI0, ?_⟩, Subtype.ext ?_⟩
      · rw [mul_comm, ← hI]; exact J.2.2.1
      · show I * B = J.1
        rw [hI, mul_comm]
  exact (Equiv.ofBijective e he).tsum_eq (fun J => F J.1)

end GlobalTheory
