"""
Database migration script to add trading journal tables
Run this to create the new journal-related tables without affecting existing data
"""

import sys
import os

# Add the backend directory to the path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

from app.db.database import engine, Base
from app.models.models import (
    TradingPlan,
    ManualTrade,
    JournalEntry,
    DecisionLog,
    WeeklyPlan
)

def migrate_journal_tables():
    """Create journal tables if they don't exist"""
    print("Starting journal tables migration...")

    try:
        # Import all models to ensure they're registered with Base
        from app.models import models

        # Create only the new tables (won't affect existing tables)
        print("Creating journal tables...")
        Base.metadata.create_all(bind=engine, checkfirst=True)

        print("✅ Journal tables migration completed successfully!")
        print("\nNew tables created:")
        print("  - trading_plans")
        print("  - manual_trades")
        print("  - journal_entries")
        print("  - decision_log")
        print("  - weekly_plans")

    except Exception as e:
        print(f"❌ Migration failed: {e}")
        import traceback
        traceback.print_exc()
        sys.exit(1)


if __name__ == "__main__":
    migrate_journal_tables()
