import argparse
from time import time
import csv
import os

from py_db import db
db = db("NSBL")

def initiate(year):
    start_time = time()

    print "\nexporting to .csv"
    export_current_rosters(year)
    export_current_team_summary(year)
    export_historical_tables()
    export_historical_team_draft_table(year)
    export_historical_team_free_agency_table(year)

    export_standings(year)
    export_changes(year)

    export_milestones(year)

    export_hidden_teams(year)
    export_hidden_projections(year)
    export_hidden_FA_batters(year)
    export_hidden_FA_pitchers(year)

    export_hidden_value(year)

    end_time = time()
    elapsed_time = float(end_time - start_time)
    print "\n\nexport_leaderboards.py"
    print "time elapsed (in seconds): " + str(elapsed_time)
    print "time elapsed (in minutes): " + str(elapsed_time/60.0)


def export_current_rosters(year):

    print "\t current rosters"

    path_base = "/Users/connordog/Dropbox/Desktop_Files/Work_Things/connor-r.github.io/csvs"

    t_name = 'current_rosters'
    table = 'excel_rosters'

    csv_title = path_base + "/NSBL_%s.csv" % (t_name)
    csv_file = open(csv_title, "wb")
    append_csv = csv.writer(csv_file)
    csv_header = []

    qry = """SELECT e.*
    FROM excel_rosters e
    JOIN (
        SELECT year
        , MAX(date) AS date
        FROM excel_rosters
        WHERE 1
            AND year = %s
    ) cur USING (year, date);"""

    query = qry % (year)

    res = db.query(query)