-- ============================================================
-- V18: 모의투자 (페이퍼 트레이딩)
-- ============================================================

ALTER TABLE gc_app_settings
    ADD COLUMN paper_trading_enabled TINYINT(1) NOT NULL DEFAULT 1
        COMMENT '모의투자 기능 사용 (1=ON)',
    ADD COLUMN paper_initial_capital DECIMAL(20, 2) NOT NULL DEFAULT 10000000
        COMMENT '모의투자 초기 자본 (KRW)',
    ADD COLUMN paper_fee_rate_pct DECIMAL(8, 4) NOT NULL DEFAULT 0.0500
        COMMENT '모의투자 수수료율 (%)',
    ADD COLUMN paper_slippage_pct DECIMAL(8, 4) NOT NULL DEFAULT 0.0000
        COMMENT '모의투자 슬리피지 (%)',
    ADD COLUMN paper_min_order_krw DECIMAL(20, 2) NOT NULL DEFAULT 5000
        COMMENT '모의투자 최소 주문 금액',
    ADD COLUMN paper_auto_trade_enabled TINYINT(1) NOT NULL DEFAULT 1
        COMMENT '실시간 전략 시그널 시 자동 모의매매 (1=ON)',
    ADD COLUMN paper_auto_trade_budget_pct DECIMAL(8, 4) NOT NULL DEFAULT 95.0000
        COMMENT '자동 매수 시 가용 현금 사용 비율 (%)';

CREATE TABLE gc_paper_account (
    id              BIGINT         NOT NULL AUTO_INCREMENT,
    user_id         BIGINT         NULL,
    device_id       VARCHAR(100)   NOT NULL,
    cash_balance    DECIMAL(20, 2) NOT NULL DEFAULT 10000000,
    realized_pnl    DECIMAL(20, 2) NOT NULL DEFAULT 0,
    created_at      DATETIME       NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at      DATETIME       NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    UNIQUE KEY uk_paper_account_device (device_id),
    UNIQUE KEY uk_paper_account_user (user_id),
    INDEX idx_paper_account_device (device_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
  COMMENT='모의투자 계좌 (현금 잔고)';

CREATE TABLE gc_paper_position (
    id              BIGINT          NOT NULL AUTO_INCREMENT,
    account_id      BIGINT          NOT NULL,
    symbol          VARCHAR(50)     NOT NULL,
    korean_name     VARCHAR(100)    NULL,
    quantity        DECIMAL(30, 12) NOT NULL DEFAULT 0,
    avg_price       DECIMAL(20, 2)  NOT NULL DEFAULT 0,
    created_at      DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at      DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    UNIQUE KEY uk_paper_position_account_symbol (account_id, symbol),
    CONSTRAINT fk_paper_position_account FOREIGN KEY (account_id)
        REFERENCES gc_paper_account(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
  COMMENT='모의투자 보유 종목';

CREATE TABLE gc_paper_trade (
    id              BIGINT          NOT NULL AUTO_INCREMENT,
    account_id      BIGINT          NOT NULL,
    symbol          VARCHAR(50)     NOT NULL,
    side            VARCHAR(10)     NOT NULL COMMENT 'BUY | SELL',
    order_kind      VARCHAR(20)     NOT NULL DEFAULT 'limit' COMMENT 'limit | market',
    source          VARCHAR(20)     NOT NULL DEFAULT 'MANUAL' COMMENT 'MANUAL | STRATEGY',
    strategy_id     BIGINT          NULL,
    price           DECIMAL(20, 2)  NOT NULL,
    quantity        DECIMAL(30, 12) NOT NULL,
    gross_amount    DECIMAL(20, 2)  NOT NULL,
    fee_amount      DECIMAL(20, 2)  NOT NULL DEFAULT 0,
    net_amount      DECIMAL(20, 2)  NOT NULL,
    cash_after      DECIMAL(20, 2)  NOT NULL,
    created_at      DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    INDEX idx_paper_trade_account (account_id),
    INDEX idx_paper_trade_created (created_at),
    CONSTRAINT fk_paper_trade_account FOREIGN KEY (account_id)
        REFERENCES gc_paper_account(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
  COMMENT='모의투자 체결 이력';
