-- AlgoDesk schema. Run this once in cPanel → phpMyAdmin (select your database first,
-- then the SQL tab, paste, Go). Safe to re-run.

CREATE TABLE IF NOT EXISTS app_state (
  id          TINYINT UNSIGNED NOT NULL PRIMARY KEY,   -- always 1 (single row)
  state       LONGTEXT NOT NULL,                        -- JSON: wallet, settings, positions, equity curve
  updated_at  TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS trades (
  id            VARCHAR(24) NOT NULL PRIMARY KEY,       -- client-generated trade id
  sym           VARCHAR(16),
  name          VARCHAR(64),
  dir           VARCHAR(4),
  strategy      VARCHAR(32),
  strategy_name VARCHAR(64),
  qty           INT,
  entry         DECIMAL(12,2),
  exit_price    DECIMAL(12,2),
  sl            DECIMAL(12,2),
  target        DECIMAL(12,2),
  gross         DECIMAL(14,2),
  costs         DECIMAL(12,2),
  net           DECIMAL(14,2),
  result        VARCHAR(10),
  exit_reason   VARCHAR(32),
  prob          INT,
  score         INT,
  duration_min  INT,
  day_no        INT,
  opened_at     BIGINT,
  closed_at     BIGINT,
  created_at    TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  INDEX idx_sym (sym),
  INDEX idx_closed (closed_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
