/-
Copyright (c) 2026 Anthropic, PBC. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
SPDX-License-Identifier: Apache-2.0
-/
import Minimal.Lemma2Reach
import Minimal.Lemma2Split

/-!
# Lemma 3.2 (step) of the paper, assembled

Paper: "**Lemma 3.2 (step).** Let (K, t) be a block of dimension d, L = L(t), τ > 0, ‖t‖ + τ ≤ 1. There are D ≥ 3,
a block (K^D, S) and µ > 0 with:
(a) ‖S − t ⊗ 1‖ ≤ τ, hence ‖S‖ ≤ 1.
(b) *Reach.* If ‖v‖, ‖w‖ ≤ 2 and ‖V⁻v‖, ‖V⁺w‖ ≥ ½, some A ∈ I₁(S) has Re⟨Av, w⟩ ≥ µ.
(c) *Split.* For A± ∈ I₁(t) some G ∈ I₁(S) has ‖G − A₋ ⊕ 0 ⊕ ⋯ ⊕ 0 ⊕ A₊‖ ≤ 24Lτ."

Lean: `lemma2`.  `K^D` is `Pw d K` with `D = d + 1` (so "D ≥ 3" is `2 ≤ d`), `t ⊗ 1` is `lift d t`,
`L(t)` is `Lconst t`, `Re⟨Av, w⟩` is `(⟪w, A v⟫_ℂ).re`, `A₋ ⊕ 0 ⊕ ⋯ ⊕ 0 ⊕ A₊` is `sepOp d A₊ A₋`.
Here the block `(K, t)` has dimension `q`; the conclusion records the size `Q = q·D` of the block `(K^D, S)`,
which the Construction needs to iterate.  `τ ≤ 1`, needed where Lemma 3.1
(`τ ∈ (0, 1]`) and the estimates of (c) are invoked, follows from `‖t‖ + τ ≤ 1`.
-/

open Polynomial Module
open scoped InnerProductSpace

noncomputable section

namespace IspMinimal

private theorem step_eps {E : Type} [NormedAddCommGroup E] [NormedSpace ℂ E] (R : E →L[ℂ] E)
    {σ : ℝ} (hσ : 0 < σ) : ∃ ε : ℂ, ε ≠ 0 ∧ ‖ε • R‖ ≤ σ := by
  have hR : 0 < ‖R‖ + 1 := by positivity
  have he : 0 < σ / (‖R‖ + 1) := div_pos hσ hR
  refine ⟨((σ / (‖R‖ + 1) : ℝ) : ℂ), Complex.ofReal_ne_zero.2 he.ne', ?_⟩
  refine (ContinuousLinearMap.opNorm_smul_le _ R).trans ?_
  rw [Complex.norm_real, Real.norm_of_nonneg he.le, div_mul_eq_mul_div, div_le_iff₀ hR]
  nlinarith [norm_nonneg R]

theorem sq2_ne_zero : sq2 ≠ 0 := by
  unfold sq2; exact Complex.ofReal_ne_zero.2 (by positivity)

/-- **Lemma 3.2 (step)** of the paper. -/
theorem lemma2 {K : Type} [NormedAddCommGroup K] [InnerProductSpace ℂ K] [FiniteDimensional ℂ K]
    (t : K →L[ℂ] K) (q : ℕ) (ht : IsBlock t q) (hq : 1 ≤ q) (τ : ℝ) (hτ : 0 < τ)
    (hn : ‖t‖ + τ ≤ 1) :
    ∃ d : ℕ, 2 ≤ d ∧ ∃ (S : Pw d K →L[ℂ] Pw d K) (μ : ℝ), 0 < μ ∧
      IsBlock S (q * (d + 1)) ∧
      -- (a)
      ‖S - lift d t‖ ≤ τ ∧ ‖S‖ ≤ 1 ∧
      -- (b) Reach
      (∀ v w : Pw d K, ‖v‖ ≤ 2 → ‖w‖ ≤ 2 → 1 / 2 ≤ ‖Vminus d K v‖ → 1 / 2 ≤ ‖Vplus d K w‖ →
        ∃ A ∈ I1 S, μ ≤ (⟪w, A v⟫_ℂ).re) ∧
      -- (c) Split
      (∀ Ap Am : K →L[ℂ] K, Ap ∈ I1 t → Am ∈ I1 t →
        ∃ G ∈ I1 S, ‖G - sepOp d Ap Am‖ ≤ 24 * Lconst t * τ) := by
  have ht1 : ‖t‖ ≤ 1 := by linarith [norm_nonneg t]
  have hτ1 : τ ≤ 1 := by linarith [norm_nonneg t]
  obtain ⟨d, hd, hd1, u, hu0, hul, hur, hu⟩ := exists_multipliers q τ hτ
  have hη0 : ((τ : ℝ) : ℂ) ≠ 0 := Complex.ofReal_ne_zero.2 hτ.ne'
  have hη : ‖((τ : ℝ) : ℂ)‖ ≤ τ := by rw [Complex.norm_real, Real.norm_of_nonneg hτ.le]
  obtain ⟨R, hR⟩ := exists_S t q ht hq u hu (τ : ℂ) hη0
  obtain ⟨ε, hε0, hε⟩ := step_eps R hτ
  obtain ⟨hblock, hpow⟩ := hR ε hε0
  set S := lift d t + ε • R with hSdef
  have hsub : S - lift d t = ε • R := by rw [hSdef, add_sub_cancel_left]
  have ha : ‖S - lift d t‖ ≤ τ := by rw [hsub]; exact hε
  have hS1 : ‖S‖ ≤ 1 := by
    have := norm_le_norm_add_norm_sub' S (lift d t)
    have := norm_lift_le (d := d) t
    linarith
  -- (b): qualitative statement, then compactness
  have hqual : ∀ ξ ζ : Pw d K, Vminus d K ξ ≠ 0 → Vplus d K ζ ≠ 0 →
      ∃ A ∈ Alg2 S, ⟪ζ, A ξ⟫_ℂ ≠ 0 := by
    intro ξ ζ hξ hζ
    refine qual_reach t q ht.pow_eq_zero hq hd u (τ : ℂ) ε S hblock hpow ξ ζ ?_ ?_
    · intro h; exact hξ (by rw [Vminus_apply, h, smul_zero])
    · intro h; exact hζ (by rw [Vplus_apply, h, smul_zero])
  obtain ⟨μ, hμ, hreach⟩ := reach_of_qual S (Vminus d K) (Vplus d K) hqual
  refine ⟨d, by omega, S, μ, hμ, hblock, ha, hS1, hreach, fun Ap Am hAp hAm => ?_⟩
  exact split hd1 t ht1 u hu0 hul τ hτ1 hur (τ : ℂ) hη S ha hS1 hq hε0 hpow (Lconst t)
    (Lconst_spec t) Ap Am hAp hAm

end IspMinimal
