CREATE DATABASE IF NOT EXISTS thermal_control_system
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;

USE thermal_control_system;

CREATE TABLE IF NOT EXISTS categories (
    id BIGINT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100) NOT NULL COMMENT '分类名称',
    type ENUM('data_file', 'data_package') NOT NULL COMMENT '分类类型',
    parent_id BIGINT DEFAULT NULL COMMENT '父分类ID',
    sort_order INT NOT NULL DEFAULT 0 COMMENT '排序',
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_type (type),
    INDEX idx_parent (parent_id)
) COMMENT='数据分类表';

CREATE TABLE IF NOT EXISTS data_files (
    id BIGINT PRIMARY KEY AUTO_INCREMENT,
    filename VARCHAR(255) NOT NULL COMMENT '原始文件名',
    stored_name VARCHAR(255) NOT NULL UNIQUE COMMENT '存储文件名',
    file_path VARCHAR(500) NOT NULL COMMENT '文件路径',
    category_id BIGINT DEFAULT NULL COMMENT '分类ID',
    data_count INT NOT NULL DEFAULT 0 COMMENT '数据条数',
    uploaded_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    remark TEXT NULL COMMENT '备注',
    INDEX idx_file_category (category_id)
) COMMENT='数据文件表';

ALTER TABLE data_files
    ADD COLUMN IF NOT EXISTS file_path VARCHAR(500) NOT NULL COMMENT '文件路径';

CREATE TABLE IF NOT EXISTS data_packages (
    id BIGINT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(255) NOT NULL COMMENT '数据包名称',
    category_id BIGINT DEFAULT NULL COMMENT '分类ID(data_package类型)',
    remark TEXT NULL COMMENT '备注',
    data_count INT NOT NULL DEFAULT 0 COMMENT '数据条数(所有关联文件合计)',
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_pkg_category (category_id)
) COMMENT='数据包表';

ALTER TABLE data_packages
    ADD COLUMN IF NOT EXISTS clean_rules JSON NULL COMMENT '野值清洗规则';

ALTER TABLE data_packages
    ADD COLUMN IF NOT EXISTS row_start INT NULL COMMENT '数据行起始行号(1-based, 含)';

ALTER TABLE data_packages
    ADD COLUMN IF NOT EXISTS row_end INT NULL COMMENT '数据行结束行号(1-based, 含)';

CREATE TABLE IF NOT EXISTS data_package_files (
    id BIGINT PRIMARY KEY AUTO_INCREMENT,
    package_id BIGINT NOT NULL COMMENT '数据包ID',
    file_id BIGINT NOT NULL COMMENT '数据文件ID',
    sort_order INT NOT NULL DEFAULT 0 COMMENT '排序',
    INDEX idx_dpf_package (package_id),
    INDEX idx_dpf_file (file_id)
) COMMENT='数据包文件关联表';

ALTER TABLE train_tasks
    ADD COLUMN IF NOT EXISTS test_loss FLOAT NULL COMMENT '测试集损失';

ALTER TABLE saved_models
    ADD COLUMN IF NOT EXISTS test_loss FLOAT NULL COMMENT '测试集损失';

ALTER TABLE saved_models
    ADD COLUMN IF NOT EXISTS description TEXT NULL COMMENT '模型说明';

CREATE TABLE IF NOT EXISTS data_quality_config (
    id BIGINT PRIMARY KEY AUTO_INCREMENT,
    field_name VARCHAR(50) NOT NULL COMMENT '字段名: current/voltage/set_temperature/actual_temperature',
    label VARCHAR(100) NOT NULL COMMENT '显示名称',
    unit VARCHAR(20) NOT NULL DEFAULT '' COMMENT '单位',
    min_value DOUBLE DEFAULT NULL COMMENT '合规最小值',
    max_value DOUBLE DEFAULT NULL COMMENT '合规最大值',
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY uk_field_name (field_name)
) COMMENT='数据质量准确性配置表';

INSERT IGNORE INTO data_quality_config (field_name, label, unit) VALUES
('current', '电流', 'A'),
('voltage', '电压', 'V'),
('set_temperature', '设定温度', '℃'),
('actual_temperature', '实际温度', '℃');

ALTER TABLE eval_records
    ADD COLUMN IF NOT EXISTS mse FLOAT NULL COMMENT '均方误差';

ALTER TABLE eval_records
    ADD COLUMN IF NOT EXISTS mape FLOAT NULL COMMENT '平均绝对百分比误差(%)';

ALTER TABLE eval_records
    ADD COLUMN IF NOT EXISTS r2 FLOAT NULL COMMENT '决定系数';

ALTER TABLE monitor_experiments
    ADD COLUMN IF NOT EXISTS input_csv_path TEXT NULL COMMENT '输入CSV路径(传感器数据源)';

ALTER TABLE monitor_experiments
    ADD COLUMN IF NOT EXISTS output_csv_path TEXT NULL COMMENT '输出CSV路径(生成曲线写入)';

ALTER TABLE monitor_experiments
    MODIFY COLUMN package_id BIGINT NULL COMMENT '初始数据包ID(旧版兼容)';

ALTER TABLE monitor_experiments
    MODIFY COLUMN package_name VARCHAR(255) NULL COMMENT '数据包名称(旧版兼容)';

