import sqlite3, sys
sys.path.insert(0, '/home/hmo/MoFin')
from strategy_lifecycle import regenerate_all

db = sqlite3.connect('/home/hmo/web-dashboard/data/mofin.db')

# Step 1: Add UNIQUE index if not exists
try:
    db.execute("CREATE UNIQUE INDEX IF NOT EXISTS idx_strategy_code ON holding_strategies(code)")
    print("UNIQUE index added")
except sqlite3.OperationalError as e:
    print(f"Index already exists or error: {e}")

# Step 2: Clear old stale data from holding_strategies (regenerate_all will rewrite)
deleted = db.execute("DELETE FROM holding_strategies").rowcount
print(f"Cleared {deleted} old strategy entries")
db.commit()
db.close()

# Step 3: Full regenerate
print("\n=== Running regenerate_all ===")
regenerate_all(stdout=True)
print("\nDone!")
