-- Migration: Model Pack support (方案 B)
-- Run: mysql -u root -p aiworker < schema_migrate_model_packs.sql

USE aiworker;

CREATE TABLE IF NOT EXISTS model_packs (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  model_id VARCHAR(64) NOT NULL,
  version VARCHAR(32) NOT NULL,
  package_path VARCHAR(512) NOT NULL,
  package_format ENUM('zip','gguf') NOT NULL DEFAULT 'gguf',
  file_size BIGINT UNSIGNED NOT NULL DEFAULT 0,
  sha256 CHAR(64) NOT NULL,
  signature TEXT DEFAULT NULL,
  status ENUM('active','disabled') NOT NULL DEFAULT 'active',
  created_at DATETIME NOT NULL,
  PRIMARY KEY (id),
  UNIQUE KEY uk_model_version (model_id, version)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

SET @col_exists = (
  SELECT COUNT(*) FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = 'aiworker' AND TABLE_NAME = 'tasks' AND COLUMN_NAME = 'required_models_json'
);
SET @sql = IF(
  @col_exists = 0,
  'ALTER TABLE tasks ADD COLUMN required_models_json JSON DEFAULT NULL AFTER required_tools_json',
  'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
