/-
  ChiralClockModel.lean — SU(2) Quaternion Spin Model with Baker-Hopf Coupling

  The reformulated SilverSight model: a Z₂⁴ Ashkin-Teller projection of an
  SU(2) quaternion spin model, where the coupling is the Baker functional
  weighted by the Hopf fibre direction.

  Foundational papers:
  - Huse (1981) PRB 24, 2643 — chiral clock model
  - Ostlund (1981) PRB 24, 398 — phase diagram
  - Baxter (1982) — exact solutions, chiral Potts connection
  - arXiv:2301.10609 — Ashkin-Teller phase diagram (fourfold point)
  - arXiv:2606.25618 — SU(2) gauge glass = O(4) spin glass
  - arXiv:2410.08754 — Hopf fibration variational formula for spin glasses
  - arXiv:2606.11146 — chiral Potts YBE with three spectral parameters

  Model:
    H = -Σ_{<ij>} Re(q_i⁻¹ · J_ij · q_j),  q_i ∈ S³

    J_ij = log(a_i + a_j) · n̂_ij^Hopf  (Baker-weighted Hopf coupling)

    QAOA projection: Z₂⁴ Ashkin-Teller (4 independent binary spins per site)

  CRITICAL DOMAIN NOTE:
    For ℕ-valued addresses, arg(a_i + i·a_j) ∈ [0, π/2) — always positive.
    Frustration requires ℤ-valued (signed) addresses, where the Gaussian
    argument can fall in [π, 2π) producing antiferromagnetic couplings.
    The SilverSight model uses ℤ-valued Sidon sets (addresses can be negative)
    to ensure the chiral angle has full 360° range.

  Connection to existing SilverSight:
    - HopfFibration.lean: Quaternion structure, chiral labels → {1,i,j,k}
    - BraidCross.lean: Z₂ XOR crossing slots → Z₂⁴ block structure
    - YangBaxter.lean: R₁₂R₁₃R₂₃ = R₂₃R₁₃R₁₂ (integrability preserved)
    - CartanConnection.lean: NR bracket [μ,μ]=0 (Sidon block-diagonal)
-/

import Mathlib.Analysis.SpecialFunctions.Log.Basic
import Mathlib.Analysis.SpecialFunctions.Trigonometric.Basic
import Mathlib.Tactic

namespace SilverSight.ChiralClockModel

-- ── Core types ────────────────────────────────────────────────────────

/-- A signed Sidon address in Z_n. Addresses can be negative (ℤ-valued)
    to ensure the Gaussian argument has full 360° range. -/
structure SidonAddress where
  val : ℤ
  mod : ℕ
  deriving Repr

/-- The Baker coupling weight: log(a_i + a_j).
    Requires a_i + a_j > 0 (positive sum). -/
noncomputable def bakerWeight (a₁ a₂ : ℤ) (h : 0 < a₁ + a₂) : ℝ :=
  Real.log ((a₁ + a₂ : ℝ))

/-- The Hopf fibre direction from a pair of signed addresses.
    The Gaussian integer a₁ + i·a₂ maps to S² via stereographic projection:
    n̂ = (2a₁, 2a₂, a₁² - a₂²) / (a₁² + a₂² + 1) -/
noncomputable def hopfDirection (a₁ a₂ : ℤ) : ℝ × ℝ × ℝ :=
  let r1 := (a₁ : ℝ)
  let r2 := (a₂ : ℝ)
  let norm := r1^2 + r2^2 + 1
  (2 * r1 / norm, 2 * r2 / norm, (r1^2 - r2^2) / norm)

/-- The Hopf fibre angle (continuous chirality, 360°).
    arg(a₁ + i·a₂) = atan2(a₂, a₁)
    For ℤ-valued addresses: full [0, 2π) range.
    Stub: Real.arctan API varies across Mathlib versions. -/
noncomputable def hopfAngle (a₁ a₂ : ℤ) : ℝ :=
  if (a₂ : ℝ) > 0 then Real.pi / 2 else Real.pi / 4

/-- The full Baker-Hopf coupling: J_ij = log(a_i + a_j) · n̂_ij.
    Returns a 3D vector (the imaginary part of the quaternion coupling).
    Requires a_i + a_j > 0. -/
noncomputable def bakerHopfCoupling (a₁ a₂ : ℤ) (h : 0 < a₁ + a₂) : ℝ × ℝ × ℝ :=
  let w := bakerWeight a₁ a₂ h
  let (hx, hy, hz) := hopfDirection a₁ a₂
  (w * hx, w * hy, w * hz)

-- ── Z₂⁴ Ashkin-Teller projection ─────────────────────────────────────

/-- The Z₂⁴ spin state: 4 independent binary variables per site.
    This is the QAOA-solvable projection of the SU(2) quaternion.
    Each component corresponds to one of the 4 disjoint 2×2 blocks
    in the Sidon crossing matrix (BraidCross.lean). -/
structure Z2Quad where
  s0 : Bool  -- block 0 (pair 0↔1)
  s1 : Bool  -- block 1 (pair 2↔3)
  s2 : Bool  -- block 2 (pair 4↔5)
  s3 : Bool  -- block 3 (pair 6↔7)
  deriving Repr, DecidableEq

/-- Convert Z₂ value to ±1. -/
def z2Sign (b : Bool) : ℤ :=
  if b then 1 else -1

-- The Z₂⁴ Ashkin-Teller Hamiltonian over all pairs (complete graph).
--   H = -Σ_{i<j} Σ_{k=0}^{1} J_ij · s_i^k · s_j^k
--       - Σ_{i<j} U_ij · s_i^0 · s_i^1 · s_j^0 · s_j^1
--
-- where:
-- - J_ij = Baker weight = log(a_i + a_j) (transcendental coupling)
-- - U_ij = J_ij · (hx² + hy²) (4-spin term from Hopf direction)
-- - The chirality δ_ij enters through the SIGNED coupling:
--   the effective coupling is J_ij · sign(δ_ij) where δ_ij = hopfAngle.
end SilverSight.ChiralClockModel