# Path: groups/db.py
# class Group(models.Model):
# class GroupMember(models.Model):
# 
# Path: mailgun/api.py
# def call_mailgun(method, api_sub_url, data, params=None):
# def send_email(to_email, from_email, subject, text_body, html_body=None, tags=None, campaign_id=None):
# def send_mass_email(to_emails, from_email, subject, text_body, html_body=None, tags=None, campaign_id=None, recipient_variables=None):
# def create_campaign(campaign_id, campaign_name):
# def create_list(address, name=None, description=None, access_level=None):
# def update_list(address, name=None, description=None, access_level=None):
# def add_list_member(list_address, member_address):
# def remove_list_member(list_address, member_address):
# def get_list(list_address):
# def get_list_members(list_address):
# def get_unsubscribes(address):
# def delete_all_unsubscribes(address):
# def get_list_stats(list_address):
# def get_logs(limit=100, skip=0):
# def fetch_all_logs():
# def get_events(limit, **kwargs):
# def get_all_events(**kwargs):
# def get_campaign_events(campaign_id, event, recipient=None, limit=None, page=None, count=None):
# def _get_campaign_action(campaign_id, action, group_by, limit=None, page=None, count=None):
# def get_campaign_opens(campaign_id, group_by, limit=None, page=None, count=None):
# def get_campaign_clicks(campaign_id, group_by, limit=None, page=None, count=None):


from django.conf import settings
from groups import db
from mailgun import api as mailgun_api
def add_group_member( group_uri, member_email ):
    group_id = group_uri2id(group_uri)
    group_db = db.Group.objects.get(id=group_id)
    if not group_db.members.filter(email=member_email).exists():
        member = db.GroupMember(email=member_email, group=group_db)
        member.save()


def remove_group_member( group_uri, member_email ):
    group_id = group_uri2id(group_uri)
    group_db = db.Group.objects.get(id=group_id)
    group_db.members.get(email=member_email).delete()


def sync_group_with_mailgun( group_uri ):
    group = get_group(group_uri)

    # check if group exists on mailgun, if not - create
    mg_group = mailgun_api.get_list(group['address'])
    if not mg_group:
        mailgun_api.create_list(group['address'], description=group['description'], access_level='members' )

    # get list of members
    mg_members = mailgun_api.get_list_members(group['address'])
    mg_member_set = [m['address'] for m in mg_members]

    # for every member locally not in mailgun, add them
    for member in group['members']:
        if member not in mg_member_set:
            # add member to mailgun

# Path: groups/db.py
# class Group(models.Model):
# class GroupMember(models.Model):
# 
# Path: mailgun/api.py
# def call_mailgun(method, api_sub_url, data, params=None):
# def send_email(to_email, from_email, subject, text_body, html_body=None, tags=None, campaign_id=None):
# def send_mass_email(to_emails, from_email, subject, text_body, html_body=None, tags=None, campaign_id=None, recipient_variables=None):
# def create_campaign(campaign_id, campaign_name):
# def create_list(address, name=None, description=None, access_level=None):
# def update_list(address, name=None, description=None, access_level=None):
# def add_list_member(list_address, member_address):
# def remove_list_member(list_address, member_address):
# def get_list(list_address):
# def get_list_members(list_address):
# def get_unsubscribes(address):
# def delete_all_unsubscribes(address):
# def get_list_stats(list_address):
# def get_logs(limit=100, skip=0):
# def fetch_all_logs():
# def get_events(limit, **kwargs):
# def get_all_events(**kwargs):
# def get_campaign_events(campaign_id, event, recipient=None, limit=None, page=None, count=None):
# def _get_campaign_action(campaign_id, action, group_by, limit=None, page=None, count=None):
# def get_campaign_opens(campaign_id, group_by, limit=None, page=None, count=None):
# def get_campaign_clicks(campaign_id, group_by, limit=None, page=None, count=None):


# Path: groups/models.py
from django.conf import settings
from groups import db
from mailgun import api as mailgun_api

def add_group_member( group_uri, member_email ):
    group_id = group_uri2id(group_uri)
    group_db = db.Group.objects.get(id=group_id)
    if not group_db.members.filter(email=member_email).exists():
        member = db.GroupMember(email=member_email, group=group_db)
        member.save()


def remove_group_member( group_uri, member_email ):
    group_id = group_uri2id(group_uri)
    group_db = db.Group.objects.get(id=group_id)
    group_db.members.get(email=member_email).delete()


def sync_group_with_mailgun( group_uri ):
    group = get_group(group_uri)

    # check if group exists on mailgun, if not - create
    mg_group = mailgun_api.get_list(group['address'])
    if not mg_group:
        mailgun_api.create_list(group['address'], description=group['description'], access_level='members' )

    # get list of members
    mg_members = mailgun_api.get_list_members(group['address'])
    mg_member_set = [m['address'] for m in mg_members]

    # for every member locally not in mailgun, add them
    for member in group['members']:
        if member not in mg_member_set:
            # add member to mailgun

            mailgun_api.add_list_member(group['address'], member)