#!/usr/bin/python3

from subprocess import run
from argparse import ArgumentParser
import os

if __name__ == '__main__':
    parser = ArgumentParser()
    parser.add_argument(
        '-i',
        '--init-db',
        help='Run the admin cli to setup the db backend',
        action='store_true'
    )
    parser.add_argument(
        '-d',
        '--db-url',
        help='Sets the database URL to use for connecting to postgres',
        default='postgres://psql:example@localhost:5432'
    )
    parser.add_argument(
        '-c',
        '--check-container',
        help='Execs into the given container with bash for debugging'
    )
    parser.add_argument(
        '-s',
        '--server',
        help='Run a debug server (assumes db is ready)',
        action='store_true'
    )
    args = parser.parse_args()
    os.environ['DB_CONNECTION_STRING'] = args.db_url
    if args.init_db:
        run(
            'cargo run --bin admin-cli -- --setup',
            env=os.environ,
            shell=True
        )
    if args.check_container:
        run(f'docker exec -it {args.check_container} bash'.split())
    if args.server:
        run(
            f'npm run debug',
            env=os.environ,
            cwd='api',
            shell=True
        )