# -*- coding: utf-8 -*-

import os
import subprocess
import errno

from flask.ext.script import Manager

from rootio import create_app
from rootio.extensions import db

from rootio.radio import Station, Language
from rootio.user import User, UserDetail, ADMIN, ACTIVE
from rootio.utils import MALE

from alembic import command
from alembic.config import Config


app = create_app()
manager = Manager(app)

alembic_config = Config(os.path.realpath(os.path.dirname(__name__)) + "/alembic.ini")

@manager.command
def run():
    """Run in local machine."""

    app.run(debug=True)

@manager.command
def alembic():
    """Run in local machine."""
    subprocess.call(["venv/bin/alembic", "init", "alembic"])

@manager.command
def migrate(direction):
    """Migrate db revision"""
    if direction == "up":
        command.upgrade(alembic_config, "head")
    elif direction == "down":
        command.downgrade(alembic_config, "-1")

@manager.command
def migration(message):
    """Create migration file"""
    command.revision(alembic_config, autogenerate=True, message=message)

@manager.command
def stamp(revision):
    """Fake a migration to a particular revision"""
    alembic_cfg = Config("alembic.ini")
    command.stamp(alembic_cfg, revision)

@manager.command
def initdb():
    """Init/reset database with default data."""

    db.drop_all()
    db.create_all()

    admin = User(
            name=u'admin',
            email=u'admin@example.com',
            password=u'123456',
            role_code=ADMIN,
            status_code=ACTIVE,
            user_detail=UserDetail(
                gender_code=MALE,
                age=25,
                url=u'http://example.com',
                location=u'Kampala',
                bio=u''))
    db.session.add(admin)
    
    english = Language(name="English",iso639_1="en",iso639_2="eng",locale_code="en_UG")
    db.session.add(english)
    luganda = Language(name="Luganda",iso639_1="lg",iso639_2="lug",locale_code="lg_UG")
    db.session.add(luganda)

    db.session.commit()
    alembic_cfg = Config("alembic.ini")
    command.stamp(alembic_cfg, "head")

manager.add_option('-c', '--config',
                   dest="config",
                   required=False,
                   help="config file")

@manager.command
def dropdb():
    #for when db.drop_all won't cut it, particularly with postgres on server
    import sqlalchemy
    engine = sqlalchemy.create_engine('postgresql://postgres:NLPog1986@localhost')
    meta = sqlalchemy.MetaData(engine)
    meta.reflect()
    meta.drop_all()

@manager.command
def list_routes():
    import urllib
    from flask import url_for
    output = []
    for rule in app.url_map.iter_rules():
        options = {}
        for arg in rule.arguments:
            options[arg] = "[{0}]".format(arg)

        methods = ','.join(rule.methods)
        url = url_for(rule.endpoint, **options)
        line = urllib.unquote("{:50s} {:20s} {}".format(rule.endpoint, methods, url))
        output.append(line)

    for line in sorted(output):
        print line

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

@@ -27,17 +27,22 @@
 mport os
+, sys
 %0A
-
 import s
@@ -1267,20 +1267,22 @@
 and%0Adef 
-init
+reset_
 db():%0A  
@@ -1290,14 +1290,9 @@
  %22%22%22
-Init/r
+R
 eset
@@ -1304,30 +1304,234 @@
 base
- wi
+%22%22%22%0A%0A    print %22WARNING: This will reset 
 th
+e
  d
-efault data.%22%22%22
+atabase and may cause data loss.%22%0A    response = raw_input(%22Are you sure you want to continue? (Yes/No) %22)%0A    if not response == %22Yes%22:%0A        print %22Aborted.%22%0A        sys.exit()
 %0A%0A  
@@ -2484,18 +2484,24 @@
 def drop
+_
 db
+_hard
 ():%0A    
@@ -2573,16 +2573,222 @@
  server%0A
+%0A    print %22WARNING: This will drop all database tables.%22%0A    response = raw_input(%22Are you sure you want to continue? (Yes/No) %22)%0A    if not response == %22Yes%22:%0A        print %22Aborted.%22%0A        sys.exit()%0A%0A
     impo

rename destructive database commands and prompt user for verification before running