import Thesis.Theorem43Lemmas
import Thesis.KummerLeopoldt

/-!
Independent statement checks against the supplied thesis and Lean sources.
The counterexample proofs below do not invoke any of the archive's admitted results.
-/

set_option pp.universes false in
#check @Thesis.rootProduct_congruences
#print Thesis.ShiftPowerSeries

namespace ThesisStatementChecks

open Thesis

local instance : Fact (Nat.Prime 5) := ⟨by decide⟩

/-- The index 1 makes the required constant term vanish. -/
noncomputable def zeroShift : ShiftPowerSeries 5 1 where
  series := 0
  constant_term := by simp [padicIntToCp]

theorem rootProduct_zeroShift : rootProduct zeroShift 0 = 0 := by
  classical
  obtain ⟨ζ, hζ⟩ := HasEnoughRootsOfUnity.exists_primitiveRoot ℂ_[5] 5
  unfold rootProduct
  apply Finset.prod_eq_zero (i := ζ)
  · simpa using (mem_primitiveRoots (by decide : 0 < 5)).mpr hζ
  · simp [ShiftPowerSeries.eval, evalPowerSeries, zeroShift, padicIntToCp]

theorem bernoulli_two_nonzero_mod_five :
    ¬ ResidueModEq 5 ((bernoulli 2 : ℂ_[5]) / 2) 0 := by
  have h6 : ‖(6 : ℂ_[5])‖ = 1 :=
    norm_natCast_eq_one_of_not_dvd (p := 5) (n := 6) (by decide)
  have h2 : ‖(2 : ℂ_[5])‖ = 1 :=
    norm_natCast_eq_one_of_not_dvd (p := 5) (n := 2) (by decide)
  rw [ResidueModEq, sub_zero, bernoulli_two, Rat.cast_inv, Rat.cast_ofNat,
    norm_div, norm_inv, h6, h2]
  norm_num

/-- This is the second implication asserted by the archive when the
    independently bound test index is 2, the series index is 1 and α is 0. -/
theorem counterexample :
    AdmissibleIndex 5 2 ∧
      ¬ (¬ResidueModEq 5 ((bernoulli 2 : ℂ_[5]) / 2) 0 →
          ResidueModEq 5 (rootProduct zeroShift 0) 1) := by
  refine ⟨⟨by decide, even_two, by decide⟩, ?_⟩
  intro h
  have bad := h bernoulli_two_nonzero_mod_five
  simp [rootProduct_zeroShift, ResidueModEq] at bad

#print axioms counterexample

/-- The norm-bound part of Lemma 4.7 already follows from the archive's
convergence estimates. It says that the product belongs to the integers of
ℂₚ. Descent to ℤₚ and identification with a field norm require more. -/
theorem rootProduct_norm_bound {p seriesIndex : ℕ} [Fact p.Prime]
    (F : ShiftPowerSeries p seriesIndex) (hp : 3 ≤ p) (α : ℕ) :
    PadicIntegral p (rootProduct F α) := by
  classical
  unfold PadicIntegral rootProduct
  rw [norm_prod]
  apply Finset.prod_le_one
  · intro ζ _
    exact norm_nonneg _
  · intro ζ hζ
    have hprim : IsPrimitiveRoot ζ (p ^ (α + 1)) :=
      (mem_primitiveRoots (pow_pos (Fact.out : Nat.Prime p).pos _)).mp hζ
    have hz : ‖ζ - 1‖ < 1 :=
      norm_sub_one_lt_one_of_pow_prime_pow_eq_one (by omega) hprim.pow_eq_one
    apply norm_tsum_le_of_forall_le (summable_evalPowerSeries F.series hz) zero_le_one
    intro j
    rw [norm_mul, norm_pow]
    exact mul_le_one₀ (norm_padicIntToCp_le_one _) (pow_nonneg (norm_nonneg _) _)
      (pow_le_one₀ (norm_nonneg _) hz.le)

/-- The two indices in the archived declaration are independently quantified.
The second implication already gives a contradiction at the stated witness. -/
theorem independent_indices_false :
    ¬ ∀ {seriesIndex : ℕ} (F : ShiftPowerSeries 5 seriesIndex)
        (_hp : 3 ≤ 5) (α n : ℕ) (_hn : AdmissibleIndex 5 n),
        (ResidueModEq 5 ((bernoulli n : ℂ_[5]) / n) 0 →
          ResidueModEq 5 (rootProduct F α) 0) ∧
        (¬ ResidueModEq 5 ((bernoulli n : ℂ_[5]) / n) 0 →
          ResidueModEq 5 (rootProduct F α) 1) := by
  intro universalClaim
  exact counterexample.2 (universalClaim zeroShift (by decide) 0 2 counterexample.1).2

/-- The field of the archive structure forces −1/3 at p=5, n=2. -/
theorem archived_constant_at_five (F : ShiftPowerSeries 5 2) :
    padicIntToCp (PowerSeries.constantCoeff F.series) = -(1 / 3 : ℂ_[5]) := by
  rw [F.constant_term, bernoulli_two]
  norm_num

/-- The normalized value from the thesis, separately formalized in the archive. -/
theorem thesis_constant_at_five : klZeta 5 2 = 1 / 3 := by
  norm_num [klZeta, bernoulli_two]

