#!/usr/bin/env python3
from __future__ import print_function
import os, sys
abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
sys.path.insert(0, os.path.join(dname, "app"))

from flask.ext.script import Manager, Server, Shell
from app.github_contributions import app, mongo

def _make_context():
    return dict(
        app=app,
        mongo=mongo
    )

manager = Manager(app)
manager.add_command('shell', Shell(
    make_context=_make_context,
    use_ipython=True,
    banner='GitHub Contributions Shell'
))

@manager.command
def ensure_indexes():
    """ ensure contributions are indexed
    """
    collection = mongo.db.contributions
    print("indexing _event_id")
    collection.create_index("_event_id",
                            sparse=True,
                            unique=True)
    print("indexing _user_lower")
    collection.create_index("_user_lower")
    print("indexing created_at")
    collection.create_index("created_at")

if __name__=='__main__':
    manager.run()
