#!/usr/bin/env python
"""
Deploy expenses ID fix to production
This script fixes:
1. Expense model ID field (UUID -> BigAutoField)
2. Branch dropdown showing all accessible branches
"""

import os
import sys

def main():
    print("=" * 70)
    print("EXPENSES ID FIX - PRODUCTION DEPLOYMENT")
    print("=" * 70)
    
    print("\n📋 CHANGES TO DEPLOY:")
    print("-" * 70)
    print("1. ✓ Updated Expense model: id field changed from UUIDField to BigAutoField")
    print("2. ✓ Updated ExpenseForm: improved branch filtering logic")
    print("3. ✓ Updated ExpenseFilterForm: added user-based branch filtering")
    print("4. ✓ Updated views: pass user to filter form")
    
    print("\n" + "=" * 70)
    print("DEPLOYMENT STEPS FOR CPANEL/PRODUCTION:")
    print("=" * 70)
    
    print("\n1️⃣  BACKUP DATABASE (IMPORTANT!)")
    print("-" * 70)
    print("   Run in phpMyAdmin or MySQL:")
    print("   CREATE TABLE expenses_backup AS SELECT * FROM expenses;")
    
    print("\n2️⃣  FIX DATABASE SCHEMA")
    print("-" * 70)
    print("   Run this SQL in phpMyAdmin:")
    print("""
   -- Check current id column type
   DESCRIBE expenses;
   
   -- If id is varchar/char, convert to bigint
   -- WARNING: This will delete existing expense records!
   TRUNCATE TABLE expenses;
   ALTER TABLE expenses MODIFY COLUMN id bigint NOT NULL AUTO_INCREMENT;
   
   -- Verify the change
   DESCRIBE expenses;
   """)
    
    print("\n3️⃣  UPLOAD UPDATED FILES")
    print("-" * 70)
    print("   Upload these files to production:")
    print("   - expenses/models.py")
    print("   - expenses/forms.py")
    print("   - expenses/views.py")
    
    print("\n4️⃣  RESTART APPLICATION")
    print("-" * 70)
    print("   In cPanel:")
    print("   - Go to 'Setup Python App'")
    print("   - Click 'Restart' button")
    print("   OR")
    print("   - Touch passenger_wsgi.py: touch ~/public_html/branchbusinessadvance.co.ke/passenger_wsgi.py")
    
    print("\n5️⃣  TEST THE FIX")
    print("-" * 70)
    print("   1. Login to the system")
    print("   2. Go to Expenses > Add Expense")
    print("   3. Check that:")
    print("      - Branch dropdown shows all your accessible branches")
    print("      - You can successfully create an expense")
    print("      - No DataError about 'Incorrect integer value'")
    
    print("\n" + "=" * 70)
    print("ALTERNATIVE: RUN FIX SCRIPT ON PRODUCTION")
    print("=" * 70)
    print("\nIf you have SSH access, you can run:")
    print("   cd ~/public_html/branchbusinessadvance.co.ke")
    print("   source virtualenv/bin/activate")
    print("   python fix_expenses_production.py")
    print("   # Then upload the updated model files")
    
    print("\n" + "=" * 70)
    print("WHAT WAS FIXED:")
    print("=" * 70)
    print("""
Issue 1: DataError - Incorrect integer value for column 'id'
  Cause: Model had UUIDField but database had bigint
  Fix: Changed model to use BigAutoField to match database

Issue 2: Branch dropdown only showing "main branch"
  Cause: Form wasn't properly filtering branches for users
  Fix: Added proper user-based branch filtering in form __init__
    """)
    
    print("\n" + "=" * 70)
    print("✅ DEPLOYMENT GUIDE COMPLETE")
    print("=" * 70)
    print("\nFollow the steps above to deploy the fix to production.")
    print("If you encounter any issues, check the error logs in cPanel.")
    print("=" * 70)


if __name__ == '__main__':
    main()
