import boto
import mongoengine as me
#import jinja2 as jinja

import rmc.models as m
import rmc.shared.constants as c
import rmc.shared.secrets as secrets

me.connect(c.MONGO_DB_RMC)

def send_exam_schedule_email():

    conn = boto.connect_ses(
        aws_access_key_id=secrets.AWS_KEY_ID,
        aws_secret_access_key=secrets.AWS_SECRET_KEY)

    # TODO(mack): come up with name for email
    EMAIL_TITLE = 'Welcome to Flow'

    # TODO(mack): should not show num_friends if it's 0
    email_body = \
"""Hey %(first_name)s!

Thanks for signing up for Flow! We're really excited to have you on board.

Did you know %(num_friends)d friends have also signed up and are using Flow to plan, track and discover new courses!

Also, Flow now lets you see your final exam schedule for this term! Check if out on your profile at: http://uwflow.com/profile. (Don't worry, we'll continue to update it, so you don't have too!)

We're working hard to bring you more awesome features! Stay connected with us on Facebook (http://www.facebook.com/planyourflow) and Twitter (https://twitter.com/useflow).

Much love,

The Flow Team."""

    # TODO(mack): .only()
    # TODO(mack): Inspect why sent_exam_schedule_notifier_email=False doesn't work
    #users = m.User.objects(
    #    email__exists=True,
    #)
    users = m.User.objects(
        fbid='1647810326',
        email__exists=True,
    )

    num_sent = 0
    for user in users:
        if user.sent_exam_schedule_notifier_email:
            continue

        try:
            params = {
                'first_name': user.first_name,
                'num_friends': len(user.friend_ids),
            }
            conn.send_email(
                c.EMAIL_SENDER,
                EMAIL_TITLE,
                email_body % params,
                [user.email],
            )
            user.sent_exam_schedule_notifier_email = True
            user.save()
            num_sent += 1
        except:
            print 'Could not send email to user: %s' % user.id

    print 'Sent email to %d users' % num_sent

if __name__ == '__main__':
    send_exam_schedule_email()

@@ -519,17 +519,17 @@
 t_name)s
-!
+,
 %0A%0AThanks
@@ -596,18 +596,17 @@
 n board.
-%0A%0A
+ 
 Did you 
@@ -676,57 +676,11 @@
 Flow
- to plan, track and discover new courses!%0A%0AAlso, 
+?%0A%0A
 Flow
@@ -744,17 +744,17 @@
  Check i
-f
+t
  out on 
@@ -860,17 +860,16 @@
  have to
-o
 !)%0A%0AWe'r
@@ -923,30 +923,18 @@
 es! 
-Stay connected with us
+Like us on
  on 
@@ -985,16 +985,29 @@
 ow) and 
+follow us on 
 Twitter 
@@ -1035,16 +1035,32 @@
 useflow)
+ to stay updated
 .%0A%0AMuch 

update exam schedule email message