"""initial migration

Revision ID: 0001
Revises: 
Create Date: 2025-09-11 16:00:00.000000

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '0001'
down_revision = None
branch_labels = None
depends_on = None


def upgrade():
    # 创建用户表
    op.create_table(
        'users',
        sa.Column('id', sa.Integer, primary_key=True, index=True),
        sa.Column('username', sa.String(50), unique=True, index=True, nullable=False),
        sa.Column('password_hash', sa.String(255), nullable=False),
        sa.Column('user_type', sa.Enum('farmer', 'merchant', 'admin', 'super_admin'), server_default='farmer'),
        sa.Column('real_name', sa.String(50)),
        sa.Column('nickname', sa.String(50)),
        sa.Column('avatar_url', sa.String(255)),
        sa.Column('email', sa.String(100), unique=True, index=True),
        sa.Column('phone', sa.String(20), unique=True, index=True),
        sa.Column('gender', sa.Enum('male', 'female', 'other'), server_default='other'),
        sa.Column('birthday', sa.DateTime),
        sa.Column('status', sa.Enum('active', 'inactive'), server_default='active'),
        sa.Column('wechat_openid', sa.String(100), unique=True, index=True),
        sa.Column('wechat_unionid', sa.String(100), unique=True, index=True),
        sa.Column('level', sa.Integer, server_default='1'),
        sa.Column('created_at', sa.DateTime, server_default=sa.func.now()),
        sa.Column('updated_at', sa.DateTime, server_default=sa.func.now(), onupdate=sa.func.now()),
        sa.Column('last_login', sa.DateTime)
    )
    
    # 创建管理员表
    op.create_table(
        'admins',
        sa.Column('id', sa.Integer, primary_key=True, index=True),
        sa.Column('username', sa.String(50), unique=True, index=True, nullable=False),
        sa.Column('password', sa.String(255), nullable=False),
        sa.Column('email', sa.String(100), unique=True, index=True),
        sa.Column('nickname', sa.String(50)),
        sa.Column('avatar', sa.String(255)),
        sa.Column('role', sa.Enum('admin', 'super_admin'), server_default='admin'),
        sa.Column('status', sa.Enum('active', 'inactive'), server_default='active'),
        sa.Column('last_login', sa.DateTime),
        sa.Column('created_at', sa.DateTime, server_default=sa.func.now()),
        sa.Column('updated_at', sa.DateTime, server_default=sa.func.now(), onupdate=sa.func.now())
    )


def downgrade():
    op.drop_table('admins')
    op.drop_table('users')