"""
添加表单分享相关表

Revision ID: 20250412_0005
Revises: 20250409_0004
Create Date: 2025-04-12
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql

# revision identifiers, used by Alembic.
revision = '20250412_0005'
down_revision = '20250409_0004'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    
    # 创建 form_shares 表
    op.create_table('form_shares',
        sa.Column('id', sa.String(36), nullable=False),
        sa.Column('table_id', sa.String(36), nullable=False),
        sa.Column('share_token', sa.String(64), nullable=False),
        sa.Column('created_by', sa.String(36), nullable=False),
        sa.Column('is_active', sa.Boolean(), nullable=False, default=True),
        sa.Column('allow_anonymous', sa.Boolean(), nullable=False, default=True),
        sa.Column('require_captcha', sa.Boolean(), nullable=False, default=False),
        sa.Column('expires_at', sa.Integer(), nullable=True),
        sa.Column('max_submissions', sa.Integer(), nullable=True),
        sa.Column('current_submissions', sa.Integer(), nullable=False, default=0),
        sa.Column('allowed_fields', sa.Text(), nullable=True, comment='JSON格式的字段ID列表，为空表示允许所有字段'),
        sa.Column('title', sa.String(200), nullable=True),
        sa.Column('description', sa.Text(), nullable=True),
        sa.Column('submit_button_text', sa.String(50), nullable=True, default='提交'),
        sa.Column('success_message', sa.String(500), nullable=True, default='提交成功，感谢您的参与！'),
        sa.Column('theme', sa.String(50), nullable=True, default='default'),
        sa.Column('created_at', sa.DateTime(), nullable=False),
        sa.Column('updated_at', sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(['table_id'], ['tables.id'], ondelete='CASCADE'),
        sa.ForeignKeyConstraint(['created_by'], ['users.id'], ondelete='CASCADE'),
        sa.PrimaryKeyConstraint('id'),
        sa.UniqueConstraint('share_token')
    )
    
    # 创建索引
    op.create_index('ix_form_shares_table_id', 'form_shares', ['table_id'])
    op.create_index('ix_form_shares_share_token', 'form_shares', ['share_token'])
    op.create_index('ix_form_shares_created_by', 'form_shares', ['created_by'])
    
    # 创建 form_submissions 表
    op.create_table('form_submissions',
        sa.Column('id', sa.String(36), nullable=False),
        sa.Column('form_share_id', sa.String(36), nullable=False),
        sa.Column('record_id', sa.String(36), nullable=True),
        sa.Column('submitter_ip', sa.String(45), nullable=True, comment='支持 IPv6 地址'),
        sa.Column('submitter_user_agent', sa.Text(), nullable=True),
        sa.Column('submitter_info', sa.Text(), nullable=True, comment='JSON格式，包含邮箱、姓名等提交者信息'),
        sa.Column('submitted_at', sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(['form_share_id'], ['form_shares.id'], ondelete='CASCADE'),
        sa.ForeignKeyConstraint(['record_id'], ['records.id'], ondelete='SET NULL'),
        sa.PrimaryKeyConstraint('id')
    )
    
    # 创建索引
    op.create_index('ix_form_submissions_form_share_id', 'form_submissions', ['form_share_id'])
    op.create_index('ix_form_submissions_record_id', 'form_submissions', ['record_id'])
    op.create_index('ix_form_submissions_submitted_at', 'form_submissions', ['submitted_at'])
    
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    
    # 删除索引
    op.drop_index('ix_form_submissions_submitted_at', table_name='form_submissions')
    op.drop_index('ix_form_submissions_record_id', table_name='form_submissions')
    op.drop_index('ix_form_submissions_form_share_id', table_name='form_submissions')
    
    op.drop_index('ix_form_shares_created_by', table_name='form_shares')
    op.drop_index('ix_form_shares_share_token', table_name='form_shares')
    op.drop_index('ix_form_shares_table_id', table_name='form_shares')
    
    # 删除表
    op.drop_table('form_submissions')
    op.drop_table('form_shares')
    
    # ### end Alembic commands ###
