import Erdos1135.Tao.Syracuse.FirstPassage
import Erdos1135.Tao.Statement

/-!
# Time-bounded Syracuse and Collatz hitting predicates

This leaf adds natural-valued time budgets without changing the existing
eventual-hitting surfaces.  Its passage lemmas are the deterministic algebra
used by the logarithmic-time Section 3 trace.
-/

namespace Erdos1135
namespace Tao

/-- The Syracuse orbit hits the natural threshold `B` within at most `T`
Syracuse steps. -/
def syracuseHitsAtMostWithin (N B T : ℕ) : Prop :=
  ∃ m : ℕ, m ≤ T ∧ (syracuse^[m]) N ≤ B

/-- The Syracuse orbit hits strictly below the real threshold `b` within at
most `T` Syracuse steps. -/
def syracuseHitsBelowRealWithin (N : ℕ) (b : ℝ) (T : ℕ) : Prop :=
  ∃ m : ℕ, m ≤ T ∧ ((syracuse^[m]) N : ℝ) < b

/-- The standard Collatz orbit hits strictly below the real threshold `b`
within at most `T` raw Collatz steps. -/
def collatzHitsBelowRealWithin (N : ℕ) (b : ℝ) (T : ℕ) : Prop :=
  ∃ m : ℕ, m ≤ T ∧ ((collatzStep^[m]) N : ℝ) < b

/-- Positive odd sources that hit `B` inside a fixed Syracuse-step budget. -/
def syracuseThresholdGoodWithin (B T : ℕ) : Set ℕ :=
  {N | Odd N ∧ syracuseHitsAtMostWithin N B T}

theorem syracuseHitsAtMostWithin.mono
    {N B S T : ℕ} (hST : S ≤ T)
    (h : syracuseHitsAtMostWithin N B S) :
    syracuseHitsAtMostWithin N B T := by
  rcases h with ⟨m, hmS, hm⟩
  exact ⟨m, hmS.trans hST, hm⟩

theorem syracuseHitsAtMostWithin.to_hitsAtMost
    {N B T : ℕ} (h : syracuseHitsAtMostWithin N B T) :
    syracuseHitsAtMost N B := by
  rcases h with ⟨m, _hmT, hm⟩
  exact ⟨m, hm⟩

theorem syracuseHitsAtMostWithin.zero_of_le
    {N B : ℕ} (hNB : N ≤ B) :
    syracuseHitsAtMostWithin N B 0 := by
  exact ⟨0, le_rfl, by simpa using hNB⟩

/-- Passing to a genuine first-passage location never increases the remaining
fixed-target time budget. -/
theorem syracuseHitsAtMostWithin_of_passLocationAtMost
    {q b N M T : ℕ} (hpass : syracusePassLocationAtMost q N M)
    (hN : syracuseHitsAtMostWithin N b T) :
    syracuseHitsAtMostWithin M b T := by
  rcases hpass with ⟨n, hn, rfl⟩
  rcases hN with ⟨k, hkT, hk⟩
  by_cases hkn : k < n
  · refine ⟨0, Nat.zero_le T, ?_⟩
    simp only [Function.iterate_zero, id_eq]
    exact hn.1.trans (Nat.le_of_lt ((hn.2 k hkn).trans_le hk))
  · have hnk : n ≤ k := Nat.le_of_not_gt hkn
    refine ⟨k - n, (Nat.sub_le k n).trans hkT, ?_⟩
    simpa only [← Function.iterate_add_apply, Nat.sub_add_cancel hnk] using hk

/-- If a source reaches the passage threshold within `S` steps and its
genuine passage endpoint reaches `b` within `T` more steps, then the source
reaches `b` within the additive budget `S + T`. -/
theorem syracuseHitsAtMostWithin_of_passLocationAtMost_add
    {q b N M S T : ℕ} (hpass : syracusePassLocationAtMost q N M)
    (hq : syracuseHitsAtMostWithin N q S)
    (hM : syracuseHitsAtMostWithin M b T) :
    syracuseHitsAtMostWithin N b (S + T) := by
  rcases hpass with ⟨n, hn, rfl⟩
  rcases hq with ⟨r, hrS, hr⟩
  have hnr : n ≤ r := by
    by_contra hnot
    have hrn : r < n := Nat.lt_of_not_ge hnot
    exact (Nat.not_lt_of_ge hr) (hn.2 r hrn)
  rcases hM with ⟨k, hkT, hk⟩
  refine ⟨n + k, add_le_add (hnr.trans hrS) hkT, ?_⟩
  simpa only [Nat.add_comm n k, Function.iterate_add_apply] using hk

/-- A bounded fixed-target-good source has a bounded-good totalized passage
endpoint.  On a genuine passage the remaining budget can only shrink; on a
no-hit path the sentinel `1` is good at time zero. -/
theorem syracuseHitsAtMostWithin_passLocationAtMostOrOne
    {b q N T : ℕ} (hb : 1 ≤ b) (hq : 1 ≤ q)
    (hN : syracuseHitsAtMostWithin N b T) :
    syracuseHitsAtMostWithin
      (syracusePassLocationAtMostOrOne q N hq : ℕ) b T := by
  classical
  by_cases hhit : syracuseHitsAtMost N q
  · rw [syracusePassLocationAtMostOrOne_of_hitsAtMost hq hhit]
    exact syracuseHitsAtMostWithin_of_passLocationAtMost
      (syracusePassLocationAtMost_of_hitsAtMost N q hhit) hN
  · rw [syracusePassLocationAtMostOrOne_of_not_hitsAtMost hq hhit]
    change syracuseHitsAtMostWithin 1 b T
    exact syracuseHitsAtMostWithin.mono (Nat.zero_le T)
      (syracuseHitsAtMostWithin.zero_of_le hb)

/-- A bounded genuine passage reverses the totalized endpoint map with an
additive time budget. -/
theorem syracuseHitsAtMostWithin_of_passLocationAtMostOrOne_add
    {b q N S T : ℕ} (hqOne : 1 ≤ q)
    (hq : syracuseHitsAtMostWithin N q S)
    (hpass : syracuseHitsAtMostWithin
      (syracusePassLocationAtMostOrOne q N hqOne : ℕ) b T) :
    syracuseHitsAtMostWithin N b (S + T) := by
  classical
  have hhit : syracuseHitsAtMost N q := hq.to_hitsAtMost
  rw [syracusePassLocationAtMostOrOne_of_hitsAtMost hqOne hhit] at hpass
  exact syracuseHitsAtMostWithin_of_passLocationAtMost_add
    (syracusePassLocationAtMost_of_hitsAtMost N q hhit) hq hpass

end Tao
end Erdos1135
