/-
Copyright (c) 2026 The thesis-formalization contributors.
Released under Apache 2.0 license as described in the file LICENSE.
-/
import Thesis.LocalEulerFactor
import Thesis.Lemma215

/-!
# The local Euler factor for the characters of a subfield of `ℚ(μ_n)`

This file combines the two preceding files:

* `Lemma215` identifies the characters of a subfield `F ⊆ ℚ(μ_n)` with a subgroup
  `fixingCharacters n K F` of the level-`n` Dirichlet characters, and proves
  `#H_F = [F : ℚ]`;
* `LocalEulerFactor` computes, for any subgroup `H` of a character group and any
  unit `σ`, the product `∏_{χ ∈ H} (1 - χ(σ) X)` as
  `(1 - X ^ #im) ^ #ker`.

Here they are put together: for every unit `u` of `ℤ/nℤ` the product of the local
factors `1 - χ(u) X` over the characters of `F` collapses to a power of
`1 - X ^ #im`, where `#im` is the number of distinct values taken by `χ ↦ χ(u)` on
the characters of `F` and `#ker` is the multiplicity of each value.  In the notation
of the thesis's proof of Theorem 2.16, `#im = f_p` and `#ker = g_p` once `u` is the
Frobenius element at `p` and `H = H_F`; the identification of `#im` with the order of
the Frobenius class in `Gal(F/ℚ)` is the remaining transport recorded in `NOTES.md`.

On integration into the project, the two import lines become
`import Thesis.LocalEulerFactor` and `import Thesis.Lemma215`.
-/

noncomputable section

open Polynomial

namespace Thesis

variable (n : ℕ) [NeZero n]
variable (K : Type*) [Field K] [NumberField K] [IsCyclotomicExtension {n} ℚ K]
variable [IsAbelianGalois ℚ K]
variable [HasEnoughRootsOfUnity ℂ (Monoid.exponent (ZMod n)ˣ)]

/-- The local Euler factor of the characters of a subfield `F ⊆ ℚ(μ_n)`. -/
theorem prod_one_sub_mul_X_fixingCharacters (F : IntermediateField ℚ K)
    [Fintype ↥(fixingCharacters n K F)] (u : (ZMod n)ˣ) :
    ∏ χ : fixingCharacters n K F,
        (1 - Polynomial.C ((χ : DirichletCharacter ℂ n) u) * Polynomial.X) =
      (1 - Polynomial.X ^ Nat.card (evalOnSubgroup (fixingCharacters n K F) u).range) ^
        Nat.card (evalOnSubgroup (fixingCharacters n K F) u).ker :=
  prod_one_sub_mul_X_subgroup_characters (fixingCharacters n K F) u

/-- The evaluated form: `∏_χ (1 - χ(u) t) = (1 - t ^ #im) ^ #ker`. -/
theorem prod_one_sub_fixingCharacters (F : IntermediateField ℚ K)
    [Fintype ↥(fixingCharacters n K F)] (u : (ZMod n)ˣ) (t : ℂ) :
    ∏ χ : fixingCharacters n K F, (1 - (χ : DirichletCharacter ℂ n) u * t) =
      (1 - t ^ Nat.card (evalOnSubgroup (fixingCharacters n K F) u).range) ^
        Nat.card (evalOnSubgroup (fixingCharacters n K F) u).ker := by
  have h := congrArg (Polynomial.eval t)
    (prod_one_sub_mul_X_fixingCharacters n K F u)
  simp only [Polynomial.eval_prod, Polynomial.eval_sub, Polynomial.eval_one,
    Polynomial.eval_mul, Polynomial.eval_C, Polynomial.eval_X, Polynomial.eval_pow] at h
  simpa [Nat.card_eq_fintype_card] using h

/-- The multiplicity of each value `χ(u)`, `χ` a character of `F`, is `#H_F / #im`. -/
theorem card_ker_fixingCharacters (F : IntermediateField ℚ K)
    [Fintype ↥(fixingCharacters n K F)] (u : (ZMod n)ˣ) :
    Nat.card (evalOnSubgroup (fixingCharacters n K F) u).ker =
      Nat.card (fixingCharacters n K F) /
        Nat.card (evalOnSubgroup (fixingCharacters n K F) u).range :=
  card_ker_evalOnSubgroup_eq_div (fixingCharacters n K F) u

/-- The degree `[F : ℚ]`, which is the exponent's numerator. -/
theorem card_fixingCharacters_eq_finrank (F : IntermediateField ℚ K) :
    Nat.card (fixingCharacters n K F) = Module.finrank ℚ F :=
  card_fixingCharacters n K F

end Thesis
