import typer
from sqlalchemy import create_engine
from sqlalchemy.orm import Session

from retailtwin.bootstrap import (
    bootstrap_clients,
    bootstrap_discounts,
    bootstrap_items,
    bootstrap_locations,
    bootstrap_providers,
    bootstrap_stock,
    bootstrap_taskowners,
)
from retailtwin.models import Base
from retailtwin.sql.sync import funcandproc

app = typer.Typer()


@app.command()
def init(db_uri: str):
    """Persist the schema on the database

    Args:
        db_uri (str): SQLAlchemy db uri
    """
    engine = create_engine(db_uri, echo=True)
    Base.metadata.create_all(engine)


@app.command()
def sync(db_uri: str):
    """Sync the functions, procedures, triggers and views

    Args:
        db_uri (str): SQLAlchemy db uri
    """
    funcandproc(db_uri)


@app.command()
def bootstrap(db_uri: str):
    """Populate the databse with data. Only execute on an empty database

    Args:
        db_uri (str): Sqlalchemy db uri
    """
    engine = create_engine(db_uri)

    with Session(engine) as session:
        print("Discounts...")
        bootstrap_discounts(session)
        print("Providers...")
        bootstrap_providers(session)
        print("Locations...")
        bootstrap_locations(session)
        print("Items...")
        bootstrap_items(session)
        print("Clients...")
        bootstrap_clients(session)
        print("Stockage...")
        bootstrap_stock(session)
        print("TaskOwners")
        bootstrap_taskowners(session)


if __name__ == "__main__":
    app()
