import Mathlib.Analysis.SpecialFunctions.Pow.Continuity
import Erdos1135.Tao.Section3.AmbientScalePilot

/-!
# Ambient Section 3 Least Crossing

This leaf selects the first descending ambient boundary whose value is at
most `B^(1 / alpha)`.  The weak crossing convention is intentional: equality
already makes the corresponding seed window time-zero good.  It can differ by
one index from Tao's printed strict crossing, so all endpoint polarities are
exported explicitly.
-/

namespace Erdos1135
namespace Tao

open Filter
open scoped Topology

noncomputable section

/-- For an ambient top strictly above one, its descending boundaries are
strictly decreasing. -/
theorem taoSection3AmbientBoundary_strictAnti
    {X : ℝ} (hX : 1 < X) :
    StrictAnti (taoSection3AmbientBoundary X) := by
  simpa only [taoSection3AmbientBoundary, Function.comp_apply] using
    (Real.strictMono_rpow_of_base_gt_one hX).comp_strictAnti
      (pow_right_strictAnti₀
        taoSection3AmbientRatio_pos taoSection3AmbientRatio_lt_one)

/-- Every positive ambient boundary sequence converges to one. -/
theorem tendsto_taoSection3AmbientBoundary_atTop
    {X : ℝ} (hX : 0 < X) :
    Tendsto (taoSection3AmbientBoundary X) atTop (nhds 1) := by
  have hpow :
      Tendsto (fun i : ℕ => taoSection3AmbientRatio ^ i) atTop (nhds 0) :=
    tendsto_pow_atTop_nhds_zero_of_lt_one
      taoSection3AmbientRatio_pos.le taoSection3AmbientRatio_lt_one
  have hrpow :=
    (Real.continuousAt_const_rpow (a := X) (b := 0) hX.ne').tendsto.comp hpow
  simpa only [taoSection3AmbientBoundary, Real.rpow_zero] using hrpow

/-- A positive target above one is eventually crossed by the descending
ambient boundary sequence. -/
theorem exists_taoSection3AmbientBoundary_le
    {B : ℕ} {X : ℝ} (hB : 2 ≤ B) (hX : 0 < X) :
    ∃ i : ℕ,
      taoSection3AmbientBoundary X i ≤
        (B : ℝ) ^ taoSection3AmbientRatio := by
  have hBone : (1 : ℝ) < (B : ℝ) := by exact_mod_cast (show 1 < B by omega)
  have htarget :
      (1 : ℝ) < (B : ℝ) ^ taoSection3AmbientRatio :=
    Real.one_lt_rpow hBone taoSection3AmbientRatio_pos
  obtain ⟨i, hi⟩ :=
    ((tendsto_taoSection3AmbientBoundary_atTop hX).eventually_lt_const
      htarget).exists
  exact ⟨i, hi.le⟩

/-- The first weak crossing of `B^(1 / alpha)` by the descending ambient
boundaries.  The index is exported as `K = j + 2`, avoiding natural
subtraction in the subsequent trace.  All strict and weak inequalities are
sharp: equality can occur at each displayed upper endpoint. -/
theorem exists_taoSection3AmbientLeastCrossing
    {B : ℕ} {X : ℝ} (hB : 2 ≤ B) (hBX : (B : ℝ) < X) :
    ∃ K j : ℕ,
      K = j + 2 ∧
      (∀ i, i < K →
        (B : ℝ) ^ taoSection3AmbientRatio <
          taoSection3AmbientBoundary X i) ∧
      (B : ℝ) ^ (taoSection3AmbientRatio ^ 2) <
        taoSection3AmbientBoundary X K ∧
      taoSection3AmbientBoundary X K ≤
        (B : ℝ) ^ taoSection3AmbientRatio ∧
      (B : ℝ) ^ taoSection3AmbientRatio <
        taoSection3AmbientBoundary X (j + 1) ∧
      taoSection3AmbientBoundary X (j + 1) ≤ (B : ℝ) ∧
      (B : ℝ) ^ (taoSection3AmbientRatio ^ 3) <
        taoSection3AmbientBoundary X (K + 1) ∧
      taoSection3AmbientBoundary X (K + 1) ≤
        (B : ℝ) ^ (taoSection3AmbientRatio ^ 2) := by
  classical
  have hBcast_nonneg : (0 : ℝ) ≤ (B : ℝ) := Nat.cast_nonneg B
  have hBone : (1 : ℝ) < (B : ℝ) := by exact_mod_cast (show 1 < B by omega)
  have hXone : (1 : ℝ) < X := hBone.trans hBX
  have hXpos : (0 : ℝ) < X := zero_lt_one.trans hXone
  have hXnonneg : (0 : ℝ) ≤ X := hXpos.le
  let P : ℕ → Prop := fun i =>
    taoSection3AmbientBoundary X i ≤
      (B : ℝ) ^ taoSection3AmbientRatio
  have hex : ∃ i : ℕ, P i :=
    exists_taoSection3AmbientBoundary_le hB hXpos
  let K : ℕ := Nat.find hex
  have hKcross : P K := Nat.find_spec hex
  have hbefore :
      ∀ i, i < K →
        (B : ℝ) ^ taoSection3AmbientRatio <
          taoSection3AmbientBoundary X i := by
    intro i hi
    exact lt_of_not_ge (Nat.find_min hex hi)
  have hKtwo : 2 ≤ K := by
    apply (Nat.le_find_iff hex 2).2
    intro i hi hPi
    have hi_cases : i = 0 ∨ i = 1 := by omega
    rcases hi_cases with rfl | rfl
    · have htarget_lt_B :
          (B : ℝ) ^ taoSection3AmbientRatio < (B : ℝ) :=
        Real.rpow_lt_self_of_one_lt hBone taoSection3AmbientRatio_lt_one
      have hPzero :
          X ≤ (B : ℝ) ^ taoSection3AmbientRatio := by
        simpa only [P, taoSection3AmbientBoundary, pow_zero,
          Real.rpow_one] using hPi
      linarith
    · have htarget_lt_source :
          (B : ℝ) ^ taoSection3AmbientRatio <
            X ^ taoSection3AmbientRatio :=
        Real.rpow_lt_rpow hBcast_nonneg hBX taoSection3AmbientRatio_pos
      have hPone :
          X ^ taoSection3AmbientRatio ≤
            (B : ℝ) ^ taoSection3AmbientRatio := by
        simpa only [P, taoSection3AmbientBoundary, pow_one] using hPi
      exact (not_le_of_gt htarget_lt_source) hPone
  obtain ⟨j, hKj⟩ := Nat.exists_eq_add_of_le' hKtwo
  have hpred_lt : j + 1 < K := by omega
  have hpred_lower :
      (B : ℝ) ^ taoSection3AmbientRatio <
        taoSection3AmbientBoundary X (j + 1) :=
    hbefore (j + 1) hpred_lt
  have hcross_lower :
      (B : ℝ) ^ (taoSection3AmbientRatio ^ 2) <
        taoSection3AmbientBoundary X K := by
    have hraised := Real.rpow_lt_rpow
      (Real.rpow_nonneg hBcast_nonneg taoSection3AmbientRatio)
      hpred_lower taoSection3AmbientRatio_pos
    calc
      (B : ℝ) ^ (taoSection3AmbientRatio ^ 2) =
          ((B : ℝ) ^ taoSection3AmbientRatio) ^
            taoSection3AmbientRatio := by
        rw [pow_two, Real.rpow_mul hBcast_nonneg]
      _ < (taoSection3AmbientBoundary X (j + 1)) ^
          taoSection3AmbientRatio := hraised
      _ = taoSection3AmbientBoundary X (j + 2) := by
        symm
        simpa only [taoSection3AmbientRatio] using
          taoSection3AmbientBoundary_succ_eq_rpow_inv hXnonneg (j + 1)
      _ = taoSection3AmbientBoundary X K := by rw [hKj]
  have hpred_upper :
      taoSection3AmbientBoundary X (j + 1) ≤ (B : ℝ) := by
    have hraised := Real.rpow_le_rpow
      (taoSection3AmbientBoundary_nonneg hXnonneg K) hKcross
      taoAlpha_pos.le
    calc
      taoSection3AmbientBoundary X (j + 1) =
          (taoSection3AmbientBoundary X (j + 2)) ^ taoAlpha := by
        symm
        exact taoSection3AmbientBoundary_succ_rpow hXnonneg (j + 1)
      _ = (taoSection3AmbientBoundary X K) ^ taoAlpha := by rw [hKj]
      _ ≤ ((B : ℝ) ^ taoSection3AmbientRatio) ^ taoAlpha := hraised
      _ = (B : ℝ) := by
        simpa only [taoSection3AmbientRatio] using
          Real.rpow_inv_rpow hBcast_nonneg taoAlpha_pos.ne'
  have hnext_lower :
      (B : ℝ) ^ (taoSection3AmbientRatio ^ 3) <
        taoSection3AmbientBoundary X (K + 1) := by
    have hraised := Real.rpow_lt_rpow
      (Real.rpow_nonneg hBcast_nonneg (taoSection3AmbientRatio ^ 2))
      hcross_lower taoSection3AmbientRatio_pos
    calc
      (B : ℝ) ^ (taoSection3AmbientRatio ^ 3) =
          ((B : ℝ) ^ (taoSection3AmbientRatio ^ 2)) ^
            taoSection3AmbientRatio := by
        rw [show taoSection3AmbientRatio ^ 3 =
            taoSection3AmbientRatio ^ 2 * taoSection3AmbientRatio by ring,
          Real.rpow_mul hBcast_nonneg]
      _ < (taoSection3AmbientBoundary X K) ^
          taoSection3AmbientRatio := hraised
      _ = taoSection3AmbientBoundary X (K + 1) := by
        symm
        simpa only [taoSection3AmbientRatio] using
          taoSection3AmbientBoundary_succ_eq_rpow_inv hXnonneg K
  have hnext_upper :
      taoSection3AmbientBoundary X (K + 1) ≤
        (B : ℝ) ^ (taoSection3AmbientRatio ^ 2) := by
    have hraised := Real.rpow_le_rpow
      (taoSection3AmbientBoundary_nonneg hXnonneg K) hKcross
      taoSection3AmbientRatio_pos.le
    calc
      taoSection3AmbientBoundary X (K + 1) =
          (taoSection3AmbientBoundary X K) ^
            taoSection3AmbientRatio := by
        simpa only [taoSection3AmbientRatio] using
          taoSection3AmbientBoundary_succ_eq_rpow_inv hXnonneg K
      _ ≤ ((B : ℝ) ^ taoSection3AmbientRatio) ^
          taoSection3AmbientRatio := hraised
      _ = (B : ℝ) ^ (taoSection3AmbientRatio ^ 2) := by
        rw [pow_two, Real.rpow_mul hBcast_nonneg]
  exact ⟨K, j, hKj, hbefore, hcross_lower, hKcross,
    hpred_lower, hpred_upper, hnext_lower, hnext_upper⟩

/-- Consumer-facing compression of the least-crossing packet.  The subsequent
trace only needs this subtraction-free `j`-indexed cut, not the private
`Nat.find` characterization. -/
theorem exists_taoSection3AmbientTraceCut
    {B : ℕ} {X : ℝ} (hB : 2 ≤ B) (hBX : (B : ℝ) < X) :
    ∃ j : ℕ,
      (B : ℝ) < taoSection3AmbientBoundary X j ∧
      (B : ℝ) ^ taoSection3AmbientRatio <
        taoSection3AmbientBoundary X (j + 1) ∧
      taoSection3AmbientBoundary X (j + 1) ≤ (B : ℝ) ∧
      (B : ℝ) ^ (taoSection3AmbientRatio ^ 2) <
        taoSection3AmbientBoundary X (j + 2) ∧
      taoSection3AmbientBoundary X (j + 2) ≤
        (B : ℝ) ^ taoSection3AmbientRatio ∧
      (B : ℝ) ^ (taoSection3AmbientRatio ^ 3) <
        taoSection3AmbientBoundary X (j + 3) ∧
      taoSection3AmbientBoundary X (j + 3) ≤
        (B : ℝ) ^ (taoSection3AmbientRatio ^ 2) := by
  rcases exists_taoSection3AmbientLeastCrossing hB hBX with
    ⟨K, j, hKj, _hbefore, hcrossLower, hcrossUpper,
      hpredLower, hpredUpper, hnextLower, hnextUpper⟩
  have hBcast_nonneg : (0 : ℝ) ≤ (B : ℝ) := Nat.cast_nonneg B
  have hXnonneg : (0 : ℝ) ≤ X := hBcast_nonneg.trans hBX.le
  have hleft : (B : ℝ) < taoSection3AmbientBoundary X j := by
    have hraised := Real.rpow_lt_rpow
      (Real.rpow_nonneg hBcast_nonneg taoSection3AmbientRatio)
      hpredLower taoAlpha_pos
    calc
      (B : ℝ) =
          ((B : ℝ) ^ taoSection3AmbientRatio) ^ taoAlpha := by
        symm
        simpa only [taoSection3AmbientRatio] using
          Real.rpow_inv_rpow hBcast_nonneg taoAlpha_pos.ne'
      _ < (taoSection3AmbientBoundary X (j + 1)) ^ taoAlpha := hraised
      _ = taoSection3AmbientBoundary X j :=
        taoSection3AmbientBoundary_succ_rpow hXnonneg j
  refine ⟨j, hleft, hpredLower, hpredUpper, ?_, ?_, ?_, ?_⟩
  · simpa only [hKj] using hcrossLower
  · simpa only [hKj] using hcrossUpper
  · simpa only [hKj, Nat.add_assoc, Nat.reduceAdd] using hnextLower
  · simpa only [hKj, Nat.add_assoc, Nat.reduceAdd] using hnextUpper

end

end Tao
end Erdos1135
