"""TAMCO demo server for a public address: the TAMCO demo company straight at the root, no demo chooser.

    python demo_server.py [--port 8000] [--reset]

Creates instance/lubwise.db with the TAMCO demo data on first start. The sign-ins (admin, manager, accountant, jubail,
viewer) and their passwords are in the README."""
import argparse
import os

os.environ.setdefault("LUBWISE_EDITION", "manufacturing")
from lubwise import create_app  # noqa: E402
from lubwise.db import create_schema  # noqa: E402

BASE = os.path.dirname(os.path.abspath(__file__))
DB = os.path.join(BASE, "instance", "lubwise.db")


def build(reset=False):
    app = create_app(DB)
    if reset:
        for suffix in ("", "-wal", "-shm"):
            if os.path.exists(DB + suffix):
                os.remove(DB + suffix)
    if not os.path.exists(DB):
        from lubwise.seed import seed
        create_schema(DB)
        with app.app_context():
            seed(demo=True, business="manufacturing")
    return app


if __name__ == "__main__":
    ap = argparse.ArgumentParser(description="TAMCO demo on one address")
    ap.add_argument("--host", default="127.0.0.1")
    ap.add_argument("--port", type=int, default=8000)
    ap.add_argument("--threads", type=int, default=8)
    ap.add_argument("--reset", action="store_true")
    args = ap.parse_args()
    from waitress import serve
    application = build(args.reset)
    print(f"TAMCO demo on http://{args.host}:{args.port}/")
    serve(application, host=args.host, port=args.port, threads=args.threads)
else:
    application = build()
