-- 실거래(Upbit) + 운영 모드 + 종목별 분봉

ALTER TABLE gc_app_settings
    ADD COLUMN trading_mode VARCHAR(10) NOT NULL DEFAULT 'PAPER'
        COMMENT 'PAPER | LIVE | BOTH' AFTER paper_auto_trade_budget_pct,
    ADD COLUMN live_auto_trade_enabled TINYINT(1) NOT NULL DEFAULT 0
        COMMENT '실거래 자동매매 ON' AFTER trading_mode,
    ADD COLUMN upbit_access_key VARCHAR(128) NULL
        COMMENT '업비트 Open API Access Key' AFTER live_auto_trade_enabled,
    ADD COLUMN upbit_secret_key VARCHAR(256) NULL
        COMMENT '업비트 Open API Secret Key' AFTER upbit_access_key;

ALTER TABLE gc_live_strategy_settings
    ADD COLUMN candle_type VARCHAR(10) NOT NULL DEFAULT '1m'
        COMMENT '전략 평가·WS warm-up 분봉: 1m,3m,5m,15m,30m,1h,4h,1d'
        AFTER execution_type;

CREATE TABLE IF NOT EXISTS gc_live_trade (
    id              BIGINT AUTO_INCREMENT PRIMARY KEY,
    device_id       VARCHAR(100)    NOT NULL,
    user_id         BIGINT          NULL,
    symbol          VARCHAR(30)     NOT NULL COMMENT 'KRW-BTC',
    side            VARCHAR(8)      NOT NULL COMMENT 'BUY | SELL',
    order_kind      VARCHAR(16)     NOT NULL DEFAULT 'market',
    source          VARCHAR(32)     NOT NULL DEFAULT 'STRATEGY' COMMENT 'STRATEGY | MANUAL | STOP_LOSS | TAKE_PROFIT',
    strategy_id     BIGINT          NULL,
    upbit_order_uuid VARCHAR(64)    NULL,
    price           DECIMAL(20, 8)  NOT NULL,
    quantity        DECIMAL(24, 12) NOT NULL,
    gross_amount    DECIMAL(20, 2)  NOT NULL,
    fee_amount      DECIMAL(20, 2)  NULL,
    state           VARCHAR(24)     NOT NULL DEFAULT 'done',
    created_at      DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP,
    INDEX idx_live_trade_device (device_id),
    INDEX idx_live_trade_symbol (symbol),
    INDEX idx_live_trade_created (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
  COMMENT='업비트 실거래 체결 이력';
