-- Stock Monitor Database Schema
-- Database: stock_monitor

-- 1. Stocks table
CREATE TABLE IF NOT EXISTS stocks (
    id INT AUTO_INCREMENT PRIMARY KEY,
    stock_code VARCHAR(10) NOT NULL UNIQUE,
    stock_name VARCHAR(50) NOT NULL,
    market VARCHAR(10) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_stock_code (stock_code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 2. Stock data table
CREATE TABLE IF NOT EXISTS stock_data (
    id INT AUTO_INCREMENT PRIMARY KEY,
    stock_code VARCHAR(10) NOT NULL,
    data_date DATE NOT NULL,

    -- Basic info
    price DECIMAL(10,3) DEFAULT NULL,
    change_percent DECIMAL(8,4) DEFAULT NULL,
    market_value DECIMAL(12,3) DEFAULT NULL,

    -- Valuation metrics
    pe_ratio DECIMAL(8,4) DEFAULT NULL,
    pb_ratio DECIMAL(8,4) DEFAULT NULL,
    ps_ratio DECIMAL(8,4) DEFAULT NULL,
    dividend_yield DECIMAL(8,4) DEFAULT NULL,

    -- Financial metrics
    roe DECIMAL(8,4) DEFAULT NULL,
    gross_profit_margin DECIMAL(8,4) DEFAULT NULL,
    net_profit_margin DECIMAL(8,4) DEFAULT NULL,
    debt_to_assets DECIMAL(8,4) DEFAULT NULL,
    revenue_yoy DECIMAL(8,4) DEFAULT NULL,
    net_profit_yoy DECIMAL(8,4) DEFAULT NULL,
    bps DECIMAL(8,4) DEFAULT NULL,
    ocfps DECIMAL(8,4) DEFAULT NULL,

    -- Metadata
    from_cache BOOLEAN DEFAULT FALSE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

    UNIQUE KEY uk_stock_date (stock_code, data_date),
    INDEX idx_stock_code (stock_code),
    INDEX idx_data_date (data_date),
    FOREIGN KEY (stock_code) REFERENCES stocks(stock_code) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 3. Watchlist table
CREATE TABLE IF NOT EXISTS watchlist (
    id INT AUTO_INCREMENT PRIMARY KEY,
    stock_code VARCHAR(10) NOT NULL,
    target_market_value_min DECIMAL(12,3) DEFAULT NULL,
    target_market_value_max DECIMAL(12,3) DEFAULT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

    UNIQUE KEY uk_stock_code (stock_code),
    FOREIGN KEY (stock_code) REFERENCES stocks(stock_code) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 4. AI Analysis table
CREATE TABLE IF NOT EXISTS ai_analysis (
    id INT AUTO_INCREMENT PRIMARY KEY,
    stock_code VARCHAR(10) NOT NULL,
    analysis_type VARCHAR(20) NOT NULL,
    analysis_date DATE NOT NULL,

    -- Investment suggestions
    investment_summary TEXT,
    investment_action TEXT,
    investment_key_points JSON,

    -- Detailed analysis
    valuation_analysis TEXT,
    financial_analysis TEXT,
    growth_analysis TEXT,
    risk_analysis TEXT,

    -- Price analysis
    reasonable_price_min DECIMAL(10,3) DEFAULT NULL,
    reasonable_price_max DECIMAL(10,3) DEFAULT NULL,
    target_market_value_min DECIMAL(12,3) DEFAULT NULL,
    target_market_value_max DECIMAL(12,3) DEFAULT NULL,

    -- Metadata
    from_cache BOOLEAN DEFAULT FALSE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

    UNIQUE KEY uk_stock_type_date (stock_code, analysis_type, analysis_date),
    INDEX idx_stock_code (stock_code),
    INDEX idx_analysis_type (analysis_type),
    INDEX idx_analysis_date (analysis_date),
    FOREIGN KEY (stock_code) REFERENCES stocks(stock_code) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 5. System config table
CREATE TABLE IF NOT EXISTS system_config (
    id INT AUTO_INCREMENT PRIMARY KEY,
    config_key VARCHAR(50) NOT NULL UNIQUE,
    config_value TEXT,
    config_type VARCHAR(20) DEFAULT 'string',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

    INDEX idx_config_key (config_key)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 6. Data update log table
CREATE TABLE IF NOT EXISTS data_update_log (
    id INT AUTO_INCREMENT PRIMARY KEY,
    data_type VARCHAR(20) NOT NULL,
    stock_code VARCHAR(10) DEFAULT NULL,
    update_status ENUM('success', 'failed', 'partial') NOT NULL,
    update_message TEXT,
    execution_time DECIMAL(8,3) DEFAULT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

    INDEX idx_data_type (data_type),
    INDEX idx_stock_code (stock_code),
    INDEX idx_update_status (update_status),
    INDEX idx_created_at (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;