__author__ = "Florian Kaiser"
__copyright__ = "Copyright 2022, Project Aktienbot"
__credits__ = ["Florian Kaiser", "Florian Kellermann", "Linus Eickhof", "Kevin Pauer"]
__license__ = "GPL 3.0"
__version__ = "1.0.0"

import pytest
from app import create_app, db
from app.models import User, Transaction, Keyword, Share

from app.helper_functions import hash_password


@pytest.fixture(scope='module')
def new_user():
    user = User(
        email="user@example.com",
        username="user",
        password=hash_password("password"),
        admin=False
    )
    return user


@pytest.fixture(scope='module')
def new_transaction():
    transaction = Transaction(
        email="user@example.com",
        symbol="DTEGY",
        time="2022-03-29T10:00:00.000Z",
        count=10,
        price=9.99
    )
    return transaction


@pytest.fixture(scope='module')
def new_keyword():
    keyword = Keyword(
        email="user@example.com",
        keyword="Elon Musk",
    )
    return keyword


@pytest.fixture(scope='module')
def new_share():
    share = Share(
        email="user@example.com",
        symbol="DTEGY",
    )
    return share


@pytest.fixture(scope='module')
def test_client():
    flask_app = create_app('config/flask_test.cfg')

    # Create a test client using the Flask application configured for testing
    with flask_app.test_client() as testing_client:
        # Establish an application context
        with flask_app.app_context():
            yield testing_client  # this is where the testing happens!


@pytest.fixture(scope='function')
def init_database(test_client):
    # Create the database and the database table
    db.create_all()

    # Insert user data
    user1 = User(
        email="user1@example.com",
        username="user1",
        password=hash_password("password"),
        telegram_user_id="12345678",
        admin=False
    )
    user2 = User(
        email="user2@example.com",
        username="user2",
        password=hash_password("password"),
        telegram_user_id="87654321",
        admin=False
    )
    admin = User(
        email="admin1@example.com",
        username="admin1",
        password=hash_password("admin1"),
        telegram_user_id="00000000",
        admin=True
    )
    bot = User(
        email="bot1@example.com",
        username="bot1",
        password=hash_password("bot1"),
        telegram_user_id="00000000",
        admin=False
    )
    db.session.add(user1)
    db.session.add(user2)
    db.session.add(admin)
    db.session.add(bot)

    # Commit the changes for the users
    db.session.commit()

    yield  # this is where the testing happens!

    db.session.commit()
    db.drop_all()