import Thesis.Congruences
import Thesis.Teichmuller

/-!
# Finite-level `Δ`/`Γ` splitting and the two-kind character decomposition

Swarm `smwl46mfx`, member `m8`, direction (C).

The thesis (Definition 3.2, equation (3.9)) decomposes the units modulo `p^(m+1)` into a tame
(Teichmüller) part and a principal part, and correspondingly decomposes every Dirichlet
character of level `p^(m+1)` as a product `χ = θ · ψ` of a character `θ` of level `p` and a
character `ψ` of `p`-power order.  `Thesis.Teich` proves the *infinite-level* statement
`ℤ_p^× ≃ (ℤ/p)^× × (1+pℤ_p)` (`Teich.padicUnitsMulEquiv`); here we prove the *finite-level*
statement together with the resulting character splitting, exactly as needed by
`Thesis.combineFirstSecondKind` / `Thesis.IsSecondKind`.

Main results (all in namespace `Thesis.FiniteDG`):

* `existsUnique_tame_principal` — every unit modulo `p^(m+1)` is *uniquely* a `(p-1)`-torsion
  unit times a principal unit (finite-level (3.9)).
* `pow_pow_eq_one_of_isPrincipal` — the principal units have exponent `p^m` (finite LTE).
* `existsUnique_combineFirstSecondKind` — every character of level `p^(m+1)` is *uniquely*
  `combineFirstSecondKind m θ ψ` with `IsSecondKind p m ψ` (existence, uniqueness,
  non-vacuity/surjectivity of the product map).
* `isSecondKind_iff_kills_torsion` — `IsSecondKind p m ψ` iff `ψ` is trivial on the
  `(p-1)`-torsion of `(ℤ/p^(m+1))^×`, i.e. iff `ψ ∈ Γ̂_m`.
* `exists_zpow_teichmuller_combine` — the first-kind part may be taken to be `ω^k`
  (thesis `Δ̂ = ⟨ω⟩`), using `Thesis.Teich.exists_zpow_eq_teichmullerCharacter`.
* `finite_delta_gamma` — the assembled statement of all of the above.

Build (from this directory, with `PROJ` the path of `thesis-formalization`):

```bash
LEAN_PATH=$PROJ/.lake/build/lib/lean:$LEAN_PATH lean FiniteDeltaGamma.lean
```

The file has no `sorry`, no `axiom`, no `opaque`; `#print axioms` on every declaration lists
only `propext`, `Classical.choice`, `Quot.sound`.
-/

namespace Thesis
namespace FiniteDG

set_option linter.style.haveILetI false

open _root_.DirichletCharacter
open scoped BigOperators

variable {p : ℕ} [hp : Fact p.Prime]

/-! ## §1 Number-theoretic input -/

/-- `p` and `p - 1` are coprime. -/
theorem prime_coprime_sub_one : Nat.Coprime p (p - 1) := by
  rw [Nat.Prime.coprime_iff_not_dvd hp.out]
  intro hdiv
  have h2 : 2 ≤ p := hp.out.two_le
  have hpos : 0 < p - 1 := by omega
  have hle := Nat.le_of_dvd hpos hdiv
  omega

/-- `p ^ m` and `p - 1` are coprime. -/
theorem coprime_pow_sub_one (m : ℕ) : Nat.Coprime (p ^ m) (p - 1) :=
  prime_coprime_sub_one (p := p).pow_left m

