"""
Configurations for all tests.

:copyright: Copyright 2013-2017, Dorian Puła
    <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() -> rookeries_db.CouchDB:
    # 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: Remove demo config, once blocks available for consumption.
    demo_config = {
        'tagline': 'Simple Site Scaffolding',
        'favicon': '/static/images/mr-penguin-amber-favicon.ico',
        'logo': '/static/images/mr-penguin-amber.svg',
        'footer': 'Footer here.'
    }

    landing_page = utils.generate_test_page(db)
    test_pages = [utils.generate_test_page(db) for _ in range(0, 3)]

    menu = [
        {'title': page.title, 'url': f'/{page.slug}'}
        for page in test_pages
    ]
    menu.append({'title': landing_page.title, 'url': f'/{landing_page.slug}'})
    test_site_ = site_models.Site(
        name='Test Site',
        menu=menu,
        landing_page=landing_page.slug,
        config=demo_config
    )

    # TODO: Make a whole lot more robust
    db.create_or_update_document(doc=test_site_.to_db_json())
    return site_models.Site.find_in_db(db, {})
