#!/usr/bin/env python

import pynexus.config
import pynexus.report
import sys
import os
import datetime
import argparse
import pprint
import json


def main(argv):
    """ Fetch the requested report from App Nexus and save it to file system. """

    # boot
    args = parse_args(argv)
    pynexus.config.set(api_endpoint = args.config['api_endpoint'],
                       auth_user    = args.config['user'],
                       auth_pass    = args.config['pass'])


    # get report request
    report = json.load(open(args.report_request, 'r'))


    # inject time params
    time_params = {'start'   : 'start_date',
                   'end'     : 'end_date',
                   'interval': 'report_interval'}

    for arg, param in time_params.items():
        report['report'][param] = getattr(args, arg)


    # prompt user for confirmation
    if not args.no_interaction:
        prompt_user_for_confirmation(report)


    # determine report path
    report_dir = os.path.join(args.save_dir, report['report']['report_type'])

    if not os.path.exists(report_dir):
        os.makedirs(report_dir)

    report_file = os.path.join(
        report_dir,
        datetime.datetime.now().strftime('%Y%m%d_%H%M.csv')
    )

    # fetch and save report
    pynexus.report.get(report, report_file, debug=args.debug)


    ##
    print os.path.realpath(report_file)



def parse_args(argv):
    """ Parse the arguments using argparse lib and set App Nexus
    configuration params using the given config file or appropriate
    env vars. """

    parser = argparse.ArgumentParser(description='Fetch the requested report from App Nexus and save it to file system.')

    parser.add_argument('report_request',          help='Path to JSON file that contains the report request.')
    parser.add_argument('-c', '--config',          help='Path to JSON file that contains the keys "api_endpoint", "user" and "pass". If this parameter is not given, env vars APPNEXUS_API_ENDPOINT, APPNEXUS_USER and APPNEXUS_PASS must be set.')
    parser.add_argument('-d', '--save_dir',        default='', help='The directory to save the report CSV. Default is current directory.')
    parser.add_argument('-I', '--no-interaction',  action='store_true', help='Whether to ask confirmation before fetching report.')
    parser.add_argument('-s', '--start',           help='Value for "start_date" parameter of report request.')
    parser.add_argument('-e', '--end',             help='Value for "end_date" parameter of report request.')
    parser.add_argument('-i', '--interval',        help='Value for "report_interval" parameter of report request.')
    parser.add_argument('--debug',                 action='store_true', help='Whether to print extra debug information or not.')

    args = parser.parse_args(argv[1:])


    if args.config:
        args.config = json.load(open(args.config, 'r'))

    elif (
        os.environ.get('APPNEXUS_API_ENDPOINT') and
        os.environ.get('APPNEXUS_USER') and
        os.environ.get('APPNEXUS_PASS')
    ):
        args.config = {
            'api_endpoint': os.environ['APPNEXUS_API_ENDPOINT'],
            'user'        : os.environ['APPNEXUS_USER'],
            'pass'        : os.environ['APPNEXUS_PASS']
        }

    else:
        print """
            You must either provide a --config parameter or
            set the env vars APPNEXUS_API_ENDPOINT, APPNEXUS_USER and APPNEXUS_PASS!
            Call this script with the --help option for more information.
        """

        sys.exit(1)


    return args


def prompt_user_for_confirmation(report_payload):
    print 'I will now fetch the report for the payload below:'
    pp(report_payload)

    confirmation = raw_input('Are you sure? > ')

    if confirmation.lower() != 'yes':
        sys.exit(0)


def pp(obj):
    print ''
    pprint.pprint(obj)
    print ''


if __name__ == '__main__':
    main(sys.argv)
