-- =============================================================================
--  CAPTURE-AND-LIQUIFY SWING ENGINE — Database Setup
--  Run this ONCE in phpMyAdmin after creating your database.
--  Replace 'swing_engine' with whatever you named your database in cPanel.
-- =============================================================================

CREATE TABLE IF NOT EXISTS `engine_state` (
  `id`           INT AUTO_INCREMENT PRIMARY KEY,
  `engine`       VARCHAR(20)   NOT NULL,          -- 'SMH_IGV' or 'QQQ_XLV'
  `z_score`      DECIMAL(8,4)  NOT NULL DEFAULT 0,
  `deploy_stage` TINYINT       NOT NULL DEFAULT 0, -- 0=flat, 1-4=tranche
  `position`     VARCHAR(30)   NOT NULL DEFAULT 'FLAT',
  `partial_done` TINYINT(1)    NOT NULL DEFAULT 0,
  `velocity`     VARCHAR(10)   NOT NULL DEFAULT 'OK', -- 'OK' or 'LOW'
  `swing_age`    TINYINT       NOT NULL DEFAULT 0,
  `signal`       VARCHAR(60)   NOT NULL DEFAULT '',   -- last alert title
  `raw_message`  TEXT,
  `received_at`  DATETIME      NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at`   DATETIME      NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Seed one row per engine so the dashboard always has something to read
INSERT INTO `engine_state` (`engine`, `position`, `signal`) VALUES
  ('SMH_IGV', 'FLAT', 'Awaiting first signal'),
  ('QQQ_XLV',  'FLAT', 'Awaiting first signal');
