"""
Database Migration: Scene Slot Bundle Tables.
[AC-SCENE-SLOT-01] 场景-槽位映射配置表迁移

创建时间: 2025-03-07
变更说明:
- 新增 scene_slot_bundles 表用于存储场景槽位包配置

执行方式:
- SQLModel 会自动创建表（通过 init_db）
- 此脚本用于手动迁移或回滚

SQL DDL:
```sql
CREATE TABLE scene_slot_bundles (
    id UUID PRIMARY KEY,
    tenant_id VARCHAR NOT NULL,
    scene_key VARCHAR(100) NOT NULL,
    scene_name VARCHAR(100) NOT NULL,
    description TEXT,
    required_slots JSON NOT NULL DEFAULT '[]',
    optional_slots JSON NOT NULL DEFAULT '[]',
    slot_priority JSON,
    completion_threshold FLOAT NOT NULL DEFAULT 1.0,
    ask_back_order VARCHAR NOT NULL DEFAULT 'priority',
    status VARCHAR NOT NULL DEFAULT 'draft',
    version INTEGER NOT NULL DEFAULT 1,
    created_at TIMESTAMP NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMP NOT NULL DEFAULT NOW()
);

CREATE INDEX ix_scene_slot_bundles_tenant ON scene_slot_bundles(tenant_id);
CREATE UNIQUE INDEX ix_scene_slot_bundles_tenant_scene ON scene_slot_bundles(tenant_id, scene_key);
CREATE INDEX ix_scene_slot_bundles_tenant_status ON scene_slot_bundles(tenant_id, status);
```

回滚 SQL:
```sql
DROP TABLE IF EXISTS scene_slot_bundles;
```
"""

SCENE_SLOT_BUNDLES_DDL = """
CREATE TABLE IF NOT EXISTS scene_slot_bundles (
    id UUID PRIMARY KEY,
    tenant_id VARCHAR NOT NULL,
    scene_key VARCHAR(100) NOT NULL,
    scene_name VARCHAR(100) NOT NULL,
    description TEXT,
    required_slots JSON NOT NULL DEFAULT '[]',
    optional_slots JSON NOT NULL DEFAULT '[]',
    slot_priority JSON,
    completion_threshold FLOAT NOT NULL DEFAULT 1.0,
    ask_back_order VARCHAR NOT NULL DEFAULT 'priority',
    status VARCHAR NOT NULL DEFAULT 'draft',
    version INTEGER NOT NULL DEFAULT 1,
    created_at TIMESTAMP NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMP NOT NULL DEFAULT NOW()
);
"""

SCENE_SLOT_BUNDLES_INDEXES = """
CREATE INDEX IF NOT EXISTS ix_scene_slot_bundles_tenant ON scene_slot_bundles(tenant_id);
CREATE UNIQUE INDEX IF NOT EXISTS ix_scene_slot_bundles_tenant_scene ON scene_slot_bundles(tenant_id, scene_key);
CREATE INDEX IF NOT EXISTS ix_scene_slot_bundles_tenant_status ON scene_slot_bundles(tenant_id, status);
"""

SCENE_SLOT_BUNDLES_ROLLBACK = """
DROP TABLE IF EXISTS scene_slot_bundles;
"""


async def upgrade(conn):
    """执行迁移"""
    await conn.execute(SCENE_SLOT_BUNDLES_DDL)
    await conn.execute(SCENE_SLOT_BUNDLES_INDEXES)


async def downgrade(conn):
    """回滚迁移"""
    await conn.execute(SCENE_SLOT_BUNDLES_ROLLBACK)
