import logging
from datetime import datetime
from typing import List

from sqlmodel import Session, select

from db import engine
from models import Social, User


def get_last_social() -> Social:
    with Session(engine) as session:
        statement = select(Social).order_by(Social.id.desc()).limit(1)
        result = session.exec(statement).one_or_none()
    logging.info(f"SELECT social row: {result}")
    return result


def get_previous_social() -> Social:
    with Session(engine) as session:
        statement = select(Social).order_by(Social.id.desc()).offset(1).limit(1)
        result = session.exec(statement).one_or_none()
    logging.info(f"SELECT previous social row: {result}")
    return result


def create_social(fb: int, ig: int, tw: int, sp: int, yt: int):
    dt_now = datetime.now().strftime("%Y%m%d_%H%M%S")
    logging.info(f"INSERT social row ({dt_now},{fb},{ig},{tw},{sp},{yt})")
    social_row = Social(dt=dt_now, fb=fb, ig=ig, tw=tw, sp=sp, yt=yt)
    with Session(engine) as session:
        session.add(social_row)
        session.commit()


def create_user(telegram_id: int, username: str = None, first_name: str = None, last_name: str = None):
    logging.info(f"INSERT user: {first_name}")
    user_row = User(telegram_id=telegram_id, username=username, first_name=first_name, last_name=last_name)
    with Session(engine) as session:
        session.add(user_row)
        session.commit()


def get_user(telegram_id: int) -> User:
    with Session(engine) as session:
        statement = select(User).where(User.telegram_id == telegram_id)
        result = session.exec(statement).one_or_none()
    logging.info(f"SELECT user: {result}")
    return result


def get_all_users() -> List[User]:
    with Session(engine) as session:
        statement = select(User)
        result = session.exec(statement).fetchall()
    logging.info(f"SELECT all users: {result}")
    return result

models.User models.Social datetime.datetime.now models.Social.id.desc sqlmodel.select sqlmodel.Session logging.info
[((364, 408), 'logging.info', 'logging.info', (['f"""SELECT social row: {result}"""'], {}), "(f'SELECT social row: {result}')\n", (376, 408), False, 'import logging\n'), ((643, 696), 'logging.info', 'logging.info', (['f"""SELECT previous social row: {result}"""'], {}), "(f'SELECT previous social row: {result}')\n", (655, 696), False, 'import logging\n'), ((839, 909), 'logging.info', 'logging.info', (['f"""INSERT social row ({dt_now},{fb},{ig},{tw},{sp},{yt})"""'], {}), "(f'INSERT social row ({dt_now},{fb},{ig},{tw},{sp},{yt})')\n", (851, 909), False, 'import logging\n'), ((927, 979), 'models.Social', 'Social', ([], {'dt': 'dt_now', 'fb': 'fb', 'ig': 'ig', 'tw': 'tw', 'sp': 'sp', 'yt': 'yt'}), '(dt=dt_now, fb=fb, ig=ig, tw=tw, sp=sp, yt=yt)\n', (933, 979), False, 'from models import Social, User\n'), ((1184, 1226), 'logging.info', 'logging.info', (['f"""INSERT user: {first_name}"""'], {}), "(f'INSERT user: {first_name}')\n", (1196, 1226), False, 'import logging\n'), ((1242, 1338), 'models.User', 'User', ([], {'telegram_id': 'telegram_id', 'username': 'username', 'first_name': 'first_name', 'last_name': 'last_name'}), '(telegram_id=telegram_id, username=username, first_name=first_name,\n    last_name=last_name)\n', (1246, 1338), False, 'from models import Social, User\n'), ((1637, 1675), 'logging.info', 'logging.info', (['f"""SELECT user: {result}"""'], {}), "(f'SELECT user: {result}')\n", (1649, 1675), False, 'import logging\n'), ((1857, 1900), 'logging.info', 'logging.info', (['f"""SELECT all users: {result}"""'], {}), "(f'SELECT all users: {result}')\n", (1869, 1900), False, 'import logging\n'), ((206, 221), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (213, 221), False, 'from sqlmodel import Session, select\n'), ((475, 490), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (482, 490), False, 'from sqlmodel import Session, select\n'), ((989, 1004), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (996, 1004), False, 'from sqlmodel import Session, select\n'), ((1344, 1359), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (1351, 1359), False, 'from sqlmodel import Session, select\n'), ((1478, 1493), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (1485, 1493), False, 'from sqlmodel import Session, select\n'), ((1740, 1755), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (1747, 1755), False, 'from sqlmodel import Session, select\n'), ((1788, 1800), 'sqlmodel.select', 'select', (['User'], {}), '(User)\n', (1794, 1800), False, 'from sqlmodel import Session, select\n'), ((794, 808), 'datetime.datetime.now', 'datetime.now', ([], {}), '()\n', (806, 808), False, 'from datetime import datetime\n'), ((1526, 1538), 'sqlmodel.select', 'select', (['User'], {}), '(User)\n', (1532, 1538), False, 'from sqlmodel import Session, select\n'), ((278, 294), 'models.Social.id.desc', 'Social.id.desc', ([], {}), '()\n', (292, 294), False, 'from models import Social, User\n'), ((254, 268), 'sqlmodel.select', 'select', (['Social'], {}), '(Social)\n', (260, 268), False, 'from sqlmodel import Session, select\n'), ((547, 563), 'models.Social.id.desc', 'Social.id.desc', ([], {}), '()\n', (561, 563), False, 'from models import Social, User\n'), ((523, 537), 'sqlmodel.select', 'select', (['Social'], {}), '(Social)\n', (529, 537), False, 'from sqlmodel import Session, select\n')]