/-
Swarm smwl46mfx, member m4, direction d11.

# The exponential generating function of the generalized Bernoulli numbers
(thesis, Definition 2.2)

`Thesis.generalizedBernoulli` is defined by the Bernoulli-polynomial formula
`B_{n,χ} = N^{n-1} ∑_{a=1}^N χ(a) B_n(a/N)` (equation (2.9)).  Here we prove that it is
the coefficient sequence of the generating function of Definition 2.2:

`(∑_n B_{n,χ} T^n/n!) · (e^{NT} - 1) = ∑_{a=1}^{N} χ(a) · T e^{aT}`

in `ℚ⟦T⟧`, i.e. `∑_n B_{n,χ}T^n/n! = ∑_a χ(a) T e^{aT}/(e^{NT}-1)`, together with its
coefficientwise consequences: the recursion
`∑_{i<n} C(n,i) B_{i,χ} N^{n-i} = n ∑_{a=1}^N χ(a) a^{n-1}` and the parity vanishing.
-/
import Thesis.GeneralizedBernoulli

open PowerSeries
open scoped BigOperators

namespace Thesis

variable {N : ℕ}

/-- The uniform cancellation of the `zpow` in the definition of `generalizedBernoulli`. -/
theorem zpow_pred_mul_self (hN : 0 < N) (n : ℕ) :
    (N : ℚ) ^ ((n : ℤ) - 1) * (N : ℚ) = (N : ℚ) ^ n := by
  cases n with
  | zero =>
      have hNq : ((N : ℚ)) ≠ 0 := by exact_mod_cast hN.ne'
      simp only [Nat.cast_zero, zero_sub, zpow_neg_one, pow_zero]
      exact inv_mul_cancel₀ hNq
  | succ k =>
      have h : (((k + 1 : ℕ) : ℤ) - 1) = (k : ℤ) := by push_cast; ring
      rw [h, zpow_natCast, pow_succ]

/-- Mathlib's Bernoulli-polynomial generating function, in the shape used below. -/
theorem bernoulliPoly_egf (x : ℚ) :
    (mk fun n => ((Polynomial.bernoulli n).eval x) / (n.factorial : ℚ)) * (exp ℚ - 1)
      = X * rescale x (exp ℚ) := by
  have h := Polynomial.bernoulli_generating_function (A := ℚ) x
  simpa [div_eq_mul_inv, mul_comm, smul_eq_mul] using h

/-- **The `N`-rescaled identity at the point `a/N`**, with the `zpow` of the definition:
`(∑_n N^{n-1}B_n(a/N)T^n/n!)(e^{NT}-1) = T e^{aT}`. -/
theorem bernoulliPoly_egf_rescale (hN : 0 < N) (a : ℕ) :
    (mk fun n => ((N : ℚ) ^ ((n : ℤ) - 1) * ((Polynomial.bernoulli n).eval ((a : ℚ) / N)) /
      (n.factorial : ℚ))) * (rescale (N : ℚ) (exp ℚ) - 1)
      = X * rescale (a : ℚ) (exp ℚ) := by
  have hNq : ((N : ℚ)) ≠ 0 := by exact_mod_cast hN.ne'
  have h := bernoulliPoly_egf ((a : ℚ) / N)
  have h' := congrArg (rescale (N : ℚ)) h
  rw [map_mul, map_sub, map_one] at h'
  have hX : (rescale (N : ℚ)) (X * rescale ((a : ℚ) / N) (exp ℚ))
      = C (N : ℚ) * (X * rescale (a : ℚ) (exp ℚ)) := by
    rw [map_mul, PowerSeries.rescale_X, PowerSeries.rescale_rescale,
      mul_comm ((a : ℚ) / N) (N : ℚ), mul_div_cancel₀ _ hNq, mul_assoc]
  rw [hX] at h'
  have h1 : (rescale (N : ℚ)) (mk fun n =>
      ((Polynomial.bernoulli n).eval ((a : ℚ) / N)) / (n.factorial : ℚ))
      = mk fun n => ((N : ℚ) ^ n * ((Polynomial.bernoulli n).eval ((a : ℚ) / N)) /
        (n.factorial : ℚ)) := by
    ext n
    simp only [coeff_rescale, coeff_mk]
    ring
  rw [h1] at h'
  -- divide by `N`, using the cancellation of `zpow`
  have hC : C ((N : ℚ)⁻¹) * (mk fun n => ((N : ℚ) ^ n *
      ((Polynomial.bernoulli n).eval ((a : ℚ) / N)) / (n.factorial : ℚ)))
      = mk fun n => ((N : ℚ) ^ ((n : ℤ) - 1) *
        ((Polynomial.bernoulli n).eval ((a : ℚ) / N)) / (n.factorial : ℚ)) := by
    ext n
    simp only [coeff_C_mul, coeff_mk]
    have hc := zpow_pred_mul_self (N := N) hN n
    rw [← hc]
    ring_nf
    rw [mul_inv_cancel₀ hNq, one_mul]
  have h'' := congrArg (fun f => C ((N : ℚ)⁻¹) * f) h'
  have hL : C ((N : ℚ)⁻¹) * ((mk fun n => ((N : ℚ) ^ n *
      ((Polynomial.bernoulli n).eval ((a : ℚ) / N)) / (n.factorial : ℚ))) *
        (rescale (N : ℚ) (exp ℚ) - 1))
      = (mk fun n => ((N : ℚ) ^ ((n : ℤ) - 1) *
          ((Polynomial.bernoulli n).eval ((a : ℚ) / N)) / (n.factorial : ℚ))) *
        (rescale (N : ℚ) (exp ℚ) - 1) := by
    rw [← mul_assoc, hC]
  have hR : C ((N : ℚ)⁻¹) * (C (N : ℚ) * (X * rescale (a : ℚ) (exp ℚ)))
      = X * rescale (a : ℚ) (exp ℚ) := by
    rw [← mul_assoc, ← map_mul, inv_mul_cancel₀ hNq, map_one, one_mul]
  rw [hL, hR] at h''
  exact h''

