import argparse
import os

import psycopg
import psycopg.conninfo

USER_POOL_ID = os.getenv("USER_POOL_ID")
POSTGRES_HOST = os.getenv("POSTGRES_HOST")
POSTGRES_PORT = os.getenv("POSTGRES_PORT")
POSTGRES_USER = os.getenv("POSTGRES_USER")
POSTGRES_PASSWORD = os.getenv("POSTGRES_PASSWORD")
POSTGRES_DB = os.getenv("POSTGRES_DB")


def main(tenant_id: str, name: str) -> None:
    print(f"TENANT_ID: {tenant_id}")
    print(f"NAME: {name}")

    __insert(
        tenant_id=tenant_id,
        name=name,
    )


def __insert(
    tenant_id: str,
    name: str,
) -> None:
    postgres_url = psycopg.conninfo.make_conninfo(
        host=POSTGRES_HOST,
        port=POSTGRES_PORT,
        user=POSTGRES_USER,
        password=POSTGRES_PASSWORD,
        dbname=POSTGRES_DB,
    )
    with psycopg.connect(postgres_url) as conn, conn.cursor() as cur:
        cur.execute(
            """--sql
            INSERT INTO client_tenants (tenant_id, name)
            VALUES (%(tenant_id)s, %(name)s)
            """,
            {
                "tenant_id": tenant_id,
                "name": name,
            },
        )
        conn.commit()


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Client API Key Creator Script")
    parser.add_argument(
        "--tenant-id",
        type=str,
        required=True,
    )
    parser.add_argument(
        "--name",
        type=str,
        required=True,
    )

    args = parser.parse_args()
    main(
        tenant_id=args.tenant_id,
        name=args.name,
    )
