from fastapi import *
from sqlmodel import Session, select, SQLModel
from sqlalchemy.exc import OperationalError
from backend.models.timelog import TimeLog
from backend.models.calendar import Calendar
from backend.utils import (
    engine,
    sqlite3_engine,
    create_db,
    tags_metadata,
    execute_sample_sql,
)
from backend.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)

session = Session(engine)
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():
    try:
        statement = select(TimeLog)
        results = session.exec(statement)
    except OperationalError:
        create_db()
        execute_sample_sql(session)


@app.on_event("startup")
def implement_calendar_table():
    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 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});"
            cur = sqlite3_engine.cursor()
            cur.execute(values_sql)
            sqlite3_engine.commit()
            sqlite3_engine.close()

backend.utils.sqlite3_engine.commit sqlmodel.Session backend.utils.sqlite3_engine.close backend.utils.execute_sample_sql sqlmodel.select backend.utils.create_db backend.utils.sqlite3_engine.cursor csv.reader
[((583, 598), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (590, 598), False, 'from sqlmodel import Session, select, SQLModel\n'), ((1080, 1095), 'sqlmodel.select', 'select', (['TimeLog'], {}), '(TimeLog)\n', (1086, 1095), False, 'from sqlmodel import Session, select, SQLModel\n'), ((1175, 1186), 'backend.utils.create_db', 'create_db', ([], {}), '()\n', (1184, 1186), False, 'from backend.utils import engine, sqlite3_engine, create_db, tags_metadata, execute_sample_sql\n'), ((1195, 1222), 'backend.utils.execute_sample_sql', 'execute_sample_sql', (['session'], {}), '(session)\n', (1213, 1222), False, 'from backend.utils import engine, sqlite3_engine, create_db, tags_metadata, execute_sample_sql\n'), ((1311, 1337), 'sqlmodel.select', 'select', (['Calendar.year_name'], {}), '(Calendar.year_name)\n', (1317, 1337), False, 'from sqlmodel import Session, select, SQLModel\n'), ((1769, 1818), 'csv.reader', 'csv.reader', (['csvfile'], {'delimiter': '""","""', 'quotechar': '"""|"""'}), "(csvfile, delimiter=',', quotechar='|')\n", (1779, 1818), False, 'import csv\n'), ((2193, 2216), 'backend.utils.sqlite3_engine.cursor', 'sqlite3_engine.cursor', ([], {}), '()\n', (2214, 2216), False, 'from backend.utils import engine, sqlite3_engine, create_db, tags_metadata, execute_sample_sql\n'), ((2265, 2288), 'backend.utils.sqlite3_engine.commit', 'sqlite3_engine.commit', ([], {}), '()\n', (2286, 2288), False, 'from backend.utils import engine, sqlite3_engine, create_db, tags_metadata, execute_sample_sql\n'), ((2301, 2323), 'backend.utils.sqlite3_engine.close', 'sqlite3_engine.close', ([], {}), '()\n', (2321, 2323), False, 'from backend.utils import engine, sqlite3_engine, create_db, tags_metadata, execute_sample_sql\n')]