/-- This comparison of the two displayed right-hand sides needs no series input. -/
theorem constant_formulas_unequal :
    (1 - (5 : ℂ_[5]) ^ (2 - 1 : ℕ)) * (bernoulli 2 : ℂ_[5]) / 2 ≠
      (klZeta 5 2 : ℂ_[5]) := by
  rw [thesis_constant_at_five, bernoulli_two]
  norm_num

theorem constant_term_mismatch (F : ShiftPowerSeries 5 2) :
    padicIntToCp (PowerSeries.constantCoeff F.series) ≠ (klZeta 5 2 : ℂ_[5]) := by
  rw [archived_constant_at_five, thesis_constant_at_five]
  norm_num

set_option maxHeartbeats 2000000 in
/-- Removing the denominator n changes the divisibility test when p divides n. -/
theorem normalized_bernoulli_at_ten :
    (5 : ℤ) ∣ (bernoulli 10).num ∧
      ¬ (5 : ℤ) ∣ (bernoulli 10 / 10).num ∧
      bernoulli 10 / 10 = 1 / 132 := by
  have h : bernoulli 10 = 5 / 66 := by decide +kernel
  norm_num [h]

/-- The primitive trivial character is present in every character group.
At index ten its generalized Bernoulli number is the ordinary one. -/
theorem trivial_character_at_ten :
    (5 : ℤ) ∣ (generalizedBernoulli (1 : DirichletCharacter ℚ 1) 10).num ∧
      ¬ (5 : ℤ) ∣ (bernoulli 10 / 10).num := by
  rw [generalizedBernoulli_modOne_of_ne_one _ (by decide : 10 ≠ 1)]
  exact ⟨normalized_bernoulli_at_ten.1, normalized_bernoulli_at_ten.2.1⟩

/-- The weighted increment permits the irregular branch of the thesis's argument. -/
theorem step_iff_weighted {p : ℕ} [Fact p.Prime] (α n : ℕ) :
    PadicModEq p (α + 1) (cyclotomicTowerValue p α n)
      (cyclotomicTowerValue p (α + 1) n) ↔
      PadicIntegral p (cyclotomicTowerValue p α n * (1 - newProduct p α n) /
        (p : ℂ_[p]) ^ (α + 1)) := by
  rw [PadicModEq, cyclotomicTowerValue_succ p α n Fact.out, mul_sub, mul_one]

/-- Base integrality and the weighted increments imply the complete tower congruence
for the archive's finite-product model. The weighted divisibility remains an input. -/
theorem main_congruence_of_weighted {p : ℕ} [Fact p.Prime] (n : ℕ)
    (hbase : PadicIntegral p (cyclotomicTowerValue p 0 n))
    (hweighted : ∀ α, PadicIntegral p (cyclotomicTowerValue p α n *
      (1 - newProduct p α n) / (p : ℂ_[p]) ^ (α + 1))) (α : ℕ) :
    PadicIntegral p (cyclotomicTowerValue p α n) ∧
      PadicModEq p (α + 1) (cyclotomicTowerValue p α n)
        (cyclotomicTowerValue p (α + 1) n) := by
  refine ⟨?_, (step_iff_weighted α n).mpr (hweighted α)⟩
  induction α with
  | zero => exact hbase
  | succ α ih =>
    exact padicIntegral_of_padicModEq p Fact.out
      (padicModEq_symm ((step_iff_weighted α n).mpr (hweighted α))) ih

/-- In the nonunit branch one factor of p at each level suffices.
Together with the archive's unit-branch reduction this implements the
two different valuation arguments in the proof of Theorem 4.1. -/
theorem main_congruence_of_divisible_factors {p : ℕ} [Fact p.Prime] (n : ℕ)
    (hbase : PadicModEq p 1 (cyclotomicTowerValue p 0 n) 0)
    (hnew : ∀ α, PadicModEq p 1 (newProduct p α n) 0) (α : ℕ) :
    PadicIntegral p (cyclotomicTowerValue p α n) ∧
      PadicModEq p (α + 1) (cyclotomicTowerValue p α n)
        (cyclotomicTowerValue p (α + 1) n) := by
  have htower : ∀ a, PadicIntegral p
      (cyclotomicTowerValue p a n / (p : ℂ_[p]) ^ (a + 1)) := by
    intro a
    induction a with
    | zero => simpa [PadicModEq] using hbase
    | succ a ih =>
      have hq : PadicIntegral p (newProduct p a n / p) := by
        simpa [PadicModEq] using hnew a
      rw [cyclotomicTowerValue_succ p a n Fact.out, pow_succ]
      simpa only [div_mul_div_comm] using padicIntegral_mul ih hq
  apply main_congruence_of_weighted n
  · exact padicIntegral_of_padicModEq p Fact.out hbase (padicIntegral_zero p)
  · intro a
    have hq : PadicIntegral p (newProduct p a n) :=
      padicIntegral_of_padicModEq p Fact.out (hnew a) (padicIntegral_zero p)
    convert padicIntegral_mul (htower a) (padicIntegral_sub (padicIntegral_one p) hq)
      using 1 <;> ring

#print axioms independent_indices_false
#print axioms rootProduct_norm_bound
#print axioms constant_formulas_unequal
#print axioms constant_term_mismatch
#print axioms normalized_bernoulli_at_ten
#print axioms trivial_character_at_ten
#print axioms main_congruence_of_weighted
#print axioms main_congruence_of_divisible_factors

end ThesisStatementChecks
