from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

revision = "10b73fee8967"
down_revision = "13d050e8cf6d"
branch_labels = None
depends_on = None

def upgrade() -> None:
    # 1. Régi táblák eltávolítása kényszerítve
    tables_to_drop = [
        "user_badges", "badges", "points_ledger", "votes", "user_scores", 
        "user_stats", "competitions", "level_configs", "point_rules", 
        "regional_settings", "translations", "system_settings", "vehicles"
    ]
    for table in tables_to_drop:
        op.execute(f"DROP TABLE IF EXISTS data.{table} CASCADE")

    # 2. Szerepkör oszlop típusának módosítása
    op.execute("ALTER TABLE data.organization_members ALTER COLUMN role TYPE orguserrole USING role::text::orguserrole")

    # 3. Új oszlopok hozzáadása (Szigorúan Sima Idézőjellel a DEFAULT-nál!)
    op.execute("ALTER TABLE data.users ADD COLUMN IF NOT EXISTS hashed_password VARCHAR")
    op.execute("ALTER TABLE data.users ADD COLUMN IF NOT EXISTS role VARCHAR DEFAULT \u0027user\u0027")
    op.execute("ALTER TABLE data.users ADD COLUMN IF NOT EXISTS is_superuser BOOLEAN DEFAULT FALSE")
    op.execute("ALTER TABLE data.users ADD COLUMN IF NOT EXISTS is_company BOOLEAN DEFAULT FALSE")
    op.execute("ALTER TABLE data.users ADD COLUMN IF NOT EXISTS updated_at TIMESTAMP WITH TIME ZONE")
    
    op.execute("ALTER TABLE data.organizations ADD COLUMN IF NOT EXISTS theme VARCHAR DEFAULT \u0027system\u0027")
    op.execute("ALTER TABLE data.organizations ADD COLUMN IF NOT EXISTS is_active BOOLEAN DEFAULT TRUE")

    # 4. Takarítás
    op.execute("ALTER TABLE data.users DROP COLUMN IF EXISTS password_hash")
    op.execute("ALTER TABLE data.users DROP COLUMN IF EXISTS is_email_verified")

def downgrade() -> None:
    pass
