/-
PhiPipelineReceipt.lean — Ring 5: pipeline receipt

This ring closes the outward dependency spiral by connecting:
  • Ring 3 (PhiDNALayout / mkPhiLayout) — the constructor
  • Ring 4 (PhiConsistency)             — the ADMIT/QUARANTINE predicates

Core theorem: a layout built with consistency vector `con : Fin 6 → Bool` is
ADMITted if and only if every entry of `con` is `true`.

  isAdmit (mkPhiLayout f τ del con) ↔ ∀ i : Fin 6, con i = true

This is the formal receipt: knowing `con` is sufficient to read the admission
decision without inspecting the full 30-base sequence.

Anti-drift: any change to the slot mapping, to the G/T encoding, or to the
8+8+8+6 layout partition breaks this theorem before it reaches the user.
-/

import SilverSight.PhiConsistency
import Mathlib.Tactic

namespace SilverSight.PhiPipelineReceipt

open HachimojiBase
open SilverSight.PhiDNALayout
open SilverSight.PhiConsistency

-- ============================================================
-- §1  KEY LEMMA: Layer 4 evaluates to encodeRule (con i)
-- ============================================================

/-- For any layout built by mkPhiLayout, the base at Layer 4 slot i
    is exactly encodeRule (con i) — G if the rule passed, T if not. -/
lemma mkPhiLayout_layer4 (f τ del : Fin 8 → HachimojiBase) (con : Fin 6 → Bool)
    (i : Fin 6) :
    (mkPhiLayout f τ del con).layer4 i = encodeRule (con i) := by
  have hi : i.val < 6 := i.isLt
  simp only [PhiLayout.layer4, mkPhiLayout, encodeRule]
  have h1 : ¬(24 + i.val < 8)  := by omega
  have h2 : ¬(24 + i.val < 16) := by omega
  have h3 : ¬(24 + i.val < 24) := by omega
  simp only [dif_neg h1, dif_neg h2, dif_neg h3]
  have hval : 24 + i.val - 24 = i.val := by omega
  simp only [hval, Fin.eta]
  cases h : con i <;> rfl

-- ============================================================
-- §2  SLOT SURJECTIVITY (and equivalence)
-- ============================================================

/-- Every Fin 6 slot is occupied by exactly one ConsistencyRule. -/
theorem slot_surjective : Function.Surjective ConsistencyRule.slot := by
  intro i
  fin_cases i <;>
    first
    | exact ⟨.balanced_parens,      rfl⟩
    | exact ⟨.valid_operator_order, rfl⟩
    | exact ⟨.valid_variable_name,  rfl⟩
    | exact ⟨.no_empty_expression,  rfl⟩
    | exact ⟨.single_expression,    rfl⟩
    | exact ⟨.defined_reference,    rfl⟩

/-- slot is a bijection between ConsistencyRule and Fin 6. -/
noncomputable def slotEquiv : ConsistencyRule ≃ Fin 6 where
  toFun    := ConsistencyRule.slot
  invFun   := fun i => (slot_surjective i).choose
  left_inv := fun r => slot_injective (slot_surjective r.slot).choose_spec
  right_inv := fun i => (slot_surjective i).choose_spec

-- ============================================================
-- §3  ADMIT IFF (rule form)
-- ============================================================

/-- ADMIT ↔ every rule passes (in terms of ConsistencyRule). -/
theorem mkPhiLayout_isAdmit_iff_rules
    (f τ del : Fin 8 → HachimojiBase) (con : Fin 6 → Bool) :
    isAdmit (mkPhiLayout f τ del con) ↔
    ∀ r : ConsistencyRule, con r.slot = true := by
  simp only [isAdmit, mkPhiLayout_layer4, encodeRule]
  constructor
  · intro h r
    have hr := h r
    cases h_con : con r.slot <;> simp_all
  · intro h r
    simp [h r]

-- ============================================================
-- §4  PIPELINE RECEIPT (index form)
-- ============================================================

/-- Pipeline admit receipt: ADMIT ↔ all 6 consistency booleans are true. -/
theorem pipeline_admit_iff
    (f τ del : Fin 8 → HachimojiBase) (con : Fin 6 → Bool) :
    isAdmit (mkPhiLayout f τ del con) ↔ ∀ i : Fin 6, con i = true := by
  rw [mkPhiLayout_isAdmit_iff_rules]
  constructor
  · intro h i
    obtain ⟨r, hr⟩ := slot_surjective i
    simpa [hr] using h r
  · intro h r
    exact h r.slot

/-- Pipeline quarantine receipt: QUARANTINE ↔ some consistency boolean is false. -/
theorem pipeline_quarantine_iff
    (f τ del : Fin 8 → HachimojiBase) (con : Fin 6 → Bool) :
    isQuarantine (mkPhiLayout f τ del con) ↔ ∃ i : Fin 6, con i = false := by
  simp only [isQuarantine, mkPhiLayout_layer4, encodeRule]
  constructor
  · intro ⟨r, hr⟩
    exact ⟨r.slot, by cases h : con r.slot <;> simp_all⟩
  · intro ⟨i, hi⟩
    obtain ⟨r, hr⟩ := slot_surjective i
    exact ⟨r, by simp [hr, hi]⟩

-- ============================================================
-- §5  COMPLEMENTARITY
-- ============================================================

/-- For mkPhiLayout: ADMIT ↔ NOT QUARANTINE. -/
theorem pipeline_admit_iff_not_quarantine
    (f τ del : Fin 8 → HachimojiBase) (con : Fin 6 → Bool) :
    isAdmit (mkPhiLayout f τ del con) ↔
    ¬isQuarantine (mkPhiLayout f τ del con) := by
  rw [pipeline_admit_iff, pipeline_quarantine_iff]
  constructor
  · intro hall ⟨i, hi⟩
    exact absurd hi (by simp [hall i])
  · intro hnone i
    rcases Bool.eq_false_or_eq_true (con i) with h | h
    · exact h
    · exfalso; exact hnone ⟨i, h⟩

-- ============================================================
-- §6  DECISION PROCEDURE
-- ============================================================

/-- Computable admission check: AND all 6 consistency booleans. -/
def decidePipeline (con : Fin 6 → Bool) : Bool :=
  con ⟨0, by omega⟩ && con ⟨1, by omega⟩ && con ⟨2, by omega⟩ &&
  con ⟨3, by omega⟩ && con ⟨4, by omega⟩ && con ⟨5, by omega⟩

theorem decidePipeline_correct
    (f τ del : Fin 8 → HachimojiBase) (con : Fin 6 → Bool) :
    isAdmit (mkPhiLayout f τ del con) ↔ decidePipeline con = true := by
  rw [pipeline_admit_iff]
  simp only [decidePipeline, Bool.and_eq_true]
  constructor
  · intro h
    exact ⟨⟨⟨⟨⟨h ⟨0, by omega⟩, h ⟨1, by omega⟩⟩, h ⟨2, by omega⟩⟩,
             h ⟨3, by omega⟩⟩, h ⟨4, by omega⟩⟩, h ⟨5, by omega⟩⟩
  · intro ⟨⟨⟨⟨⟨h0, h1⟩, h2⟩, h3⟩, h4⟩, h5⟩ i
    fin_cases i <;> assumption

end SilverSight.PhiPipelineReceipt
