import typer
import warnings
from rich.console import Console
from typing_extensions import Annotated


from cruxctl.command_groups.bigsnow.bigquery.bigquery_orgs import BigqueryOrgs
from cruxctl.command_groups.bigsnow.databricks.databricks_orgs import DatabricksOrgs
from cruxctl.command_groups.bigsnow.snowflake.snowflake_orgs import SnowflakeOrgs

cdu = typer.Typer()
console = Console()
error_console = Console(stderr=True)
warnings.filterwarnings("ignore")


def get_cls(ctx_obj):
    instance = ctx_obj.get("instance")

    if instance.lower() == "snowflake":
        return SnowflakeOrgs()
    elif instance.lower() == "bigquery":
        return BigqueryOrgs()
    elif instance.lower() == "databricks":
        return DatabricksOrgs()
    else:
        typer.echo(f"Unknown instance type: {instance}")
        raise typer.Exit(code=1)


@cdu.command("provision-list")
def provision_list(
    ctx: typer.Context,
    cdu_id: Annotated[
        str, typer.Option("--cdu_id", "-cdu_id", help="Pass in the cdu id")
    ] = None,
    region: Annotated[
        str, typer.Option("--region", "-region", help="Pass in the region")
    ] = None,
):

    instance_cls = get_cls(ctx.obj)
    instance_cls.cdu_provision_list(cdu_id, region)


@cdu.command("provision-add")
def provision_add(
    ctx: typer.Context,
    view_name: Annotated[
        str, typer.Option("--view_name", "-view_name", help="Pass in the view name")
    ] = None,
    region: Annotated[
        str, typer.Option("--region", "-region", help="Pass in the region")
    ] = None,
):

    instance_cls = get_cls(ctx.obj)
    instance_cls.cdu_provision_add(view_name, region)


@cdu.command("provision-delete")
def provision_delete(
    ctx: typer.Context,
    view_name: Annotated[
        str, typer.Option("--view_name", "-view_name", help="Pass in the org id")
    ] = None,
    region: Annotated[
        str, typer.Option("--region", "-region", help="Pass in the region")
    ] = None,
):

    instance_cls = get_cls(ctx.obj)
    instance_cls.cdu_provision_delete(view_name, region)


@cdu.command("provision-add-table")
def provision_add_table(
    ctx: typer.Context,
    table_name: Annotated[
        str, typer.Option("--table_name", "-table_name", help="Pass in the table name")
    ] = None,
    region: Annotated[
        str, typer.Option("--region", "-region", help="Pass in the region")
    ] = None,
):

    instance_cls = get_cls(ctx.obj)
    instance_cls.cdu_provision_add_table(table_name, region)
