import os
from fastapi import *
from psycopg2.errors import UndefinedTable
from sqlmodel import Session, select, text
from sqlalchemy.exc import ProgrammingError
from .models.timelog import TimeLog
from .models.calendar import Calendar
from .utils import (
    engine,
    create_db,
    tags_metadata,
    execute_sample_sql,
)
from .api import (
    user,
    timelog,
    forecast,
    epic,
    epic_area,
    client,
    rate,
    team,
    role,
    sponsor,
    capacity,
    demand,
)
import csv

app = FastAPI(title="timeflow app API", openapi_tags=tags_metadata)

app.include_router(timelog.router)
app.include_router(forecast.router)
app.include_router(user.router)
app.include_router(epic.router)
app.include_router(epic_area.router)
app.include_router(client.router)
app.include_router(rate.router)
app.include_router(team.router)
app.include_router(role.router)
app.include_router(sponsor.router)
app.include_router(capacity.router)
app.include_router(demand.router)


@app.on_event("startup")
def on_startup():
    with Session(engine) as session:
        if os.getenv("TIMEFLOW_DEV") == "true":
            try:
                statement = select(TimeLog)
                results = session.exec(statement)
            except ProgrammingError:
                create_db()
                execute_sample_sql()
        elif os.getenv("TIMEFLOW_DEV") == "false":
            try:
                statement = select(TimeLog)
                results = session.exec(statement)
            except ProgrammingError:
                create_db()



@app.on_event("startup")
def implement_calendar_table():
    with Session(engine) as session:
        try:
            statement = select(Calendar.year_name).where(Calendar.id == 1)
            result = session.exec(statement).one()
        except Exception as e:
            print(e)
            values_sql = f"""INSERT INTO app_db.calendar (date, year_number, year_name, quarter_number, quarter_name
                        , month_number, month_name, week_number, week_name, week_day_number, week_day_name)
                        VALUES """
            with open("backend/calendar.csv") as csvfile:
                reader = csv.reader(csvfile, delimiter=",", quotechar="|")
                values_list = []
                for index, row in enumerate(reader):
                    if index > 0 and row[0] != "":
                        _row = [f"'{item}'" for item in row]
                        row_sql = ", ".join(_row)
                        values = f"({row_sql}),"
                        values_sql += values
                values_sql += f"({row_sql});"
                session.execute(text(values_sql))
                session.commit()

sqlmodel.Session os.getenv sqlmodel.select sqlmodel.text csv.reader
[((1035, 1050), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (1042, 1050), False, 'from sqlmodel import Session, select, text\n'), ((1620, 1635), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (1627, 1635), False, 'from sqlmodel import Session, select, text\n'), ((1074, 1099), 'os.getenv', 'os.getenv', (['"""TIMEFLOW_DEV"""'], {}), "('TIMEFLOW_DEV')\n", (1083, 1099), False, 'import os\n'), ((1156, 1171), 'sqlmodel.select', 'select', (['TimeLog'], {}), '(TimeLog)\n', (1162, 1171), False, 'from sqlmodel import Session, select, text\n'), ((1337, 1362), 'os.getenv', 'os.getenv', (['"""TIMEFLOW_DEV"""'], {}), "('TIMEFLOW_DEV')\n", (1346, 1362), False, 'import os\n'), ((1420, 1435), 'sqlmodel.select', 'select', (['TimeLog'], {}), '(TimeLog)\n', (1426, 1435), False, 'from sqlmodel import Session, select, text\n'), ((1685, 1711), 'sqlmodel.select', 'select', (['Calendar.year_name'], {}), '(Calendar.year_name)\n', (1691, 1711), False, 'from sqlmodel import Session, select, text\n'), ((2182, 2231), 'csv.reader', 'csv.reader', (['csvfile'], {'delimiter': '""","""', 'quotechar': '"""|"""'}), "(csvfile, delimiter=',', quotechar='|')\n", (2192, 2231), False, 'import csv\n'), ((2652, 2668), 'sqlmodel.text', 'text', (['values_sql'], {}), '(values_sql)\n', (2656, 2668), False, 'from sqlmodel import Session, select, text\n')]