# Lettura dati con SQLModel, all, first, one; update; delete
# https://sqlmodel.tiangolo.com/tutorial/select/ e seguito
from typing import Optional
from sqlmodel import Field, SQLModel, Session, create_engine, select


class Tag(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    name: str


class ProductType(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    name: str


sqlite_file_name = "database.db"
sqlite_url = f"sqlite:///{sqlite_file_name}"
engine = create_engine(sqlite_url, echo=True)


def create_db_and_tables():
    SQLModel.metadata.create_all(engine)


def create_entities():
    tag_offerta = Tag(name="Offerta")
    tag_maionese = Tag(name="Con Maionese")
    tipo_panino = ProductType(name="panino")
    tipo_bibita = ProductType(name="bibita")
    with Session(engine) as session:
        session.add(tag_offerta)
        session.add(tag_maionese)
        session.add(tipo_panino)
        session.add(tipo_bibita)
        session.commit()
        print("After committing the session")
        print("Tag 1:", tag_offerta)
        # No refresh, no print
        print("Product Type 1:", tipo_panino)
        # Refresh automatica
        print("Product Type 1:", tipo_panino.name)
        # Refresh esplicita
        session.refresh(tipo_bibita)
        session.refresh(tag_maionese)
        print("Product Type 2:", tipo_bibita)
    print("After the session closes")
    print("Tag 2:", tag_maionese)


def select_product_types():
    with Session(engine) as session:
        statement = select(ProductType)
        results = session.exec(statement)
        for product_type in results:
            print("product_type:", product_type)


def select_product_type_panino():
    with Session(engine) as session:
        statement = select(ProductType).where(ProductType.name == 'panino')
        results = session.exec(statement)
        for product_type in results:
            print("panino:", product_type)


def select_first_row_tag():
    with Session(engine) as session:
        statement = 