# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import pytest
from django.contrib.auth import get_user_model
from django.core import mail
from millionmilestogether.models import Invitation, RegisteredUser
from millionmilestogether.models.account import InvitationException


def test_registered_in_tests():
    assert get_user_model() == RegisteredUser


@pytest.mark.django_db
def test_friends(user, other_user, fake_request):
    friends = user.get_friends()
    assert len(friends) == 0

    # Initiating a friend request doesn't change the state of
    # friendships.
    user.initiate_friend_request(fake_request, other_user)
    friends = user.get_friends()
    assert len(friends) == 0

    # Once confirmed, both users are friends with each other.
    other_user.confirm_friend_request(user)
    friends = user.get_friends()
    assert len(friends) == 1
    assert friends[0] == other_user

    # Friendships should be reflexive.
    other_friends = other_user.get_friends()
    assert len(other_friends) == 1
    assert other_friends[0] == user


@pytest.mark.django_db
def test_send_invites(user, fake_request):
    user.send_invitation(fake_request, 'mike@example.com')

    invitations = Invitation.objects.all()
    assert len(invitations) == 1
    assert str(invitations[0]) == 'mike@example.com'

    assert len(mail.outbox) == 1
    email = mail.outbox[0]
    subject = "You've been invited to join millionmilestogether.com"
    assert email.subject == subject
    assert email.to == ['mike@example.com', ]
    assert email.from_email == 'webmaster@localhost'
    assert invitations[0].get_absolute_url() in email.body


@pytest.mark.django_db
def test_invitations_remaining(user, fake_request):
    assert user.invites_remaining() == 5
    user.send_invitation(fake_request, 'mike@example.com')
    assert user.invites_remaining() == 4
    user.send_invitation(fake_request, 'sully@example.com')
    assert user.invites_remaining() == 3
    user.send_invitation(fake_request, 'boo@example.com')
    assert user.invites_remaining() == 2
    user.send_invitation(fake_request, 'randall@example.com')
    assert user.invites_remaining() == 1
    user.send_invitation(fake_request, 'james@example.com')
    assert user.invites_remaining() == 0

    with pytest.raises(InvitationException):
        user.send_invitation(fake_request, 'roz@example.com')

    assert user.invites_remaining() == 0


@pytest.mark.django_db
def test_invitations_remaining_is_never_negative(user):
    user.invites_allowed = -1
    user.save()
    assert user.invites_remaining() == 0


@pytest.mark.django_db
def test_invite_the_same_person_twice(user, other_user, fake_request):
    assert user.invites_remaining() == 5
    assert other_user.invites_remaining() == 5
    user.send_invitation(fake_request, 'mike@example.com')
    assert user.invites_remaining() == 4

    with pytest.raises(InvitationException):
        user.send_invitation(fake_request, 'mike@example.com')

    assert user.invites_remaining() == 4
    assert other_user.invites_remaining() == 5

    other_user.send_invitation(fake_request, 'mike@example.com')

    assert user.invites_remaining() == 4
    assert other_user.invites_remaining() == 4


@pytest.mark.django_db
def test_invite_existing_user(user, other_user, fake_request):
    assert user.invites_remaining() == 5
    assert other_user.invites_remaining() == 5

    with pytest.raises(InvitationException):
        user.send_invitation(fake_request, other_user.email)

    assert user.invites_remaining() == 5
    assert other_user.invites_remaining() == 5


@pytest.mark.django_db
def test_display_name(user):
    assert user.display_name() == 'bob'

    user.first_name = 'Bob'
    assert user.display_name() == 'Bob'
