import json
from datadog import api as datadog_api
import requests
from django.core.management import call_command
from corehq.apps.hqadmin.management.utils import get_deploy_email_message_body
from django.core.management.base import BaseCommand
from corehq.apps.hqadmin.models import HqDeploy
from datetime import datetime
from optparse import make_option
from django.conf import settings
from pillow_retry.models import PillowError

STYLE_MARKDOWN = 'markdown'
STYLE_SLACK = 'slack'


def diff_link(style, url):
    if style == STYLE_MARKDOWN:
        return '[here]({})'.format(url)
    elif style == STYLE_SLACK:
        return '<{}|here>'.format(url)


class Command(BaseCommand):
    help = "Creates an HqDeploy document to record a successful deployment."
    args = "[user]"

    option_list = BaseCommand.option_list + (
        make_option('--user', help='User', default=False),
        make_option('--environment', help='Environment {production|staging etc...}', default=settings.SERVER_ENVIRONMENT),
        make_option('--mail_admins', help='Mail Admins', default=False, action='store_true'),
        make_option('--url', help='A link to a URL for the deploy', default=False),
    )

    def handle(self, *args, **options):
        compare_url = options.get('url', None)

        deploy = HqDeploy(
            date=datetime.utcnow(),
            user=options['user'],
            environment=options['environment'],
            diff_url=compare_url
        )
        deploy.save()

        #  reset PillowTop errors in the hope that a fix has been deployed
        rows_updated = PillowError.bulk_reset_attempts(datetime.utcnow())
        if rows_updated:
            print "\n---------------- Pillow Errors Reset ----------------\n" \
                  "{} pillow errors queued for retry\n".format(rows_updated)

        deploy_notification_text = (
            "CommCareHQ has been successfully deployed to *{}* by *{}*. Monitor the dashboard: https://p.datadoghq.com/sb/5c4af2ac8-1f739e93ef"
            "Find the diff {{diff_link}}".format(
                options['environment'],
                options['user'],
            )
        )
        if hasattr(settings, 'MIA_THE_DEPLOY_BOT_API'):
            link = diff_link(STYLE_SLACK, compare_url)
            requests.post(settings.MIA_THE_DEPLOY_BOT_API, data=json.dumps({
                "username": "Igor the Iguana",
                "text": deploy_notification_text.format(diff_link=link),
            }))

        if settings.DATADOG_API_KEY:
            tags = ['environment:{}'.format(options['environment'])]
            link = diff_link(STYLE_MARKDOWN, compare_url)
            datadog_api.Event.create(
                title="Deploy Success",
                text=deploy_notification_text.format(diff_link=link),
                tags=tags,
                alert_type="success"
            )

            print "\n=============================================================\n" \
                  "Congratulations! Deploy Complete.\n\n" \
                  "Don't forget to keep an eye on the deploy dashboard to " \
                  "make sure everything is running smoothly.\n\n" \
                  "https://p.datadoghq.com/sb/5c4af2ac8-1f739e93ef" \
                  "\n=============================================================\n"

        if options['mail_admins']:
            message_body = get_deploy_email_message_body(
                environment=options['environment'], user=options['user'],
                compare_url=compare_url)
            call_command('mail_admins', message_body, **{'subject': 'Deploy successful', 'html': True})

@@ -482,29 +482,252 @@
 ck'%0A
-%0A%0Adef diff_link(style
+DASHBOARD_URL = 'https://p.datadoghq.com/sb/5c4af2ac8-1f739e93ef'%0A%0A%0Adef diff_link(style, url):%0A    return make_link(style, 'here', url)%0A%0A%0Adef dashboard_link(style, url):%0A    return make_link(style, 'dashboard', url)%0A%0A%0Adef make_link(style, label
 , ur
@@ -783,26 +783,49 @@
 n '%5B
-here%5D(%7B%7D)'.format(
+%7Blabel%7D%5D(%7Burl%7D)'.format(label=label, url=
 url)
@@ -878,24 +878,47 @@
  '%3C%7B
-%7D%7Chere%3E'.format(
+url%7D%7C%7Blabel%7D%3E'.format(label=label, url=
 url)
@@ -2211,16 +2211,18 @@
 tor the 
+%7B%7B
 dashboar
@@ -2226,57 +2226,17 @@
 oard
-: https://p.datadoghq.com/sb/5c4af2ac8-1f739e93ef
+_link%7D%7D. 
 %22%0A  
@@ -2667,38 +2667,156 @@
 text.format(
-diff_link=link
+%0A                    dashboard_link=dashboard_link(STYLE_SLACK, DASHBOARD_URL),%0A                    diff_link=link,%0A                
 ),%0A         
@@ -3118,30 +3118,151 @@
 .format(
-diff_link=link
+%0A                    dashboard_link=dashboard_link(STYLE_MARKDOWN, DASHBOARD_URL),%0A                    diff_link=link,%0A                
 ),%0A     

make dashboard a URL link