-- Phase 2 migration: server-authoritative spin authorizations and completion acknowledgments.

CREATE TABLE IF NOT EXISTS spins (
  id BINARY(16) PRIMARY KEY,
  wedding_id BINARY(16) NOT NULL,
  guest_id BINARY(16) NULL,
  session_id BINARY(16) NULL,
  idempotency_key VARCHAR(96) NOT NULL,
  state_version BIGINT UNSIGNED NOT NULL,
  result_type ENUM('NO_WIN','NORMAL_WIN','MADE_IN_HEAVEN','CELEBRATION_ONLY','EVENT_PAUSED','EVENT_CLOSED') NOT NULL,
  target_symbol_identity_id BINARY(16) NULL,
  symbols_json JSON NOT NULL,
  target_stops_json JSON NOT NULL,
  claim_token_hash BINARY(32) NULL,
  commitment_hash BINARY(32) NOT NULL,
  authorized_at DATETIME(6) NOT NULL,
  expires_at DATETIME(6) NOT NULL,
  completed_at DATETIME(6) NULL,
  UNIQUE KEY uq_phase2_spin_idempotency (wedding_id, session_id, idempotency_key),
  KEY ix_phase2_spin_wedding_time (wedding_id, authorized_at),
  CONSTRAINT fk_phase2_spin_wedding FOREIGN KEY (wedding_id) REFERENCES weddings(id)
) ENGINE=InnoDB;
