"""add report tables

Revision ID: c3d4e5f6g7h8
Revises: b2c3d4e5f6g7
Create Date: 2026-05-22 10:00:00.000000

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa

revision: str = 'c3d4e5f6g7h8'
down_revision: Union[str, None] = 'b2c3d4e5f6g7'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None

_BASE_COLS = [
    sa.Column('id', sa.String(length=21), nullable=False, comment='主键ID(NanoId)'),
    sa.Column('sort', sa.Integer(), nullable=True, comment='排序'),
    sa.Column('is_deleted', sa.Boolean(), nullable=True, comment='是否删除'),
    sa.Column('sys_create_datetime', sa.DateTime(), server_default=sa.text('now()'), nullable=True, comment='创建时间'),
    sa.Column('sys_update_datetime', sa.DateTime(), server_default=sa.text('now()'), nullable=True, comment='更新时间'),
    sa.Column('sys_creator_id', sa.String(length=21), nullable=True, comment='创建人ID'),
    sa.Column('sys_modifier_id', sa.String(length=21), nullable=True, comment='修改人ID'),
    sa.Column('sys_dept_id', sa.String(length=21), nullable=True, comment='部门ID'),
]


def upgrade() -> None:
    op.create_table(
        'report_template',
        sa.Column('application_id', sa.String(length=21), nullable=True, comment='所属应用ID'),
        sa.Column('name', sa.String(length=100), nullable=False, comment='报表名称'),
        sa.Column('code', sa.String(length=100), nullable=False, comment='报表编码'),
        sa.Column('category', sa.String(length=50), nullable=True, comment='分类'),
        sa.Column('description', sa.Text(), nullable=True, comment='描述'),
        sa.Column('status', sa.String(length=20), nullable=True, server_default='draft', comment='状态'),
        sa.Column('allow_export', sa.Boolean(), nullable=True, server_default='true', comment='允许导出'),
        sa.Column('allow_print', sa.Boolean(), nullable=True, server_default='true', comment='允许打印'),
        sa.Column('allow_watermark', sa.Boolean(), nullable=True, server_default='false', comment='允许水印'),
        sa.Column('watermark_config', sa.JSON(), nullable=True, comment='水印配置'),
        *_BASE_COLS,
        sa.PrimaryKeyConstraint('id'),
        sa.UniqueConstraint('code'),
    )
    op.create_index('ix_report_template_application_id', 'report_template', ['application_id'])
    op.create_index('ix_report_template_status', 'report_template', ['status'])
    op.create_index('ix_report_template_is_deleted', 'report_template', ['is_deleted'])

    op.create_table(
        'report_version',
        sa.Column('template_id', sa.String(length=21), nullable=False, comment='模板ID'),
        sa.Column('version', sa.Integer(), nullable=True, server_default='1', comment='版本号'),
        sa.Column('state', sa.SmallInteger(), nullable=True, server_default='0', comment='版本状态'),
        sa.Column('snapshot', sa.JSON(), nullable=True, comment='Univer snapshot'),
        sa.Column('cells', sa.JSON(), nullable=True, comment='单元格绑定'),
        sa.Column('query_list', sa.JSON(), nullable=True, comment='查询条件'),
        sa.Column('sort_list', sa.JSON(), nullable=True, comment='排序'),
        sa.Column('column_list', sa.JSON(), nullable=True, comment='分栏'),
        sa.Column('fence_list', sa.JSON(), nullable=True, comment='围栏'),
        sa.Column('convert_config', sa.JSON(), nullable=True, comment='转换配置'),
        *_BASE_COLS,
        sa.PrimaryKeyConstraint('id'),
    )
    op.create_index('ix_report_version_template_id', 'report_version', ['template_id'])
    op.create_index('ix_report_version_state', 'report_version', ['state'])
    op.create_index('ix_report_version_is_deleted', 'report_version', ['is_deleted'])

    op.create_table(
        'report_dataset',
        sa.Column('version_id', sa.String(length=21), nullable=False, comment='版本ID'),
        sa.Column('data_source_id', sa.String(length=21), nullable=False, comment='数据源ID'),
        sa.Column('alias', sa.String(length=100), nullable=False, comment='别名'),
        sa.Column('field_mapping', sa.JSON(), nullable=True, comment='字段映射'),
        sa.Column('convert_config', sa.JSON(), nullable=True, comment='转换配置'),
        *_BASE_COLS,
        sa.PrimaryKeyConstraint('id'),
    )
    op.create_index('ix_report_dataset_version_id', 'report_dataset', ['version_id'])
    op.create_index('ix_report_dataset_data_source_id', 'report_dataset', ['data_source_id'])


def downgrade() -> None:
    op.drop_index('ix_report_dataset_data_source_id', table_name='report_dataset')
    op.drop_index('ix_report_dataset_version_id', table_name='report_dataset')
    op.drop_table('report_dataset')
    op.drop_index('ix_report_version_is_deleted', table_name='report_version')
    op.drop_index('ix_report_version_state', table_name='report_version')
    op.drop_index('ix_report_version_template_id', table_name='report_version')
    op.drop_table('report_version')
    op.drop_index('ix_report_template_is_deleted', table_name='report_template')
    op.drop_index('ix_report_template_status', table_name='report_template')
    op.drop_index('ix_report_template_application_id', table_name='report_template')
    op.drop_table('report_template')
