from behave import given, when, then
from genosdb.models import User
from genosdb.exceptions import UserNotFound

# 'mongodb://localhost:27017/')


@given('a valid user with values {username}, {password}, {email}, {first_name}, {last_name}')
def step_impl(context, username, password, email, first_name, last_name):
    context.base_user = User(username=username, email=email, password=password, first_name=first_name,
                        last_name=last_name)


@when('I add the user to the collection')
def step_impl(context):
    context.user_service.save(context.base_user)


@then('I check {user_name} exists')
def step_impl(context, user_name):
    user_exists = context.user_service.exists(user_name)
    assert context.base_user.username == user_exists['username']
    assert context.base_user.password == user_exists['password']
    assert context.base_user.email == user_exists['email']
    assert context.base_user.first_name == user_exists['first_name']
    assert context.base_user.last_name == user_exists['last_name']
    assert user_exists['_id'] is not None

@given('I update {username} {field} with {value}')
def step_impl(context, username, field, value):
    user = context.user_service.exists(username)

    if user is not None:
        user[field] = value
        context.user_service.update(user.to_json())
    else:
        raise UserNotFound(username, "User was not found")





@then('I check {username} {field} is {value}')
def step_impl(context, username, field, value):
    user = context.user_service.exists(username)

    if user is not None:
        assert user[field] == value
    else:
        raise UserNotFound(username, "User was not found")



