#!/usr/bin/env python

import json
import os
import click

from elementalcms import management, ElementalContext
from elementalcms.core import FlaskContext, MongoDbContext
from elementalcms.management import GlobalDeps, Pages


@click.group()
@click.option('--debug/--no-debug', default=True)
@click.pass_context
def cli(ctx, debug):
    """Elemental CMS management CLI"""
    ctx.ensure_object(dict)

    ctx.obj['debug'] = debug

    if not os.path.exists('settings'):
        click.echo('Settings folder do not exist, you need to create a settings folder and at least a debug.json '
                   'settings file to start using the CLI.')
        exit(1)

    if debug:
        if not os.path.exists('settings/debug.json'):
            click.echo('Settings file do not exist. Please create a debug settings file in order to be able to use '
                       'the CLI.')
            exit(1)
    else:
        if not os.path.exists('settings/prod.json'):
            click.echo('Settings file do not exist. Please create a production settings file in order to be able to '
                       'use the CLI.')
            exit(1)

    with open(f'settings/{"debug" if debug else "prod"}.json') as config_file:
        settings = json.load(config_file)
        if 'cmsCoreContext' not in settings:
            click.echo('Settings file incomplete.')
            exit(1)
        if 'cmsDbContext' not in settings:
            click.echo('Settings file incomplete.')
            exit(1)
        cms_core_context = FlaskContext(settings['cmsCoreContext'])
        cms_db_context = MongoDbContext(settings['cmsDbContext'])
        ctx.obj['elemental_context'] = ElementalContext(cms_core_context, cms_db_context)


if __name__ == "__main__":
    cli.add_command(management.init)
    cli.add_command(GlobalDeps())
    cli.add_command(Pages())
    cli()