/-- **Bezout input.**  There are natural numbers `v, k` with `(p-1) * v = 1 + p^m * k`,
so that `v` inverts `p-1` modulo `p^m`. -/
theorem exists_bezout (m : ℕ) : ∃ v k : ℕ, (p - 1) * v = 1 + p ^ m * k := by
  rcases Nat.eq_zero_or_pos m with hm | hm
  · subst hm
    refine ⟨1, p - 2, ?_⟩
    have h2 : 2 ≤ p := hp.out.two_le
    simp only [pow_zero, mul_one]
    omega
  · haveI : NeZero (p ^ m) := ⟨pow_ne_zero _ hp.out.ne_zero⟩
    have hunit : IsUnit (((p - 1 : ℕ) : ZMod (p ^ m))) := by
      rw [ZMod.isUnit_iff_coprime]
      exact (coprime_pow_sub_one m).symm
    refine ⟨(((hunit.unit)⁻¹ : (ZMod (p ^ m))ˣ) : ZMod (p ^ m)).val, ?_⟩
    set v : ℕ := (((hunit.unit)⁻¹ : (ZMod (p ^ m))ˣ) : ZMod (p ^ m)).val with hv
    have hcoe : ((v : ℕ) : ZMod (p ^ m)) = ((hunit.unit)⁻¹ : (ZMod (p ^ m))ˣ) :=
      ZMod.natCast_zmod_val _
    have hprod : (((p - 1) * v : ℕ) : ZMod (p ^ m)) = 1 := by
      have hcast : (((p - 1) * v : ℕ) : ZMod (p ^ m))
          = (((p - 1 : ℕ) : ZMod (p ^ m))) * ((v : ℕ) : ZMod (p ^ m)) := by
        push_cast
        ring
      have hval : ((hunit.unit : (ZMod (p ^ m))ˣ) : ZMod (p ^ m)) = ((p - 1 : ℕ) : ZMod (p ^ m)) :=
        IsUnit.unit_spec hunit
      have h2 : (((p - 1 : ℕ) : ZMod (p ^ m))) *
          (((hunit.unit)⁻¹ : (ZMod (p ^ m))ˣ) : ZMod (p ^ m)) = 1 := by
        have h := Units.mul_inv hunit.unit
        rwa [hval] at h
      rw [hcast, hcoe, h2]
    have hmod : (p - 1) * v ≡ 1 [MOD p ^ m] := by
      rw [← ZMod.natCast_eq_natCast_iff]
      simpa using hprod
    have hpos : 1 ≤ (p - 1) * v := by
      rcases Nat.eq_zero_or_pos ((p - 1) * v) with h0 | h0
      · rw [h0] at hmod
        have hdvd : p ^ m ∣ 1 := by
          have := (Nat.modEq_iff_dvd' (Nat.zero_le 1)).mp hmod
          simpa using this
        have h2 : 2 ≤ p := hp.out.two_le
        have hle : 2 ≤ p ^ m := le_trans h2 (Nat.le_self_pow (by omega : m ≠ 0) p)
        have := Nat.le_of_dvd (by norm_num) hdvd
        omega
      · exact h0
    obtain ⟨k, hk⟩ := (Nat.modEq_iff_dvd' hpos).mp hmod.symm
    exact ⟨k, by omega⟩

/-- A chosen Bezout coefficient: `(p-1) * bezoutV v = 1 + p^m * k`. -/
noncomputable def bezoutV (m : ℕ) : ℕ := (exists_bezout (p := p) m).choose

theorem bezoutV_spec (m : ℕ) : ∃ k : ℕ, (p - 1) * bezoutV (p := p) m = 1 + p ^ m * k :=
  (exists_bezout (p := p) m).choose_spec

/-! ## §2 The finite-level LTE step -/

/-- The geometric sum `∑_{i<p} u^i` is divisible by `p` as soon as `u ≡ 1 (mod p)`. -/
theorem dvd_geom_sum_of_dvd_sub_one {u : ℤ} (hu : (p : ℤ) ∣ u - 1) :
    (p : ℤ) ∣ ∑ i ∈ Finset.range p, u ^ i := by
  have hterm : ∀ i : ℕ, (p : ℤ) ∣ u ^ i - 1 := fun i =>
    dvd_trans hu (by simpa using sub_dvd_pow_sub_pow u 1 i)
  have hsum : (p : ℤ) ∣ ∑ i ∈ Finset.range p, (u ^ i - 1) := Finset.dvd_sum fun i _ => hterm i
  have hsplit : (∑ i ∈ Finset.range p, u ^ i) - (p : ℤ)
      = ∑ i ∈ Finset.range p, (u ^ i - 1) := by
    rw [Finset.sum_sub_distrib]
    simp
  have heq : (∑ i ∈ Finset.range p, (u ^ i - 1)) + (p : ℤ) = ∑ i ∈ Finset.range p, u ^ i := by
    rw [← hsplit]; ring
  rw [← heq]
  exact dvd_add hsum (dvd_refl (p : ℤ))

/-- **Finite-level LTE.**  If `a ≡ 1 (mod p)` then `a ^ (p^m) ≡ 1 (mod p^(m+1))`. -/
theorem pow_pow_sub_one_dvd (m : ℕ) (a : ℤ) (ha : (p : ℤ) ∣ a - 1) :
    (p : ℤ) ^ (m + 1) ∣ a ^ (p ^ m) - 1 := by
  induction m with
  | zero => simpa using ha
  | succ m ih =>
    obtain ⟨z, hz⟩ := ih
    have haz : a ^ (p ^ m) = 1 + (p : ℤ) ^ (m + 1) * z := by linarith
    have hPp : (p : ℤ) ∣ (p : ℤ) ^ (m + 1) := dvd_pow_self (p : ℤ) (by omega)
    have hS : (p : ℤ) ∣ ∑ i ∈ Finset.range p, (1 + (p : ℤ) ^ (m + 1) * z) ^ i := by
      refine dvd_geom_sum_of_dvd_sub_one ?_
      rw [show (1 + (p : ℤ) ^ (m + 1) * z) - 1 = (p : ℤ) ^ (m + 1) * z by ring]
      exact dvd_mul_of_dvd_left hPp z
    have hgoal : a ^ (p ^ (m + 1)) - 1
        = (p : ℤ) ^ (m + 1) * z * ∑ i ∈ Finset.range p, (1 + (p : ℤ) ^ (m + 1) * z) ^ i := by
      rw [Nat.pow_succ, pow_mul, haz]
      have hg := mul_geom_sum (1 + (p : ℤ) ^ (m + 1) * z) p
      rw [show (1 + (p : ℤ) ^ (m + 1) * z) - 1 = (p : ℤ) ^ (m + 1) * z by ring] at hg
      rw [← hg]
    rw [hgoal]
    obtain ⟨w, hw⟩ := hS
    rw [hw]
    exact ⟨z * w, by ring⟩

/-- The residue of a unit modulo `p` is `1` exactly for the principal units. -/
theorem isPrincipal_iff {m : ℕ} (x : (ZMod (p ^ (m + 1)))ˣ) :
    ZMod.unitsMap (dvd_pow_self p (Nat.succ_ne_zero m)) x = 1
      ↔ ((x : ZMod (p ^ (m + 1))).cast : ZMod p) = 1 := by
  constructor
  · intro h
    have := congrArg (fun u : (ZMod p)ˣ => (u : ZMod p)) h
    simpa [ZMod.unitsMap_val] using this
  · intro h
    refine Units.ext ?_
    rw [ZMod.unitsMap_val, Units.val_one]
    exact h

/-- **The principal units are killed by the `p^m`-th power.**  This is the finite-level
statement that `1 + pℤ/p^(m+1)ℤ` has exponent dividing `p^m`. -/
theorem pow_pow_eq_one_of_isPrincipal {m : ℕ} (x : (ZMod (p ^ (m + 1)))ˣ)
    (hx : ZMod.unitsMap (dvd_pow_self p (Nat.succ_ne_zero m)) x = 1) : x ^ (p ^ m) = 1 := by
  haveI : NeZero (p ^ (m + 1)) := ⟨pow_ne_zero _ hp.out.ne_zero⟩
  have hcast : ((x : ZMod (p ^ (m + 1))).cast : ZMod p) = 1 := (isPrincipal_iff x).mp hx
  set a : ℕ := (x : ZMod (p ^ (m + 1))).val with ha
  have hax : ((a : ℕ) : ZMod (p ^ (m + 1))) = (x : ZMod (p ^ (m + 1))) := ZMod.natCast_zmod_val _
  have ha1 : ((a : ℕ) : ZMod p) = 1 := by
    rw [← ZMod.cast_natCast (dvd_pow_self p (Nat.succ_ne_zero m)) a, hax]
    exact hcast
  have hdvd : (p : ℤ) ∣ (a : ℤ) - 1 := by
    have h0 : (((a : ℤ) - 1 : ℤ) : ZMod p) = 0 := by
      push_cast
      rw [ha1]; ring
    exact (ZMod.intCast_zmod_eq_zero_iff_dvd _ p).mp h0
  have hlte := pow_pow_sub_one_dvd (p := p) m (a : ℤ) hdvd
  have hzero : ((((a : ℤ) ^ (p ^ m) - 1) : ℤ) : ZMod (p ^ (m + 1))) = 0 := by
    rw [ZMod.intCast_zmod_eq_zero_iff_dvd]
    have hcastN : ((p ^ (m + 1) : ℕ) : ℤ) = (p : ℤ) ^ (m + 1) := by push_cast; ring
    rwa [hcastN]
  refine Units.ext ?_
  rw [Units.val_pow_eq_pow_val, Units.val_one, ← hax]
  have hval : (((a : ℤ) ^ (p ^ m) : ℤ) : ZMod (p ^ (m + 1))) = 1 := by
    have h := hzero
    push_cast at h ⊢
    linear_combination h
  calc (((a : ℕ) : ZMod (p ^ (m + 1)))) ^ (p ^ m)
      = (((a : ℤ) ^ (p ^ m) : ℤ) : ZMod (p ^ (m + 1))) := by push_cast; ring
    _ = 1 := hval


/-! ## §3 The finite-level `Δ`/`Γ` splitting of the unit group -/

/-- Every unit modulo `p^(m+1)` has order dividing `p^m * (p-1)`. -/
theorem pow_card_units_prime_pow (m : ℕ) (x : (ZMod (p ^ (m + 1)))ˣ) :
    x ^ (p ^ m * (p - 1)) = 1 := by
  haveI : NeZero (p ^ (m + 1)) := ⟨pow_ne_zero _ hp.out.ne_zero⟩
  have hcard : Fintype.card (ZMod (p ^ (m + 1)))ˣ = p ^ m * (p - 1) := by
    rw [ZMod.card_units_eq_totient, Nat.totient_prime_pow hp.out (Nat.succ_pos m)]
    norm_num
  have h := pow_card_eq_one (x := x)
  rwa [hcard] at h

/-- A unit that is killed by both `p^m` and `p-1` is trivial (coprimality). -/
theorem eq_one_of_pow_primePow_of_pow_sub_one {m : ℕ} {ρ : (ZMod (p ^ (m + 1)))ˣ}
    (he : ρ ^ (p ^ m) = 1) (hf : ρ ^ (p - 1) = 1) : ρ = 1 := by
  have h1 : orderOf ρ ∣ p ^ m := orderOf_dvd_iff_pow_eq_one.mpr he
  have h2 : orderOf ρ ∣ p - 1 := orderOf_dvd_iff_pow_eq_one.mpr hf
  have h3 : orderOf ρ ∣ 1 := by
    have hg : Nat.gcd (p ^ m) (p - 1) = 1 := (coprime_pow_sub_one (p := p) m).gcd_eq_one
    have h4 : orderOf ρ ∣ Nat.gcd (p ^ m) (p - 1) := Nat.dvd_gcd h1 h2
    rwa [hg] at h4
  exact orderOf_eq_one_iff.mp (Nat.dvd_one.mp h3)

/-- The reduction map `(ℤ/p^(m+1))^× → (ℤ/p)^×`. -/
def reduction (m : ℕ) : (ZMod (p ^ (m + 1)))ˣ →* (ZMod p)ˣ :=
  ZMod.unitsMap (dvd_pow_self p (Nat.succ_ne_zero m))

theorem reduction_def (m : ℕ) : reduction (p := p) m =
    ZMod.unitsMap (dvd_pow_self p (Nat.succ_ne_zero m)) := rfl

/-- The principal part `⟨x⟩ = x^(p-1)v` of a unit modulo `p^(m+1)`; it is `≡ 1 mod p`. -/
noncomputable def principalPartFinite (m : ℕ) (x : (ZMod (p ^ (m + 1)))ˣ) :
    (ZMod (p ^ (m + 1)))ˣ :=
  x ^ ((p - 1) * bezoutV (p := p) m)

/-- The tame part `ω(x) = x·⟨x⟩⁻¹` of a unit modulo `p^(m+1)`; it is `(p-1)`-torsion. -/
noncomputable def tamePartFinite (m : ℕ) (x : (ZMod (p ^ (m + 1)))ˣ) :
    (ZMod (p ^ (m + 1)))ˣ :=
  x * (principalPartFinite m x)⁻¹

/-- Every level-`p` unit has order dividing `p-1`. -/
theorem reduction_pow_sub_one (a : (ZMod p)ˣ) : a ^ (p - 1) = 1 := by
  have h := pow_card_eq_one (x := a)
  rwa [ZMod.card_units] at h

theorem principalPartFinite_mem (m : ℕ) (x : (ZMod (p ^ (m + 1)))ˣ) :
    reduction m (principalPartFinite m x) = 1 := by
  rw [principalPartFinite, map_pow, pow_mul, reduction_pow_sub_one, one_pow]

theorem tamePartFinite_pow_sub_one (m : ℕ) (x : (ZMod (p ^ (m + 1)))ˣ) :
    tamePartFinite m x ^ (p - 1) = 1 := by
  obtain ⟨k, hk⟩ := bezoutV_spec (p := p) m
  have hexp : (p - 1) * ((p - 1) * bezoutV (p := p) m) = (p - 1) + (p ^ m * (p - 1)) * k := by
    calc (p - 1) * ((p - 1) * bezoutV (p := p) m)
        = (p - 1) * (1 + p ^ m * k) := by rw [hk]
      _ = (p - 1) + (p ^ m * (p - 1)) * k := by ring
  have hx1 : x ^ ((p - 1) * ((p - 1) * bezoutV (p := p) m)) = x ^ (p - 1) := by
    calc x ^ ((p - 1) * ((p - 1) * bezoutV (p := p) m))
        = x ^ ((p - 1) + (p ^ m * (p - 1)) * k) := congrArg (fun t => x ^ t) hexp
      _ = x ^ (p - 1) * x ^ ((p ^ m * (p - 1)) * k) := pow_add x _ _
      _ = x ^ (p - 1) * (x ^ (p ^ m * (p - 1))) ^ k := by rw [pow_mul]
      _ = x ^ (p - 1) * 1 ^ k := by rw [pow_card_units_prime_pow]
      _ = x ^ (p - 1) := by rw [one_pow, mul_one]
  have hσ : principalPartFinite m x ^ (p - 1) = x ^ (p - 1) := by
    calc principalPartFinite m x ^ (p - 1)
        = (x ^ ((p - 1) * bezoutV (p := p) m)) ^ (p - 1) := rfl
      _ = x ^ (((p - 1) * bezoutV (p := p) m) * (p - 1)) := (pow_mul x _ _).symm
      _ = x ^ ((p - 1) * ((p - 1) * bezoutV (p := p) m)) := congrArg (fun t => x ^ t) (by ring)
      _ = x ^ (p - 1) := hx1
  calc tamePartFinite m x ^ (p - 1)
      = x ^ (p - 1) * ((principalPartFinite m x) ^ (p - 1))⁻¹ := by
        rw [tamePartFinite, mul_pow, inv_pow]
    _ = x ^ (p - 1) * (x ^ (p - 1))⁻¹ := by rw [hσ]
    _ = 1 := mul_inv_cancel _

theorem tamePartFinite_principal (m : ℕ) (x : (ZMod (p ^ (m + 1)))ˣ) :
    reduction m ((tamePartFinite m x)⁻¹ * x) = 1 := by
  have h : (tamePartFinite m x)⁻¹ * x = principalPartFinite m x := by
    rw [tamePartFinite]; group
  rw [h]
  exact principalPartFinite_mem m x

/-- **Finite-level `Δ`/`Γ` splitting.**  Every unit modulo `p^(m+1)` is uniquely the product
of a `(p-1)`-torsion unit and a principal unit: the finite-level form of thesis (3.9). -/
theorem existsUnique_tame_principal (m : ℕ) (x : (ZMod (p ^ (m + 1)))ˣ) :
    ∃! μ : (ZMod (p ^ (m + 1)))ˣ, μ ^ (p - 1) = 1 ∧ reduction m (μ⁻¹ * x) = 1 := by
  refine ⟨tamePartFinite m x, ⟨tamePartFinite_pow_sub_one m x, tamePartFinite_principal m x⟩, ?_⟩
  intro ν hν
  obtain ⟨hνp, hνred⟩ := hν
  set μ := tamePartFinite m x with hμ
  have hred : reduction m (ν * μ⁻¹) = 1 := by
    have h1 : reduction m ν = reduction m x := by
      have h := hνred
      rw [map_mul, map_inv] at h
      calc reduction m ν = (reduction m ν) * ((reduction m ν)⁻¹ * reduction m x) := by rw [h, mul_one]
        _ = reduction m x := by group
    have h2 : reduction m μ = reduction m x := by
      have h := tamePartFinite_principal m x
      rw [map_mul, map_inv] at h
      calc reduction m μ = (reduction m μ) * ((reduction m μ)⁻¹ * reduction m x) := by rw [h, mul_one]
        _ = reduction m x := by group
    rw [map_mul, map_inv, h1, h2, mul_inv_cancel]
  have hpow : (ν * μ⁻¹) ^ (p ^ m) = 1 := pow_pow_eq_one_of_isPrincipal _ hred
  have hpow' : (ν * μ⁻¹) ^ (p - 1) = 1 := by
    rw [mul_pow, inv_pow, hνp, tamePartFinite_pow_sub_one, one_mul, inv_one]
  have hone : ν * μ⁻¹ = 1 := eq_one_of_pow_primePow_of_pow_sub_one hpow hpow'
  rwa [mul_inv_eq_one] at hone


/-! ## §4 The two-kind decomposition of Dirichlet characters -/

/-- A group element killed by coprime exponents is trivial. -/
theorem eq_one_of_pow_coprime {G : Type*} [CommGroup G] {g : G} {e f : ℕ}
    (hcop : Nat.Coprime e f) (he : g ^ e = 1) (hf : g ^ f = 1) : g = 1 := by
  have h1 : orderOf g ∣ e := orderOf_dvd_iff_pow_eq_one.mpr he
  have h2 : orderOf g ∣ f := orderOf_dvd_iff_pow_eq_one.mpr hf
  have h4 : orderOf g ∣ Nat.gcd e f := Nat.dvd_gcd h1 h2
  rw [hcop.gcd_eq_one] at h4
  exact orderOf_eq_one_iff.mp (Nat.dvd_one.mp h4)

/-- Every level-`p` character has `(p-1)`-st power `1`. -/
theorem level_character_pow_sub_one (θ : DirichletCharacter ℂ_[p] p) : θ ^ (p - 1) = 1 := by
  refine MulChar.ext fun a => ?_
  rw [MulChar.pow_apply_coe, MulChar.one_apply_coe, ← map_pow,
    ← Units.val_pow_eq_pow_val, reduction_pow_sub_one, Units.val_one, map_one]

/-- `ofUnitHom` and `toUnitHom` are inverse. -/
theorem toUnitHom_ofUnitHom {M : Type*} [CommMonoid M] (f : Mˣ →* ℂ_[p]ˣ) :
    (MulChar.ofUnitHom f).toUnitHom = f :=
  (MulChar.equivToUnitHom).apply_symm_apply f

/-- The principal-unit (second kind) part `ψ` of a character `χ` of level `p^(m+1)`. -/
noncomputable def secondKindPart (m : ℕ) (χ : DirichletCharacter ℂ_[p] (p ^ (m + 1))) :
    DirichletCharacter ℂ_[p] (p ^ (m + 1)) :=
  MulChar.ofUnitHom (χ.toUnitHom.comp (powMonoidHom ((p - 1) * bezoutV (p := p) m)))

/-- The first-kind part `θ` of a character `χ` of level `p^(m+1)`. -/
noncomputable def firstKindPart (m : ℕ) (χ : DirichletCharacter ℂ_[p] (p ^ (m + 1))) :
    DirichletCharacter ℂ_[p] (p ^ (m + 1)) :=
  MulChar.ofUnitHom (χ.toUnitHom *
    (χ.toUnitHom.comp (powMonoidHom ((p - 1) * bezoutV (p := p) m)))⁻¹)

theorem secondKindPart_toUnitHom (m : ℕ) (χ : DirichletCharacter ℂ_[p] (p ^ (m + 1))) :
    (secondKindPart m χ).toUnitHom
      = χ.toUnitHom.comp (powMonoidHom ((p - 1) * bezoutV (p := p) m)) :=
  toUnitHom_ofUnitHom _

theorem firstKindPart_toUnitHom (m : ℕ) (χ : DirichletCharacter ℂ_[p] (p ^ (m + 1))) :
    (firstKindPart m χ).toUnitHom = χ.toUnitHom *
      (χ.toUnitHom.comp (powMonoidHom ((p - 1) * bezoutV (p := p) m)))⁻¹ :=
  toUnitHom_ofUnitHom _

theorem secondKindPart_apply_unit (m : ℕ) (χ : DirichletCharacter ℂ_[p] (p ^ (m + 1)))
    (a : (ZMod (p ^ (m + 1)))ˣ) :
    (secondKindPart m χ).toUnitHom a = χ.toUnitHom (a ^ ((p - 1) * bezoutV (p := p) m)) := by
  rw [secondKindPart_toUnitHom, MonoidHom.comp_apply, powMonoidHom_apply]

theorem firstKindPart_apply_unit (m : ℕ) (χ : DirichletCharacter ℂ_[p] (p ^ (m + 1)))
    (a : (ZMod (p ^ (m + 1)))ˣ) :
    (firstKindPart m χ).toUnitHom a
      = χ.toUnitHom a * ((secondKindPart m χ).toUnitHom a)⁻¹ := by
  rw [firstKindPart_toUnitHom, MonoidHom.mul_apply, MonoidHom.inv_apply, MonoidHom.comp_apply,
    powMonoidHom_apply, secondKindPart_toUnitHom, MonoidHom.comp_apply, powMonoidHom_apply]

/-- **The second-kind part has `p`-power order.** -/
theorem secondKindPart_pow_eq_one (m : ℕ) (χ : DirichletCharacter ℂ_[p] (p ^ (m + 1))) :
    secondKindPart m χ ^ (p ^ m) = 1 := by
  refine MulChar.ext fun a => ?_
  rw [MulChar.pow_apply_coe, MulChar.one_apply_coe,
    ← MulChar.coe_toUnitHom (secondKindPart m χ) a, secondKindPart_apply_unit,
    ← Units.val_pow_eq_pow_val, ← map_pow,
    show a ^ ((p - 1) * bezoutV (p := p) m) = principalPartFinite m a from rfl,
    pow_pow_eq_one_of_isPrincipal _ (principalPartFinite_mem m a), map_one, Units.val_one]

/-- **The two-kind product is the character.** -/
theorem firstKindPart_mul_secondKindPart (m : ℕ) (χ : DirichletCharacter ℂ_[p] (p ^ (m + 1))) :
    firstKindPart m χ * secondKindPart m χ = χ := by
  refine MulChar.ext fun a => ?_
  rw [MulChar.mul_apply, ← MulChar.coe_toUnitHom (firstKindPart m χ) a,
    ← MulChar.coe_toUnitHom (secondKindPart m χ) a, ← MulChar.coe_toUnitHom χ a,
    ← Units.val_mul, firstKindPart_apply_unit]
  congr 1
  group

/-- **The first-kind part kills the principal units.** -/
theorem firstKindPart_kills_principal (m : ℕ) (χ : DirichletCharacter ℂ_[p] (p ^ (m + 1)))
    {a : (ZMod (p ^ (m + 1)))ˣ} (ha : reduction m a = 1) :
    firstKindPart m χ (a : ZMod (p ^ (m + 1))) = 1 := by
  have hpow : a ^ ((p - 1) * bezoutV (p := p) m) = a := by
    obtain ⟨k, hk⟩ := bezoutV_spec (p := p) m
    have he : a ^ (p ^ m) = 1 := pow_pow_eq_one_of_isPrincipal a ha
    calc a ^ ((p - 1) * bezoutV (p := p) m)
        = a ^ (1 + p ^ m * k) := congrArg (fun t => a ^ t) hk
      _ = a ^ 1 * a ^ (p ^ m * k) := pow_add a 1 _
      _ = a * (a ^ (p ^ m)) ^ k := by rw [pow_one, pow_mul]
      _ = a := by rw [he, one_pow, mul_one]
  have hunit : (firstKindPart m χ).toUnitHom a = 1 := by
    rw [firstKindPart_apply_unit, secondKindPart_apply_unit, hpow, mul_inv_cancel]
  rw [← MulChar.coe_toUnitHom (firstKindPart m χ) a, hunit, Units.val_one]

/-- **The first-kind part is a level-`p` character.**  A character of level `p^(m+1)` that
kills the principal units factors through level `p`. -/
theorem exists_firstKind_level (m : ℕ) (χ : DirichletCharacter ℂ_[p] (p ^ (m + 1))) :
    ∃ θ : DirichletCharacter ℂ_[p] p,
      changeLevel (dvd_pow_self p (Nat.succ_ne_zero m)) θ = firstKindPart m χ := by
  classical
  haveI : NeZero (p ^ (m + 1)) := ⟨pow_ne_zero _ hp.out.ne_zero⟩
  set θu : (ZMod (p ^ (m + 1)))ˣ →* ℂ_[p]ˣ := (firstKindPart m χ).toUnitHom with hθu
  have hker : ∀ a : (ZMod (p ^ (m + 1)))ˣ, reduction m a = 1 → θu a = 1 := by
    intro a ha
    have h := firstKindPart_kills_principal m χ ha
    rw [hθu]
    refine Units.ext ?_
    rw [MulChar.coe_toUnitHom, h, Units.val_one]
  have hcong : ∀ a b : (ZMod (p ^ (m + 1)))ˣ, reduction m a = reduction m b → θu a = θu b := by
    intro a b hab
    have h1 : reduction m (a * b⁻¹) = 1 := by rw [map_mul, map_inv, hab, mul_inv_cancel]
    have h2 : θu (a * b⁻¹) = 1 := hker _ h1
    rw [map_mul, map_inv] at h2
    calc θu a = θu a * ((θu b)⁻¹ * θu b) := by group
      _ = θu b := by rw [← mul_assoc, h2, one_mul]
  have hsurj : Function.Surjective (ZMod.unitsMap (dvd_pow_self p (Nat.succ_ne_zero m))) :=
    ZMod.unitsMap_surjective _
  let θ₀ : (ZMod p)ˣ →* ℂ_[p]ˣ :=
    { toFun := fun u => θu ((hsurj u).choose)
      map_one' := hker _ ((hsurj 1).choose_spec)
      map_mul' := fun u v => by
        show θu ((hsurj (u * v)).choose) = θu ((hsurj u).choose) * θu ((hsurj v).choose)
        rw [← map_mul]
        refine hcong _ _ ?_
        show ZMod.unitsMap (dvd_pow_self p (Nat.succ_ne_zero m)) ((hsurj (u * v)).choose)
          = ZMod.unitsMap (dvd_pow_self p (Nat.succ_ne_zero m))
              ((hsurj u).choose * (hsurj v).choose)
        rw [map_mul, (hsurj (u * v)).choose_spec, (hsurj u).choose_spec,
          (hsurj v).choose_spec] }
  have hθ₀app : ∀ u : (ZMod p)ˣ, θ₀ u = θu ((hsurj u).choose) := fun u => rfl
  refine ⟨MulChar.ofUnitHom θ₀, ?_⟩
  refine (DirichletCharacter.toUnitHom_inj _ _).mp ?_
  rw [DirichletCharacter.changeLevel_toUnitHom, toUnitHom_ofUnitHom]
  refine MonoidHom.ext fun a => ?_
  rw [MonoidHom.comp_apply]
  show θu ((hsurj (reduction m a)).choose) = θu a
  exact hcong _ _ ((hsurj (reduction m a)).choose_spec)

/-- The first-kind part has order dividing `p-1`. -/
theorem firstKindPart_pow_eq_one (m : ℕ) (χ : DirichletCharacter ℂ_[p] (p ^ (m + 1))) :
    firstKindPart m χ ^ (p - 1) = 1 := by
  obtain ⟨θ, hθ⟩ := exists_firstKind_level m χ
  rw [← hθ, ← map_pow, level_character_pow_sub_one, map_one]

/-- **Existence of the two-kind decomposition** (thesis Definition 3.2).  Every Dirichlet
character of level `p^(m+1)` is a first-kind character of level `p` times a second-kind
character of `p`-power order. -/
theorem exists_combineFirstSecondKind (m : ℕ) (χ : DirichletCharacter ℂ_[p] (p ^ (m + 1))) :
    ∃ θ : DirichletCharacter ℂ_[p] p, ∃ ψ : DirichletCharacter ℂ_[p] (p ^ (m + 1)),
      IsSecondKind p m ψ ∧ χ = combineFirstSecondKind m θ ψ := by
  obtain ⟨θ, hθ⟩ := exists_firstKind_level m χ
  refine ⟨θ, secondKindPart m χ, secondKindPart_pow_eq_one m χ, ?_⟩
  refine (firstKindPart_mul_secondKindPart m χ).symm.trans ?_
  rw [combineFirstSecondKind, liftFirstKind, ← hθ]

/-- **Uniqueness of the two-kind decomposition.** -/
theorem combineFirstSecondKind_unique {m : ℕ} {θ₁ θ₂ : DirichletCharacter ℂ_[p] p}
    {ψ₁ ψ₂ : DirichletCharacter ℂ_[p] (p ^ (m + 1))}
    (h₁ : IsSecondKind p m ψ₁) (h₂ : IsSecondKind p m ψ₂)
    (h : combineFirstSecondKind m θ₁ ψ₁ = combineFirstSecondKind m θ₂ ψ₂) :
    θ₁ = θ₂ ∧ ψ₁ = ψ₂ := by
  haveI : NeZero (p ^ (m + 1)) := ⟨pow_ne_zero _ hp.out.ne_zero⟩
  have hρe : (ψ₂ * ψ₁⁻¹) ^ (p ^ m) = 1 := by
    rw [mul_pow, inv_pow, h₂, h₁]
    simp
  have hρf : (ψ₂ * ψ₁⁻¹) ^ (p - 1) = 1 := by
    have heq : ψ₂ * ψ₁⁻¹
        = changeLevel (dvd_pow_self p (Nat.succ_ne_zero m)) (θ₁ * θ₂⁻¹) := by
      have hh := h
      rw [combineFirstSecondKind, combineFirstSecondKind, liftFirstKind, liftFirstKind] at hh
      rw [map_mul, map_inv]
      have step : ψ₂ = (changeLevel (dvd_pow_self p (Nat.succ_ne_zero m)) θ₂)⁻¹ *
          (changeLevel (dvd_pow_self p (Nat.succ_ne_zero m)) θ₁ * ψ₁) := by
        rw [hh, inv_mul_cancel_left]
      rw [step, mul_assoc, mul_assoc, mul_inv_cancel, mul_one, mul_comm]
    rw [heq, ← map_pow, level_character_pow_sub_one, map_one]
  have hone : ψ₂ * ψ₁⁻¹ = 1 := eq_one_of_pow_coprime (coprime_pow_sub_one m) hρe hρf
  have hψ : ψ₁ = ψ₂ := by
    rw [mul_inv_eq_one] at hone
    exact hone.symm
  have hθ : θ₁ = θ₂ := by
    have hh := h
    rw [combineFirstSecondKind, combineFirstSecondKind, liftFirstKind, liftFirstKind, hψ] at hh
    exact DirichletCharacter.changeLevel_injective _ (mul_right_cancel hh)
  exact ⟨hθ, hψ⟩

/-- **The two-kind product map is injective.** -/
theorem combineFirstSecondKind_inj {m : ℕ} {θ₁ θ₂ : DirichletCharacter ℂ_[p] p}
    {ψ₁ ψ₂ : DirichletCharacter ℂ_[p] (p ^ (m + 1))}
    (h₁ : IsSecondKind p m ψ₁) (h₂ : IsSecondKind p m ψ₂)
    (h : combineFirstSecondKind m θ₁ ψ₁ = combineFirstSecondKind m θ₂ ψ₂) :
    θ₁ = θ₂ ∧ ψ₁ = ψ₂ :=
  combineFirstSecondKind_unique h₁ h₂ h

/-- **The two-kind decomposition is bijective** (non-vacuity and surjectivity of
`combineFirstSecondKind`). -/
theorem existsUnique_combineFirstSecondKind (m : ℕ) (χ : DirichletCharacter ℂ_[p] (p ^ (m + 1))) :
    ∃! θψ : DirichletCharacter ℂ_[p] p × DirichletCharacter ℂ_[p] (p ^ (m + 1)),
      IsSecondKind p m θψ.2 ∧ χ = combineFirstSecondKind m θψ.1 θψ.2 := by
  obtain ⟨θ, ψ, hψ, hχ⟩ := exists_combineFirstSecondKind m χ
  refine ⟨⟨θ, ψ⟩, ⟨hψ, hχ⟩, ?_⟩
  rintro ⟨θ', ψ'⟩ ⟨hψ', hχ'⟩
  obtain ⟨h1, h2⟩ := combineFirstSecondKind_unique hψ' hψ (hχ'.symm.trans hχ)
  exact Prod.ext h1 h2

/-- **The `Δ`-part is a power of the Teichmüller character** (thesis `Δ̂ = ⟨ω⟩`): the
first-kind part can be taken to be `ω^k`. -/
theorem exists_zpow_teichmuller_combine (m : ℕ) (χ : DirichletCharacter ℂ_[p] (p ^ (m + 1))) :
    ∃ k : ℤ, ∃ ψ : DirichletCharacter ℂ_[p] (p ^ (m + 1)),
      IsSecondKind p m ψ ∧
        χ = combineFirstSecondKind m (Teich.teichmullerCharacter (p := p) ^ k) ψ := by
  obtain ⟨θ, ψ, hψ, hχ⟩ := exists_combineFirstSecondKind m χ
  obtain ⟨k, hk⟩ := Teich.exists_zpow_eq_teichmullerCharacter (p := p) θ
  exact ⟨k, ψ, hψ, by rw [hχ, ← hk]⟩


/-- A second-kind character of level `p^(m+1)` is trivial on the `(p-1)`-torsion. -/
theorem secondKindPart_eq_one_of_pow_sub_one (m : ℕ)
    (χ : DirichletCharacter ℂ_[p] (p ^ (m + 1)))
    {a : (ZMod (p ^ (m + 1)))ˣ} (ha : a ^ (p - 1) = 1) :
    secondKindPart m χ (a : ZMod (p ^ (m + 1))) = 1 := by
  rw [← MulChar.coe_toUnitHom (secondKindPart m χ) a, secondKindPart_apply_unit,
    show a ^ ((p - 1) * bezoutV (p := p) m) = 1 by rw [pow_mul, ha, one_pow],
    map_one, Units.val_one]

/-- A field element killed by coprime exponents is `1`. -/
theorem field_eq_one_of_pow_coprime {K : Type*} [Field K] {x : K} {e f : ℕ}
    (he : x ^ e = 1) (hf : x ^ f = 1) (hcop : Nat.Coprime e f) : x = 1 := by
  rcases Nat.eq_zero_or_pos e with he0 | hepos
  · have hf1 : f = 1 := by
      have h : Nat.gcd e f = 1 := hcop
      rw [he0] at h
      simpa using h
    rw [hf1, pow_one] at hf
    exact hf
  · have hx : x ≠ 0 := by
      rintro rfl
      rw [zero_pow (by omega : e ≠ 0)] at he
      exact zero_ne_one he
    let u : Kˣ := Units.mk0 x hx
    have hu : ((u : Kˣ) : K) = x := Units.val_mk0 hx
    have he' : u ^ e = 1 := by
      refine Units.ext ?_
      show ((u ^ e : Kˣ) : K) = ((1 : Kˣ) : K)
      rw [Units.val_pow_eq_pow_val, Units.val_one, hu]
      exact he
    have hf' : u ^ f = 1 := by
      refine Units.ext ?_
      show ((u ^ f : Kˣ) : K) = ((1 : Kˣ) : K)
      rw [Units.val_pow_eq_pow_val, Units.val_one, hu]
      exact hf
    have hone : u = 1 := eq_one_of_pow_coprime hcop he' hf'
    calc x = ((u : Kˣ) : K) := hu.symm
      _ = ((1 : Kˣ) : K) := by rw [hone]
      _ = 1 := Units.val_one

/-- **Characterisation of `IsSecondKind`.**  A character of level `p^(m+1)` has `p`-power
order exactly when it is trivial on the `(p-1)`-torsion (`= μ_(p-1)`) of the unit group. -/
theorem isSecondKind_iff_kills_torsion (m : ℕ)
    (ψ : DirichletCharacter ℂ_[p] (p ^ (m + 1))) :
    IsSecondKind p m ψ ↔
      ∀ a : (ZMod (p ^ (m + 1)))ˣ, a ^ (p - 1) = 1 → ψ (a : ZMod (p ^ (m + 1))) = 1 := by
  constructor
  · intro hψ a ha
    have he : ψ (a : ZMod (p ^ (m + 1))) ^ (p ^ m) = 1 := by
      have h := congrArg
        (fun η : DirichletCharacter ℂ_[p] (p ^ (m + 1)) => η (a : ZMod (p ^ (m + 1)))) hψ
      simpa [MulChar.pow_apply_coe] using h
    have hf : ψ (a : ZMod (p ^ (m + 1))) ^ (p - 1) = 1 := by
      rw [← map_pow, ← Units.val_pow_eq_pow_val, ha, Units.val_one, map_one]
    exact field_eq_one_of_pow_coprime he hf (coprime_pow_sub_one m)
  · intro hT
    refine MulChar.ext fun a => ?_
    rw [MulChar.pow_apply_coe, MulChar.one_apply_coe]
    have hτ : ψ ((tamePartFinite m a : (ZMod (p ^ (m + 1)))ˣ) :
        ZMod (p ^ (m + 1))) = 1 := hT _ (tamePartFinite_pow_sub_one m a)
    have hsplit_units : a = tamePartFinite m a * principalPartFinite m a := by
      rw [tamePartFinite, mul_assoc, inv_mul_cancel, mul_one]
    rw [hsplit_units, Units.val_mul, map_mul, hτ, one_mul, ← map_pow,
      ← Units.val_pow_eq_pow_val,
      pow_pow_eq_one_of_isPrincipal _ (principalPartFinite_mem m a), Units.val_one, map_one]


/-! ## §5 The decomposition as a splitting

The two maps `firstKindPart` and `secondKindPart` are the projections onto the two factors of
the decomposition; on the image of `combineFirstSecondKind` they act as the identity, so that
`combineFirstSecondKind` is a section of the product map. -/

/-- The second-kind part of a lifted level-`p` character is trivial. -/
theorem secondKindPart_liftFirstKind (m : ℕ) (θ : DirichletCharacter ℂ_[p] p) :
    secondKindPart m (liftFirstKind m θ) = 1 := by
  have hunit : ∀ a : (ZMod (p ^ (m + 1)))ˣ,
      (secondKindPart m (liftFirstKind m θ)).toUnitHom a = 1 := by
    intro a
    rw [secondKindPart_apply_unit, liftFirstKind,
      DirichletCharacter.changeLevel_toUnitHom, MonoidHom.comp_apply,
      show ZMod.unitsMap (dvd_pow_self p (Nat.add_one_ne_zero m))
          (a ^ ((p - 1) * bezoutV (p := p) m)) = 1 by
        rw [map_pow, pow_mul, reduction_pow_sub_one, one_pow],
      map_one]
  refine MulChar.ext fun a => ?_
  rw [MulChar.one_apply_coe, ← MulChar.coe_toUnitHom (secondKindPart m (liftFirstKind m θ)) a,
    hunit a, Units.val_one]

/-- The first-kind part of a lifted level-`p` character is that character. -/
theorem firstKindPart_liftFirstKind (m : ℕ) (θ : DirichletCharacter ℂ_[p] p) :
    firstKindPart m (liftFirstKind m θ) = liftFirstKind m θ := by
  have h : firstKindPart m (liftFirstKind m θ) * secondKindPart m (liftFirstKind m θ)
      = liftFirstKind m θ := firstKindPart_mul_secondKindPart m _
  rw [secondKindPart_liftFirstKind, mul_one] at h
  exact h

/-- A second-kind character has trivial first-kind part. -/
theorem firstKindPart_eq_one_of_isSecondKind (m : ℕ)
    {ψ : DirichletCharacter ℂ_[p] (p ^ (m + 1))} (hψ : IsSecondKind p m ψ) :
    firstKindPart m ψ = 1 := by
  have hsplit := firstKindPart_mul_secondKindPart m ψ
  have hmem : firstKindPart m ψ = ψ * (secondKindPart m ψ)⁻¹ := by
    calc firstKindPart m ψ
        = (firstKindPart m ψ * secondKindPart m ψ) * (secondKindPart m ψ)⁻¹ :=
          (mul_inv_cancel_right _ _).symm
      _ = ψ * (secondKindPart m ψ)⁻¹ := by rw [hsplit]
  have hsecond : (ψ * (secondKindPart m ψ)⁻¹) ^ (p ^ m) = 1 := by
    rw [mul_pow, inv_pow, hψ, secondKindPart_pow_eq_one]
    simp
  have hfirst : (ψ * (secondKindPart m ψ)⁻¹) ^ (p - 1) = 1 := by
    rw [← hmem]
    exact firstKindPart_pow_eq_one m ψ
  rw [hmem]
  exact eq_one_of_pow_coprime (coprime_pow_sub_one m) hsecond hfirst

/-- The second-kind part of a second-kind character is itself. -/
theorem secondKindPart_eq_self_of_isSecondKind (m : ℕ)
    {ψ : DirichletCharacter ℂ_[p] (p ^ (m + 1))} (hψ : IsSecondKind p m ψ) :
    secondKindPart m ψ = ψ := by
  have hsplit := firstKindPart_mul_secondKindPart m ψ
  rw [firstKindPart_eq_one_of_isSecondKind m hψ, one_mul] at hsplit
  exact hsplit


/-! ## §6 Comparison with the `p`-adic Teichmüller lift of `Thesis.Teich`

The finite-level `Δ`-part `tamePartFinite` is constructed as a power of the unit; here it is
identified with the reduction modulo `p^(m+1)` of the `p`-adic Teichmüller lift
`Teich.teichLift`, i.e. with the `Δ`-part used in `Teich.padicUnitsMulEquiv`.  This is the
finite/infinite compatibility of (3.9). -/

theorem toZModPow_cast_eq_toZMod (m : ℕ) (x : ℤ_[p]) :
    ((PadicInt.toZModPow (m+1) x : ZMod (p ^ (m+1))).cast : ZMod p) = PadicInt.toZMod x := by
  haveI : NeZero (p ^ (m+1)) := ⟨pow_ne_zero _ hp.out.ne_zero⟩
  rw [ZMod.cast_eq_val]
  have hrepr : (PadicInt.toZModPow (m+1) x)
      = ((x.appr (m+1) : ℕ) : ZMod (p ^ (m+1))) := rfl
  have hval : (PadicInt.toZModPow (m+1) x).val = x.appr (m+1) := by
    rw [hrepr, ZMod.val_natCast, Nat.mod_eq_of_lt (PadicInt.appr_lt x (m+1))]
  rw [hval, ← ZMod.natCast_zmod_val (PadicInt.toZMod x), PadicInt.val_toZMod_eq_zmodRepr]
  refine PadicInt.zmod_congr_of_sub_mem_max_ideal x (x.appr (m+1)) (x.zmodRepr) ?_ ?_
  · rw [PadicInt.maximalIdeal_eq_span_p, Ideal.mem_span_singleton]
    have h := PadicInt.appr_spec (m+1) x
    rw [Ideal.mem_span_singleton] at h
    exact dvd_trans (dvd_pow_self (p : ℤ_[p]) (by omega)) h
  · exact PadicInt.sub_zmodRepr_mem (p := p) x

/-- The reduction modulo `p^(m+1)` of the `p`-adic Teichmüller lift: the finite-level
`Δ`-part coming from `Thesis.Teich`. -/
noncomputable def teichUnitFinite (m : ℕ) (u : (ZMod p)ˣ) : (ZMod (p ^ (m+1)))ˣ :=
  Units.map (PadicInt.toZModPow (m+1)).toMonoidHom (Teich.teichLiftHom u)

theorem teichUnitFinite_val (m : ℕ) (u : (ZMod p)ˣ) :
    ((teichUnitFinite m u : (ZMod (p ^ (m+1)))ˣ) : ZMod (p ^ (m+1)))
      = PadicInt.toZModPow (m+1) (Teich.teichLift u) := by
  rw [teichUnitFinite, Units.coe_map, Teich.teichLiftHom_apply]
  rfl

theorem teichUnitFinite_pow_sub_one (m : ℕ) (u : (ZMod p)ˣ) :
    teichUnitFinite m u ^ (p - 1) = 1 := by
  rw [teichUnitFinite, ← map_pow, Teich.teichLiftHom_pow_card, map_one]

theorem reduction_teichUnitFinite (m : ℕ) (u : (ZMod p)ˣ) :
    reduction m (teichUnitFinite m u) = u := by
  refine Units.ext ?_
  change ((ZMod.unitsMap (dvd_pow_self p (Nat.succ_ne_zero m)) (teichUnitFinite m u)) : ZMod p)
    = ((u : (ZMod p)ˣ) : ZMod p)
  rw [ZMod.unitsMap_val, teichUnitFinite_val, toZModPow_cast_eq_toZMod, Teich.teichLift_toZMod]

/-- **The finite-level `Δ`-part is the reduction of the `p`-adic Teichmüller part** (3.9):
the tame part of a unit modulo `p^(m+1)` is the reduction of `Teich.teichLift` of its
residue modulo `p`. -/
theorem teichUnitFinite_eq_tamePartFinite (m : ℕ) (x : (ZMod (p ^ (m+1)))ˣ) :
    teichUnitFinite m (reduction m x) = tamePartFinite m x := by
  refine (existsUnique_tame_principal m x).unique ?_
    ⟨tamePartFinite_pow_sub_one m x, tamePartFinite_principal m x⟩
  exact ⟨teichUnitFinite_pow_sub_one m _,
    by rw [map_mul, map_inv, reduction_teichUnitFinite, inv_mul_cancel]⟩


/-! ## §7 The node-closing statement -/

/-- **Thesis Definition 3.2 / equation (3.9) at finite level**, assembled.

For an odd prime `p` and every `m`, the following hold at cyclotomic level `p^(m+1)`:

1. every unit is uniquely a `(p-1)`-torsion unit times a principal unit (`Δ`/`Γ` splitting);
2. every Dirichlet character decomposes uniquely as `combineFirstSecondKind m θ ψ` with
   `IsSecondKind p m ψ`, and the product map is injective;
3. `IsSecondKind p m ψ` holds exactly for the characters trivial on the `(p-1)`-torsion;
4. the first-kind part can always be taken to be a power of the Teichmüller character;
5. the two projections are a retraction of `combineFirstSecondKind`. -/
theorem finite_delta_gamma (m : ℕ) :
    (∀ x : (ZMod (p ^ (m + 1)))ˣ,
        ∃! μ : (ZMod (p ^ (m + 1)))ˣ, μ ^ (p - 1) = 1 ∧ reduction m (μ⁻¹ * x) = 1) ∧
    (∀ χ : DirichletCharacter ℂ_[p] (p ^ (m + 1)),
        ∃! θψ : DirichletCharacter ℂ_[p] p × DirichletCharacter ℂ_[p] (p ^ (m + 1)),
          IsSecondKind p m θψ.2 ∧ χ = combineFirstSecondKind m θψ.1 θψ.2) ∧
    (∀ ψ : DirichletCharacter ℂ_[p] (p ^ (m + 1)),
        (IsSecondKind p m ψ ↔
          ∀ a : (ZMod (p ^ (m + 1)))ˣ, a ^ (p - 1) = 1 →
            ψ (a : ZMod (p ^ (m + 1))) = 1)) ∧
    (∀ χ : DirichletCharacter ℂ_[p] (p ^ (m + 1)),
        ∃ k : ℤ, ∃ ψ : DirichletCharacter ℂ_[p] (p ^ (m + 1)),
          IsSecondKind p m ψ ∧
            χ = combineFirstSecondKind m (Teich.teichmullerCharacter (p := p) ^ k) ψ) ∧
    (∀ θ : DirichletCharacter ℂ_[p] p,
        firstKindPart m (liftFirstKind m θ) = liftFirstKind m θ ∧
          secondKindPart m (liftFirstKind m θ) = 1) ∧
    (∀ x : (ZMod (p ^ (m + 1)))ˣ,
        teichUnitFinite m (reduction m x) = tamePartFinite m x) :=
  ⟨existsUnique_tame_principal m, existsUnique_combineFirstSecondKind m,
    isSecondKind_iff_kills_torsion m, exists_zpow_teichmuller_combine m,
    fun θ => ⟨firstKindPart_liftFirstKind m θ, secondKindPart_liftFirstKind m θ⟩,
    teichUnitFinite_eq_tamePartFinite m⟩


end FiniteDG
end Thesis
