# Copyright: 2020 MoinMoin:RogerHaase
# Copyright: 2023-2024 MoinMoin:UlrichB
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.

"""
MoinMoin - CLI command to create a wiki instance.

Activate a Moin virtual environment, then run this command:

    moin create-instance --path /path/to/new/instance

If --path is not specified, the current working directory (CWD) is used.

If the path does not exist, directories are created.
If wikiconfig.py does not exist, it is copied from the venv config directory.
If intermap.txt does not exist, it is copied from the venv contrib directory.
If a wiki_local directory does not exist, it is created.

Next: change to the new instance directory and run this command to initialize storage and the index:

    moin index-create

Optionally, populate the empty wiki with additional commands:

    moin load <options>
    moin import19 <options>
    moin load-help <options>
"""

import os
import shutil
import click

from flask.cli import FlaskGroup
from flask.ctx import AppContext

from moin import config, contrib, log
from moin.app import create_app
from moin.cli.maint import index, modify_item

logging = log.getLogger(__name__)


@click.group(cls=FlaskGroup, create_app=create_app)
def cli():
    pass


@cli.command("create-instance", help="Create wikiconfig and wiki instance directories and copy required files.")
@click.option(
    "--full",
    "-f",
    required=False,
    is_flag=True,
    default=False,
    help="Full setup, including index creation and loading help data and the welcome page.",
)
@click.option(
    "--path",
    "-p",
    required=False,
    type=str,
    help="Path to the new wikiconfig directory; defaults to CWD if not specified.",
)
def cli_CreateInstance(full, path):
    return CreateInstance(full, path=path)


def CreateInstance(full, **kwargs):
    """
    Create wikiconfig and wiki instance directories and copy required files.
    """
    path = kwargs.get("path", None)
    if full and path:
        logging.error("The parameter full and path are mutually exclusive.")
        return False
    logging.debug("Instance creation started.")
    config_path = os.path.dirname(config.__file__)
    contrib_path = os.path.dirname(contrib.__file__)

    if not path:
        path = os.getcwd()
    if os.path.exists(path):
        logging.info("Directory %s already exists, using as wikiconfig dir.", os.path.abspath(path))
    else:
        os.makedirs(path)
        logging.info("Directory %s created.", os.path.abspath(path))

    if os.path.isfile(os.path.join(path, "wikiconfig.py")):
        logging.info("wikiconfig.py already exists, not copied.")
    else:
        shutil.copy(os.path.join(config_path, "wikiconfig.py"), path)

    if os.path.isfile(os.path.join(path, "intermap.txt")):
        logging.info("intermap.txt already exists, not copied.")
    else:
        shutil.copy(os.path.join(contrib_path, "intermap.txt"), path)
    local_path = os.path.join(path, "wiki_local")
    if not os.path.isdir(local_path):
        os.mkdir(local_path)
    logging.info("Instance creation finished.")

    if full:
        with AppContext(create_app()):
            build_instance()


def build_instance():
    """
    Create and build the index; load help data and the welcome page.
    """
    logging.info("Build instance started.")
    if index.IndexCreate():
        modify_item.LoadHelp(namespace="help-en", path_to_help=None)
        modify_item.LoadHelp(namespace="help-common", path_to_help=None)
        modify_item.LoadWelcome()
        index.IndexOptimize(tmp=False)
        logging.info("Full instance setup finished.")
        logging.info('You can now use "moin run" to start the built-in server.')
    else:
        logging.error("Build Instance failed.")
