""" Demo data seeder script. Creates a sample account and imports the provided CSV file. """ import sys from pathlib import Path # Add parent directory to path sys.path.insert(0, str(Path(__file__).parent)) from sqlalchemy.orm import Session from app.database import SessionLocal, engine, Base from app.models import Account from app.services import ImportService from app.services.position_tracker import PositionTracker def seed_demo_data(): """Seed demo account and transactions.""" print("🌱 Seeding demo data...") # Create tables Base.metadata.create_all(bind=engine) # Create database session db = SessionLocal() try: # Check if demo account already exists existing = ( db.query(Account) .filter(Account.account_number == "DEMO123456") .first() ) if existing: print("āœ… Demo account already exists") demo_account = existing else: # Create demo account demo_account = Account( account_number="DEMO123456", account_name="Demo Trading Account", account_type="margin", ) db.add(demo_account) db.commit() db.refresh(demo_account) print(f"āœ… Created demo account (ID: {demo_account.id})") # Check for CSV file csv_path = Path("/app/imports/History_for_Account_X38661988.csv") if not csv_path.exists(): # Try alternative path (development) csv_path = Path(__file__).parent.parent / "History_for_Account_X38661988.csv" if not csv_path.exists(): print("āš ļø Sample CSV file not found. Skipping import.") print(" Place the CSV file in /app/imports/ to seed demo data.") return # Import transactions print(f"šŸ“Š Importing transactions from {csv_path.name}...") import_service = ImportService(db) result = import_service.import_from_file(csv_path, demo_account.id) print(f"āœ… Imported {result.imported} transactions") print(f" Skipped {result.skipped} duplicates") if result.errors: print(f" āš ļø {len(result.errors)} errors occurred") # Build positions if result.imported > 0: print("šŸ“ˆ Building positions...") position_tracker = PositionTracker(db) positions_created = position_tracker.rebuild_positions(demo_account.id) print(f"āœ… Created {positions_created} positions") print("\nšŸŽ‰ Demo data seeded successfully!") print(f"\nšŸ“ Demo Account Details:") print(f" Account Number: {demo_account.account_number}") print(f" Account Name: {demo_account.account_name}") print(f" Account ID: {demo_account.id}") except Exception as e: print(f"āŒ Error seeding demo data: {e}") db.rollback() raise finally: db.close() if __name__ == "__main__": seed_demo_data()