from .interfaces import IConnectionConfig
from .solr_search import ConnectionConfig
from zope import schema
from zope.component.zcml import utility
from zope.interface import Interface


class IConnectionConfigDirective(Interface):
    """Directive which registers a SOLR connection config"""

    host = schema.ASCIILine(
        title="Host",
        description="The host name of the SOLR instance to be used.",
        required=True,
    )

    port = schema.Int(
        title="Port",
        description="The port of the SOLR instance to be used.",
        required=True,
    )

    basepath = schema.ASCIILine(
        title="Base path",
        description=(
            "The base prefix of the SOLR instance to be used -"
            " this should start with a forward-slash"
        ),
        required=True,
    )

    core = schema.ASCIILine(
        title="Core name",
        description="The name of the SOLR core to be used.",
        required=True,
    )


def configure_connection(_context, host, port, basepath, core):
    return utility(
        _context,
        provides=IConnectionConfig,
        component=ConnectionConfig(host, port, basepath, core),
    )
