-- Phase 7: 素材管理相关表
-- 创建时间: 2026-06-17

-- 素材库表
CREATE TABLE IF NOT EXISTS asset_libraries (
    id VARCHAR(36) PRIMARY KEY,
    workspace_id VARCHAR(36) NOT NULL,
    project_id VARCHAR(36),
    name VARCHAR(255) NOT NULL,
    kind VARCHAR(20) NOT NULL CHECK (kind IN ('video', 'voice', 'image')),
    asset_count INTEGER DEFAULT 0,
    total_size BIGINT DEFAULT 0,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    
    CONSTRAINT fk_asset_libraries_workspace FOREIGN KEY (workspace_id) REFERENCES workspaces(id) ON DELETE CASCADE,
    CONSTRAINT fk_asset_libraries_project FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE
);

CREATE INDEX idx_asset_libraries_workspace ON asset_libraries(workspace_id);
CREATE INDEX idx_asset_libraries_project ON asset_libraries(project_id);
CREATE INDEX idx_asset_libraries_kind ON asset_libraries(kind);

-- 素材表
CREATE TABLE IF NOT EXISTS assets (
    id VARCHAR(36) PRIMARY KEY,
    workspace_id VARCHAR(36) NOT NULL,
    project_id VARCHAR(36) NOT NULL,
    asset_library_id VARCHAR(36) NOT NULL,
    
    -- 文件信息
    name VARCHAR(500) NOT NULL,
    file_type VARCHAR(20) NOT NULL CHECK (file_type IN ('video', 'image', 'audio')),
    file_size BIGINT NOT NULL,
    file_url VARCHAR(1000) NOT NULL,
    thumbnail_url VARCHAR(1000),
    
    -- 视频元数据
    duration FLOAT,
    width INTEGER,
    height INTEGER,
    fps FLOAT,
    codec VARCHAR(50),
    
    -- 状态
    status VARCHAR(20) NOT NULL DEFAULT 'uploading' CHECK (status IN ('uploading', 'ready', 'processing', 'error')),
    
    -- 分类信息
    classification_status VARCHAR(20) NOT NULL DEFAULT 'pending' CHECK (classification_status IN ('pending', 'processing', 'completed', 'failed')),
    classification_result JSONB,
    quality_score FLOAT,
    
    -- 审计
    uploaded_by_user_id VARCHAR(36) NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    
    CONSTRAINT fk_assets_workspace FOREIGN KEY (workspace_id) REFERENCES workspaces(id) ON DELETE CASCADE,
    CONSTRAINT fk_assets_project FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE,
    CONSTRAINT fk_assets_library FOREIGN KEY (asset_library_id) REFERENCES asset_libraries(id) ON DELETE CASCADE,
    CONSTRAINT fk_assets_user FOREIGN KEY (uploaded_by_user_id) REFERENCES users(id) ON DELETE CASCADE
);

CREATE INDEX idx_assets_workspace ON assets(workspace_id);
CREATE INDEX idx_assets_project ON assets(project_id);
CREATE INDEX idx_assets_library ON assets(asset_library_id);
CREATE INDEX idx_assets_type ON assets(file_type);
CREATE INDEX idx_assets_status ON assets(status);
CREATE INDEX idx_assets_classification_status ON assets(classification_status);
CREATE INDEX idx_assets_created_at ON assets(created_at DESC);

-- 添加注释
COMMENT ON TABLE asset_libraries IS '素材库表';
COMMENT ON TABLE assets IS '素材表';
COMMENT ON COLUMN assets.classification_result IS '分类结果 JSON，包含场景标签、质量评分等';

CREATE TABLE IF NOT EXISTS classification_jobs (
    id VARCHAR(32) PRIMARY KEY,
    workspace_id VARCHAR(32) NOT NULL,
    project_id VARCHAR(32) NOT NULL,
    asset_id VARCHAR(32) NOT NULL,
    status VARCHAR(20) NOT NULL DEFAULT 'pending',
    classification VARCHAR(50) NOT NULL DEFAULT '',
    confidence FLOAT NOT NULL DEFAULT 0,
    error_message TEXT NOT NULL DEFAULT '',
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX idx_classification_jobs_workspace ON classification_jobs(workspace_id);
CREATE INDEX idx_classification_jobs_project ON classification_jobs(project_id);
CREATE INDEX idx_classification_jobs_asset ON classification_jobs(asset_id);
CREATE INDEX idx_classification_jobs_status ON classification_jobs(status);

