# Copyright (C) 2016-2024 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License, version 3
# (only), as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# This program is dual-licensed. If you wish to learn more about the
# RhodeCode Enterprise Edition, including its added features, Support services,
# and proprietary license terms, please see https://rhodecode.com/licenses/
import logging

import click
import pyramid.paster

from rhodecode.lib.pyramid_utils import bootstrap
from rhodecode.lib.config_utils import get_app_config
from rhodecode.lib.db_manage import DbManage
from rhodecode.lib.utils2 import get_encryption_key
from rhodecode.model.db import Session


log = logging.getLogger(__name__)


@click.command()
@click.argument("ini_path", type=click.Path(exists=True))
@click.option("--force-yes/--force-no", default=None, help="Force yes/no to every question")
@click.option("--user", default=None, help="Initial super-admin username")
@click.option("--email", default=None, help="Initial super-admin email address.")
@click.option("--password", default=None, help="Initial super-admin password. Minimum 6 chars.")
@click.option("--api-key", help="Initial API key for the admin user")
@click.option(
    "--repos",
    default=None,
    help="Absolute path to storage location. This is storage for all "
    "existing and future repositories, and repository groups.",
)
@click.option(
    "--public-access/--no-public-access",
    default=None,
    help="Enable public access on this installation. Default is public access enabled.",
)
@click.option(
    "--skip-existing-db",
    default=False,
    is_flag=True,
    help="Do not destroy and re-initialize the database if it already exist.",
)
@click.option(
    "--apply-license-key",
    default=False,
    is_flag=True,
    help="Get the license key from a license file or ENV and apply during DB creation.",
)
def main(
    ini_path, force_yes, user, email, password, api_key, repos, public_access, skip_existing_db, apply_license_key
):
    return command(
        ini_path, force_yes, user, email, password, api_key, repos, public_access, skip_existing_db, apply_license_key
    )


def command(
    ini_path, force_yes, user, email, password, api_key, repos, public_access, skip_existing_db, apply_license_key
):
    # mapping of old parameters to new CLI from click
    options = dict(
        username=user,
        email=email,
        password=password,
        api_key=api_key,
        repos_location=repos,
        force_ask=force_yes,
        public_access=public_access,
    )
    pyramid.paster.setup_logging(ini_path)

    config = get_app_config(ini_path)

    db_uri = config["sqlalchemy.db1.url"]
    enc_key = get_encryption_key(config)
    dbmanage = DbManage(log_sql=True, dbconf=db_uri, root=".", tests=False, cli_args=options, enc_key=enc_key)
    if skip_existing_db and dbmanage.db_exists():
        return

    dbmanage.create_tables(override=True)
    dbmanage.set_db_version()
    opts = dbmanage.config_prompt(None)
    dbmanage.create_settings(opts)
    dbmanage.create_default_user()
    dbmanage.create_ghost_user()
    dbmanage.create_ai_user()
    dbmanage.create_admin_and_prompt()
    dbmanage.create_permissions()
    dbmanage.populate_default_permissions()
    if apply_license_key:
        from rhodecode.model.license import apply_license_from_file

        license_file_path = config.get("license.import_path")
        if license_file_path:
            apply_license_from_file(license_file_path, force=True)

    Session().commit()

    with bootstrap(ini_path, env={"RC_CMD_SETUP_RC": "1"}) as env:
        msg = "Successfully initialized database, schema and default data."
        print()
        print("*" * len(msg))
        print(msg.upper())
        print("*" * len(msg))
