from datetime import date
from typing import Optional, Iterator

from sqlmodel import SQLModel, Field, create_engine, Session  # type: ignore[import]

from my_feed.log import logger
from app.settings import settings


# base non-table sql model
class FeedBase(SQLModel):  # type: ignore[misc]
    model_id: str
    ftype: str  # feed item type
    title: str
    score: Optional[float] = Field(default=None)

    # more metadata
    subtitle: Optional[str] = Field(default=None)
    creator: Optional[str] = Field(default=None)
    part: Optional[int] = Field(default=None)
    subpart: Optional[int] = Field(default=None)
    collection: Optional[str] = Field(default=None)

    # dates
    when: int
    release_date: Optional[date] = Field(default=None)

    # urls
    image_url: Optional[str] = Field(default=None)
    url: Optional[str] = Field(default=None)


# feedbase, with an ID/table
class FeedModel(FeedBase, table=True):  # type: ignore
    id: int = Field(index=True, primary_key=True)

    # store JSON as strings, these are only used on the frontend anyways
    tags: str = Field(default=r"[]")  # List[str]
    data: Optional[bytes] = Field(default=None)  # Dict[str, Any]


feed_engine = create_engine(
    settings.SQLITE_DB_PATH,
    echo=settings.SQL_ECHO,
)


def init_db() -> None:
    logger.info("Creating tables...")
    SQLModel.metadata.create_all(feed_engine)


def get_db() -> Iterator[Session]:
    with Session(feed_engine) as session:
        yield session

sqlmodel.create_engine sqlmodel.Session sqlmodel.SQLModel.metadata.create_all sqlmodel.Field my_feed.log.logger.info
[((1207, 1269), 'sqlmodel.create_engine', 'create_engine', (['settings.SQLITE_DB_PATH'], {'echo': 'settings.SQL_ECHO'}), '(settings.SQLITE_DB_PATH, echo=settings.SQL_ECHO)\n', (1220, 1269), False, 'from sqlmodel import SQLModel, Field, create_engine, Session\n'), ((388, 407), 'sqlmodel.Field', 'Field', ([], {'default': 'None'}), '(default=None)\n', (393, 407), False, 'from sqlmodel import SQLModel, Field, create_engine, Session\n'), ((459, 478), 'sqlmodel.Field', 'Field', ([], {'default': 'None'}), '(default=None)\n', (464, 478), False, 'from sqlmodel import SQLModel, Field, create_engine, Session\n'), ((508, 527), 'sqlmodel.Field', 'Field', ([], {'default': 'None'}), '(default=None)\n', (513, 527), False, 'from sqlmodel import SQLModel, Field, create_engine, Session\n'), ((554, 573), 'sqlmodel.Field', 'Field', ([], {'default': 'None'}), '(default=None)\n', (559, 573), False, 'from sqlmodel import SQLModel, Field, create_engine, Session\n'), ((603, 622), 'sqlmodel.Field', 'Field', ([], {'default': 'None'}), '(default=None)\n', (608, 622), False, 'from sqlmodel import SQLModel, Field, create_engine, Session\n'), ((655, 674), 'sqlmodel.Field', 'Field', ([], {'default': 'None'}), '(default=None)\n', (660, 674), False, 'from sqlmodel import SQLModel, Field, create_engine, Session\n'), ((737, 756), 'sqlmodel.Field', 'Field', ([], {'default': 'None'}), '(default=None)\n', (742, 756), False, 'from sqlmodel import SQLModel, Field, create_engine, Session\n'), ((800, 819), 'sqlmodel.Field', 'Field', ([], {'default': 'None'}), '(default=None)\n', (805, 819), False, 'from sqlmodel import SQLModel, Field, create_engine, Session\n'), ((845, 864), 'sqlmodel.Field', 'Field', ([], {'default': 'None'}), '(default=None)\n', (850, 864), False, 'from sqlmodel import SQLModel, Field, create_engine, Session\n'), ((965, 1000), 'sqlmodel.Field', 'Field', ([], {'index': '(True)', 'primary_key': '(True)'}), '(index=True, primary_key=True)\n', (970, 1000), False, 'from sqlmodel import SQLModel, Field, create_engine, Session\n'), ((1091, 1110), 'sqlmodel.Field', 'Field', ([], {'default': '"""[]"""'}), "(default='[]')\n", (1096, 1110), False, 'from sqlmodel import SQLModel, Field, create_engine, Session\n'), ((1153, 1172), 'sqlmodel.Field', 'Field', ([], {'default': 'None'}), '(default=None)\n', (1158, 1172), False, 'from sqlmodel import SQLModel, Field, create_engine, Session\n'), ((1310, 1343), 'my_feed.log.logger.info', 'logger.info', (['"""Creating tables..."""'], {}), "('Creating tables...')\n", (1321, 1343), False, 'from my_feed.log import logger\n'), ((1348, 1389), 'sqlmodel.SQLModel.metadata.create_all', 'SQLModel.metadata.create_all', (['feed_engine'], {}), '(feed_engine)\n', (1376, 1389), False, 'from sqlmodel import SQLModel, Field, create_engine, Session\n'), ((1436, 1456), 'sqlmodel.Session', 'Session', (['feed_engine'], {}), '(feed_engine)\n', (1443, 1456), False, 'from sqlmodel import SQLModel, Field, create_engine, Session\n')]