/-- **The generating function of Definition 2.2**: the character-weighted sum. -/
theorem generalizedBernoulli_egf {N : ℕ} (hN : 0 < N) (χ : DirichletCharacter ℚ N) :
    (mk fun n => generalizedBernoulli χ n / (n.factorial : ℚ)) *
      (rescale (N : ℚ) (exp ℚ) - 1)
      = ∑ a ∈ Finset.Icc 1 N, χ a • (X * rescale (a : ℚ) (exp ℚ)) := by
  have hterm : ∀ a ∈ Finset.Icc 1 N,
      (mk fun n => ((N : ℚ) ^ ((n : ℤ) - 1) *
        ((Polynomial.bernoulli n).eval ((a : ℚ) / N)) / (n.factorial : ℚ))) *
        (rescale (N : ℚ) (exp ℚ) - 1) = X * rescale (a : ℚ) (exp ℚ) :=
    fun a _ => bernoulliPoly_egf_rescale hN a
  have hsum : (∑ a ∈ Finset.Icc 1 N, χ a •
        ((mk fun n => ((N : ℚ) ^ ((n : ℤ) - 1) *
          ((Polynomial.bernoulli n).eval ((a : ℚ) / N)) / (n.factorial : ℚ))) *
          (rescale (N : ℚ) (exp ℚ) - 1)))
      = ∑ a ∈ Finset.Icc 1 N, χ a • (X * rescale (a : ℚ) (exp ℚ)) := by
    refine Finset.sum_congr rfl fun a ha => ?_
    rw [hterm a ha]
  rw [← hsum]
  have hsmul : ∀ a ∈ Finset.Icc 1 N, χ a •
        ((mk fun n => ((N : ℚ) ^ ((n : ℤ) - 1) *
          ((Polynomial.bernoulli n).eval ((a : ℚ) / N)) / (n.factorial : ℚ))) *
          (rescale (N : ℚ) (exp ℚ) - 1))
      = (χ a • (mk fun n => ((N : ℚ) ^ ((n : ℤ) - 1) *
          ((Polynomial.bernoulli n).eval ((a : ℚ) / N)) / (n.factorial : ℚ)))) *
        (rescale (N : ℚ) (exp ℚ) - 1) := by
    intro a _
    rw [smul_mul_assoc]
  rw [Finset.sum_congr rfl hsmul, ← Finset.sum_mul]
  congr 1
  ext n
  simp only [map_sum, coeff_smul, coeff_mk, smul_eq_mul, generalizedBernoulli]
  rw [Finset.mul_sum, Finset.sum_div]
  refine Finset.sum_congr rfl fun a _ => ?_
  have hmap : Polynomial.map (algebraMap ℚ ℚ) (Polynomial.bernoulli n)
      = Polynomial.bernoulli n := by
    rw [show (algebraMap ℚ ℚ) = RingHom.id ℚ from rfl, Polynomial.map_id]
  rw [hmap]
  ring

lemma coeff_exp_sub_one_rescale (hN : 0 < N) (m : ℕ) :
    coeff (m + 1) (rescale (N : ℚ) (exp ℚ) - 1)
      = (N : ℚ) ^ (m + 1) / ((m + 1).factorial : ℚ) := by
  have h1 : (coeff (m + 1)) (1 : ℚ⟦X⟧) = 0 := by
    rw [coeff_one, if_neg (Nat.succ_ne_zero m)]
  rw [map_sub, coeff_rescale, coeff_exp, h1, sub_zero]
  simp [div_eq_mul_inv]

lemma coeff_zero_rescale_exp_sub_one (hN : 0 < N) :
    coeff 0 (rescale (N : ℚ) (exp ℚ) - 1) = 0 := by
  have h1 : (coeff 0) (1 : ℚ⟦X⟧) = 1 := by rw [coeff_one, if_pos rfl]
  rw [map_sub, coeff_rescale, coeff_exp, h1]
  norm_num

lemma coeff_mk_mul_exp_sub_one (hN : 0 < N) (f : ℕ → ℚ) (n : ℕ) :
    coeff n ((mk f) * (rescale (N : ℚ) (exp ℚ) - 1))
      = ∑ i ∈ Finset.range n, f i * ((N : ℚ) ^ (n - i) / ((n - i).factorial : ℚ)) := by
  rw [coeff_mul, Finset.Nat.sum_antidiagonal_eq_sum_range_succ_mk, Finset.sum_range_succ]
  have hzero : (coeff (n, n - n).1) (mk f) *
      (coeff (n, n - n).2) (rescale (N : ℚ) (exp ℚ) - 1) = 0 := by
    simp only [coeff_mk, Nat.sub_self]
    rw [coeff_zero_rescale_exp_sub_one hN, mul_zero]
  rw [hzero, add_zero]
  refine Finset.sum_congr rfl fun i hi => ?_
  have hlt : i < n := Finset.mem_range.mp hi
  simp only [coeff_mk]
  rw [show n - i = (n - i - 1) + 1 by omega, coeff_exp_sub_one_rescale hN]

end Thesis
