import os
from datetime import datetime
from typing import List, Optional

from sqlmodel import (
    Column,
    DateTime,
    Field,
    Relationship,
    Session,
    SQLModel,
    create_engine,
    select,
)


class Block(SQLModel, table=True):
    id: int = Field(primary_key=True)
    chain_id: int
    height: int
    timestamp: datetime = Field(sa_column=Column(DateTime(timezone=True)))
    snapshot: Optional[datetime] = Field(sa_column=Column(DateTime(timezone=True)))

    snapshots: List["Snapshot"] = Relationship(back_populates="block")


class Snapshot(SQLModel, table=True):
    id: int = Field(primary_key=True)
    product: str
    name: str
    assets: float

    block_id: int = Field(foreign_key="block.id")
    block: Block = Relationship(back_populates="snapshots")


pguser = os.environ.get('PGUSER', 'postgres')
pgpassword = os.environ.get('PGPASSWORD', '<PASSWORD>')
pghost = os.environ.get('PGHOST', 'localhost')
pgdatabase = os.environ.get('PGDATABASE', 'yearn')

dsn = f'postgresql://{pguser}:{pgpassword}@{pghost}:5432/{pgdatabase}'
engine = create_engine(dsn, echo=False)

# SQLModel.metadata.drop_all(engine)
SQLModel.metadata.create_all(engine)

sqlmodel.SQLModel.metadata.create_all sqlmodel.Field sqlmodel.create_engine os.environ.get sqlmodel.DateTime sqlmodel.Relationship
[((802, 838), 'os.environ.get', 'os.environ.get', (['"""PGUSER"""', '"""postgres"""'], {}), "('PGUSER', 'postgres')\n", (816, 838), False, 'import os\n'), ((852, 894), 'os.environ.get', 'os.environ.get', (['"""PGPASSWORD"""', '"""<PASSWORD>"""'], {}), "('PGPASSWORD', '<PASSWORD>')\n", (866, 894), False, 'import os\n'), ((904, 941), 'os.environ.get', 'os.environ.get', (['"""PGHOST"""', '"""localhost"""'], {}), "('PGHOST', 'localhost')\n", (918, 941), False, 'import os\n'), ((955, 992), 'os.environ.get', 'os.environ.get', (['"""PGDATABASE"""', '"""yearn"""'], {}), "('PGDATABASE', 'yearn')\n", (969, 992), False, 'import os\n'), ((1074, 1104), 'sqlmodel.create_engine', 'create_engine', (['dsn'], {'echo': '(False)'}), '(dsn, echo=False)\n', (1087, 1104), False, 'from sqlmodel import Column, DateTime, Field, Relationship, Session, SQLModel, create_engine, select\n'), ((1143, 1179), 'sqlmodel.SQLModel.metadata.create_all', 'SQLModel.metadata.create_all', (['engine'], {}), '(engine)\n', (1171, 1179), False, 'from sqlmodel import Column, DateTime, Field, Relationship, Session, SQLModel, create_engine, select\n'), ((264, 287), 'sqlmodel.Field', 'Field', ([], {'primary_key': '(True)'}), '(primary_key=True)\n', (269, 287), False, 'from sqlmodel import Column, DateTime, Field, Relationship, Session, SQLModel, create_engine, select\n'), ((516, 552), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""block"""'}), "(back_populates='block')\n", (528, 552), False, 'from sqlmodel import Column, DateTime, Field, Relationship, Session, SQLModel, create_engine, select\n'), ((607, 630), 'sqlmodel.Field', 'Field', ([], {'primary_key': '(True)'}), '(primary_key=True)\n', (612, 630), False, 'from sqlmodel import Column, DateTime, Field, Relationship, Session, SQLModel, create_engine, select\n'), ((701, 730), 'sqlmodel.Field', 'Field', ([], {'foreign_key': '"""block.id"""'}), "(foreign_key='block.id')\n", (706, 730), False, 'from sqlmodel import Column, DateTime, Field, Relationship, Session, SQLModel, create_engine, select\n'), ((750, 790), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""snapshots"""'}), "(back_populates='snapshots')\n", (762, 790), False, 'from sqlmodel import Column, DateTime, Field, Relationship, Session, SQLModel, create_engine, select\n'), ((371, 394), 'sqlmodel.DateTime', 'DateTime', ([], {'timezone': '(True)'}), '(timezone=True)\n', (379, 394), False, 'from sqlmodel import Column, DateTime, Field, Relationship, Session, SQLModel, create_engine, select\n'), ((455, 478), 'sqlmodel.DateTime', 'DateTime', ([], {'timezone': '(True)'}), '(timezone=True)\n', (463, 478), False, 'from sqlmodel import Column, DateTime, Field, Relationship, Session, SQLModel, create_engine, select\n')]