"""
Migration: Add industry column to projects table
Date: 2025-10-23
"""

import os
import sys
from pathlib import Path

# Add parent directory to path to import app modules
sys.path.insert(0, str(Path(__file__).parent.parent))

from sqlalchemy import create_engine, text
from app.db.db import DATABASE_URL, engine


def upgrade():
    """Add industry column to projects table"""
    print("Running migration: Add industry column to projects table")
    
    with engine.connect() as conn:
        # Check if column already exists
        result = conn.execute(text("PRAGMA table_info(projects)"))
        columns = [row[1] for row in result]
        
        if 'industry' in columns:
            print("Column 'industry' already exists in projects table. Skipping migration.")
            return
        
        # Add the industry column
        conn.execute(text("ALTER TABLE projects ADD COLUMN industry VARCHAR"))
        conn.commit()
        
        print("Successfully added 'industry' column to projects table")


def downgrade():
    """Remove industry column from projects table"""
    print("Running downgrade: Remove industry column from projects table")
    
    # Note: SQLite doesn't support DROP COLUMN directly
    # This is a simplified version - in production you'd need to recreate the table
    print("Warning: SQLite doesn't support DROP COLUMN.")
    print("To remove the column, you would need to:")
    print("1. Create a new table without the industry column")
    print("2. Copy data from old table to new table")
    print("3. Drop old table and rename new table")


if __name__ == "__main__":
    import argparse
    
    parser = argparse.ArgumentParser(description="Run database migration")
    parser.add_argument(
        "direction",
        choices=["upgrade", "downgrade"],
        default="upgrade",
        nargs="?",
        help="Migration direction (default: upgrade)"
    )
    
    args = parser.parse_args()
    
    if args.direction == "upgrade":
        upgrade()
    else:
        downgrade()
