from flask import url_for
from flask.ext.testing import TestCase

from webapp import app, db
from webapp.models import Monkey, Friendship, BestFriend
from config import HEROKU_POSTGRESQL_CHARCOAL_URL


def setup_module():
    app.config['SQLALCHEMY_DATABASE_URI'] = HEROKU_POSTGRESQL_CHARCOAL_URL
    app.config['WTF_CSRF_ENABLED'] = False
    db.create_all()


def teardown_module():
    db.session.remove()
    db.drop_all()


class TestProfilePage(TestCase):
    def create_app(self):
        return app

    def test_profile_page_is_working(self):
        monkey = Monkey(name='Sajjad', age='21', email='Sajjad@nono.com')
        db.session.add(monkey)
        db.session.commit()
        response = self.client.get(url_for('profile',
                                           monkey_id=monkey.monkey_id))
        self.assert_200(response)

    def test_edit_info_inside_profile_page_valid_input_works(self):
        monkey = Monkey(name='Sajjad', age='21', email='Sajjad@nono.com')
        db.session.add(monkey)
        db.session.commit()
        response = self.client.get(url_for('profile',
                                           monkey_id=monkey.monkey_id))
        assert b'Sajjad@nono.com' in response.data

        data = {
            'name': 'Sajjad',
            'age': '21',
            'email': 'Sajjad@gmail.com'
        }
        response = self.client.post(url_for('profile',
                                            monkey_id=monkey.monkey_id),
                                    data=data,
                                    follow_redirects=True)
        self.assert_200(response)
        assert b'Sajjad@gmail.com' in response.data

    def test_best_friend_status_not_yet_and_name_of_bf_works(self):
        monkey = Monkey(name='Sajjad', age='21', email='Sajjad@nono.com')
        db.session.add(monkey)
        db.session.commit()
        response = self.client.get(url_for('profile',
                                           monkey_id=monkey.monkey_id))
        assert b'Best friend: Not yet' in response.data

        bf = Monkey(name='Alice', age='100', email='Alice@nono.com')
        db.session.add(bf)
        db.session.commit()
        best_friend = BestFriend(monkey_id=monkey.monkey_id,
                                 best_friend_id=bf.monkey_id)
        db.session.add(best_friend)
        db.session.commit()
        response = self.client.get(url_for('profile',
                                           monkey_id=monkey.monkey_id))
        assert b'Best friend: Alice' in response.data

    def test_terminate_monkey_works(self):
        monkey = Monkey(name='About2Terminate', age='21', email='ATT@no.com')
        monkey2 = Monkey(name='PoorFriend', age='21', email='ATT@no.com')
        monkey3 = Monkey(name='EvenPoorer', age='21', email='ATT@no.com')
        db.session.add(monkey)
        db.session.add(monkey2)
        db.session.add(monkey3)
        db.session.commit()
        friendship = Friendship(monkey_id=monkey.monkey_id,
                                friend_id=monkey2.monkey_id)
        friendship_reciprocal = Friendship(monkey_id=monkey2.monkey_id,
                                           friend_id=monkey.monkey_id)
        best_friend = BestFriend(monkey_id=monkey.monkey_id,
                                 best_friend_id=monkey3.monkey_id)
        db.session.add(friendship)
        db.session.add(best_friend)
        db.session.commit()
        response = self.client.get(url_for('terminate',
                                           monkey_id=monkey.monkey_id),
                                   follow_redirects=True)
        self.assert_200(response)
        assert b'About2Terminate was terminated' in response.data
        assert monkey not in Monkey.query.all()
        assert friendship not in Friendship.query.all()
        assert best_friend not in BestFriend.query.all()

    def test_edit_info_a_non_existent_monkey_gets_handled(self):
        data = {
            'name': 'Sajjad',
            'age': '21',
            'email': 'Sajjad@gmail.com'
        }
        response = self.client.post(url_for('profile',
                                            monkey_id=999999999),
                                    data=data)
        self.assert_200(response)
        assert b'500: Internal Server Error' in response.data
