import click

from inspira.cli.create_app import generate_project
from inspira.cli.create_controller import create_controller_file, create_src_directory
from inspira.cli.generate_database_file import create_database_file
from inspira.cli.generate_model_file import database_file_exists, generate_model_file
from inspira.cli.generate_repository_file import generate_repository_file
from inspira.cli.generate_service_file import generate_service_file
from inspira.migrations.migrations import create_migrations, run_migrations
from inspira.migrations.utils import get_all_module_names

DATABASE_TYPES = ["postgres", "mysql", "sqlite", "mssql"]


@click.group()
def cli():
    pass


@cli.group()
def new():
    pass


@new.command()
@click.argument("name")
@click.option("--only-controller", "only_controller", is_flag=True, required=False)
@click.option("--websocket", "is_websocket", is_flag=True, required=False)
def module(name, only_controller, is_websocket):
    if not name:
        click.echo("Please provide a name for the module")
        return

    if not only_controller and not database_file_exists():
        click.echo(
            "Database file doesn't exist. Please generate one before generating modules"
        )
        return

    create_module_files(name, only_controller, is_websocket)


def create_module_files(name, only_controller, is_websocket):
    create_src_directory()
    create_controller_file(name, is_websocket)

    if not only_controller:
        generate_model_file(name)
        generate_service_file(name)
        generate_repository_file(name)


@new.command()
@click.option("--name", required=True, help="Name of the database.")
@click.option("--type", required=True, help="Database type")
def database(name, type):
    create_database_file(name, type)


@cli.command()
@click.argument("module_name")
@click.option(
    "--empty", nargs=1, type=str, required=False, help="Generate empty migration file."
)
def createmigrations(module_name, empty):
    migration_name = None

    if empty:
        migration_name = empty

    create_migrations(module_name, migration_name)


@cli.command()
@click.argument("module_name", required=False)
def migrate(module_name):
    if module_name:
        module_names = [module_name]
    else:
        module_names = get_all_module_names()

    for module_name in module_names:
        run_migrations(module_name)


@cli.command()
def init():
    generate_project()
    click.echo("App file created successfully.")


if __name__ == "__main__":
    cli()
