import psycopg as pg

sql_create = [
    "sql/create/0.base.sql",
    "sql/create/1.title.sql",
    "sql/create/2.junctions.sql",
    "sql/create/3.reservoirs.sql",
    "sql/create/4.tanks.sql",
    "sql/create/5.pipes.sql",
    "sql/create/6.pumps.sql",
    "sql/create/7.valves.sql",
    "sql/create/8.tags.sql",
    "sql/create/9.demands.sql",
    "sql/create/10.status.sql",
    "sql/create/11.patterns.sql",
    "sql/create/12.curves.sql",
    "sql/create/13.controls.sql",
    "sql/create/14.rules.sql",
    "sql/create/15.energy.sql",
    "sql/create/16.emitters.sql",
    "sql/create/17.quality.sql",
    "sql/create/18.sources.sql",
    "sql/create/19.reactions.sql",
    "sql/create/20.mixing.sql",
    "sql/create/21.times.sql",
    "sql/create/22.report.sql",
    "sql/create/23.options.sql",
    "sql/create/24.coordinates.sql",
    "sql/create/25.vertices.sql",
    "sql/create/26.labels.sql",
    "sql/create/27.backdrop.sql",
    "sql/create/28.end.sql",
    "sql/create/operation.sql"
]

sql_drop = [
    "sql/drop/operation.sql",
    "sql/drop/28.end.sql",
    "sql/drop/27.backdrop.sql",
    "sql/drop/26.labels.sql",
    "sql/drop/25.vertices.sql",
    "sql/drop/24.coordinates.sql",
    "sql/drop/23.options.sql",
    "sql/drop/22.report.sql",
    "sql/drop/21.times.sql",
    "sql/drop/20.mixing.sql",
    "sql/drop/19.reactions.sql",
    "sql/drop/18.sources.sql",
    "sql/drop/17.quality.sql",
    "sql/drop/16.emitters.sql",
    "sql/drop/15.energy.sql",
    "sql/drop/14.rules.sql",
    "sql/drop/13.controls.sql",
    "sql/drop/12.curves.sql",
    "sql/drop/11.patterns.sql",
    "sql/drop/10.status.sql",
    "sql/drop/9.demands.sql",
    "sql/drop/8.tags.sql",
    "sql/drop/7.valves.sql",
    "sql/drop/6.pumps.sql",
    "sql/drop/5.pipes.sql",
    "sql/drop/4.tanks.sql",
    "sql/drop/3.reservoirs.sql",
    "sql/drop/2.junctions.sql",
    "sql/drop/1.title.sql",
    "sql/drop/0.base.sql"
]

def create_template():
    with pg.connect(conninfo="dbname=postgres host=127.0.0.1", autocommit=True) as conn:
        with conn.cursor() as cur:
            cur.execute("create database project")
    with pg.connect(conninfo="dbname=project host=127.0.0.1") as conn:
        with conn.cursor() as cur:
            for sql in sql_create:
                with open(sql, "r") as f:
                    cur.execute(f.read())
                print(f'executed {sql}')
        conn.commit()

def have_template():
    with pg.connect(conninfo="dbname=postgres host=127.0.0.1", autocommit=True) as conn:
        with conn.cursor() as cur:
            cur.execute("select * from pg_database where datname = 'project'")
            return cur.rowcount > 0

def delete_template():
    with pg.connect(conninfo="dbname=project host=127.0.0.1") as conn:
        with conn.cursor() as cur:
            for sql in sql_drop:
                with open(sql, "r") as f:
                    cur.execute(f.read())
                print(f'executed {sql}')
        conn.commit()
    with pg.connect(conninfo="dbname=postgres host=127.0.0.1", autocommit=True) as conn:
        with conn.cursor() as cur:
            cur.execute("drop database project")

if __name__ == "__main__":
    if (have_template()):
        delete_template()
    create_template()
