"""
Migration script to add project settings fields to the projects table.
This adds support for upload location configuration and default task templates.
"""

import sqlite3
import json

def migrate_project_settings():
    """Add project settings fields to projects table"""
    conn = sqlite3.connect('vfx_project_management.db')
    cursor = conn.cursor()
    
    try:
        # Check if columns already exist
        cursor.execute("PRAGMA table_info(projects)")
        columns = [column[1] for column in cursor.fetchall()]
        
        # Add upload_data_location column if it doesn't exist
        if 'upload_data_location' not in columns:
            print("Adding upload_data_location column...")
            cursor.execute("""
                ALTER TABLE projects 
                ADD COLUMN upload_data_location TEXT
            """)
            print("✓ Added upload_data_location column")
        else:
            print("✓ upload_data_location column already exists")
        
        # Add asset_task_templates column if it doesn't exist
        if 'asset_task_templates' not in columns:
            print("Adding asset_task_templates column...")
            cursor.execute("""
                ALTER TABLE projects 
                ADD COLUMN asset_task_templates TEXT
            """)
            
            # Set default values for existing projects
            default_asset_templates = json.dumps({
                "characters": ["modeling", "surfacing", "rigging"],
                "props": ["modeling", "surfacing"],
                "sets": ["modeling", "surfacing"],
                "vehicles": ["modeling", "surfacing", "rigging"]
            })
            cursor.execute("""
                UPDATE projects 
                SET asset_task_templates = ?
                WHERE asset_task_templates IS NULL
            """, (default_asset_templates,))
            print("✓ Added asset_task_templates column with default values")
        else:
            print("✓ asset_task_templates column already exists")
        
        # Add shot_task_templates column if it doesn't exist
        if 'shot_task_templates' not in columns:
            print("Adding shot_task_templates column...")
            cursor.execute("""
                ALTER TABLE projects 
                ADD COLUMN shot_task_templates TEXT
            """)
            
            # Set default values for existing projects
            default_shot_templates = json.dumps([
                "layout", "animation", "simulation", "lighting", "compositing"
            ])
            cursor.execute("""
                UPDATE projects 
                SET shot_task_templates = ?
                WHERE shot_task_templates IS NULL
            """, (default_shot_templates,))
            print("✓ Added shot_task_templates column with default values")
        else:
            print("✓ shot_task_templates column already exists")
        
        # Add enabled_asset_tasks column if it doesn't exist
        if 'enabled_asset_tasks' not in columns:
            print("Adding enabled_asset_tasks column...")
            cursor.execute("""
                ALTER TABLE projects 
                ADD COLUMN enabled_asset_tasks TEXT
            """)
            print("✓ Added enabled_asset_tasks column")
        else:
            print("✓ enabled_asset_tasks column already exists")
        
        # Add enabled_shot_tasks column if it doesn't exist
        if 'enabled_shot_tasks' not in columns:
            print("Adding enabled_shot_tasks column...")
            cursor.execute("""
                ALTER TABLE projects 
                ADD COLUMN enabled_shot_tasks TEXT
            """)
            print("✓ Added enabled_shot_tasks column")
        else:
            print("✓ enabled_shot_tasks column already exists")
        
        conn.commit()
        print("\n✅ Migration completed successfully!")
        
    except Exception as e:
        conn.rollback()
        print(f"\n❌ Migration failed: {e}")
        raise
    finally:
        conn.close()

if __name__ == "__main__":
    print("Starting project settings migration...")
    print("=" * 50)
    migrate_project_settings()
