#!/usr/bin/env python3
"""
Manual Duplicate Column Fix

This script provides step-by-step instructions to manually fix the duplicate
column error.

Usage:
    python manual_duplicate_fix.py
"""

def print_step(step_num, title, commands, description):
    """Print a step with instructions"""
    print("\n" + "="*60)
    print(f"STEP {step_num}: {title}")
    print("="*60)
    print(description)
    print("\nRun these commands:")
    for i, cmd in enumerate(commands, 1):
        print(f"{i}. {cmd}")
    print("\nPress Enter after completing all commands...")
    input()

def main():
    """Main function"""
    print("="*80)
    print("MANUAL DUPLICATE COLUMN FIX")
    print("="*80)
    print("This script will guide you through manually fixing the duplicate column error.")
    print("The error occurs because the 'unassigned_date' column already exists in the")
    print("portfolioassignment table, but the migration is trying to add it again.")
    
    # Step 1: Check migration status
    print_step(
        1,
        "CHECK MIGRATION STATUS",
        [
            "python manage.py showmigrations users"
        ],
        "First, let's see the current migration status to understand what's happening."
    )
    
    # Step 2: Check database tables
    print_step(
        2,
        "CHECK DATABASE TABLES",
        [
            "python manage.py shell",
            ">>> from django.db import connection",
            ">>> cursor = connection.cursor()",
            ">>> cursor.execute('DESCRIBE portfolioassignment')",
            ">>> print(cursor.fetchall())",
            ">>> cursor.execute('DESCRIBE user_permissions')",
            ">>> print(cursor.fetchall())",
            ">>> exit()"
        ],
        "Check which tables exist and what columns they have."
    )
    
    # Step 3: Mark merge migration as fake
    print_step(
        3,
        "MARK MERGE MIGRATION AS FAKE",
        [
            "python manage.py migrate users 0023 --fake"
        ],
        "Mark the merge migration as fake since the database changes already exist."
    )
    
    # Step 4: Apply remaining migrations
    print_step(
        4,
        "APPLY REMAINING MIGRATIONS",
        [
            "python manage.py migrate users"
        ],
        "Apply any remaining migrations that haven't been applied yet."
    )
    
    # Step 5: Verify the fix
    print_step(
        5,
        "VERIFY THE FIX",
        [
            "python manage.py shell",
            ">>> from django.db import connection",
            ">>> cursor = connection.cursor()",
            ">>> cursor.execute('SELECT COUNT(*) FROM user_permissions WHERE module = \\'test\\'')",
            ">>> print(cursor.fetchone())",
            ">>> exit()"
        ],
        "Verify that the user_permissions table is working correctly."
    )
    
    print("\n" + "="*80)
    print("MANUAL FIX COMPLETED!")
    print("="*80)
    print("If all steps were completed successfully, the duplicate column error")
    print("should be resolved and the user_permissions table should work correctly.")
    print("\nNext steps:")
    print("1. Test the user permissions page")
    print("2. Try checking/unchecking permissions")
    print("3. Verify navigation items show/hide correctly")

if __name__ == '__main__':
    main()
