"""
Configurations for all tests.

:copyright: Copyright 2013-2017, Dorian Pula <dorian.pula@amber-penguin-software.ca>
:license: AGPL v3+
"""

import pytest

from rookeries import database as rookeries_db
from rookeries.sites import models as site_models
from tests import utils


def pytest_addoption(parser):
    help_message = "Hostname and port of server under test: localhost, " \
                   "127.0.0.1, myremote-server.net..."
    parser.addoption(
        "--server-host",
        action="store",
        default="rookeries:5000",
        help=help_message)


@pytest.fixture
def api_base_uri(request):
    """
    The base URI of the API server to build request to API.  The base URI is
    the format http://hostname:port, provided by the server hostname and
    server port pytest parameters.

    :param request: The request for the test.
    :return: The base URI of the API server.
    """
    server_host = request.config.getoption("--server-host")
    return f"http://{server_host}"


@pytest.fixture(scope="session")
def db():
    # No need for a random database in a docker env
    db_server = rookeries_db.CouchDB.configure_from_env()
    db_server.create_db('rookeries')
    yield db_server


@pytest.fixture(scope="session")
def test_site(db) -> site_models.Site:
    """
    Generates a test site.

    :param db: The database to test against.
    :return: A tuple with information about the test site.
    """

    # TODO: Include way to generate and save the menu appropriately
    menu = []
    # TODO: Add a way to generate a save a test landing page
    landing_page = utils.generate_random_suffix()
    test_site_ = site_models.Site(
        name='Test Site',
        menu=menu,
        landing_page=landing_page,
    )

    # TODO: Make a whole lot more robust
    db.create_document(doc=test_site_.to_db_json())
    return site_models.Site(
        **db.find_first_document({'type': site_models.Site.doc_type()})
    )
