from typing import List, Optional

from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select


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

    heroes: List["Hero"] = Relationship()


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

    team_id: Optional[int] = Field(default=None, foreign_key="team.id")
    team: Optional[Team] = Relationship()


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_heroes():
    with Session(engine) as session:
        team_preventers = Team(name="Preventers", headquarters="Sharp Tower")
        team_z_force = Team(name="Z-Force", headquarters="Sister Margaret’s Bar")

        hero_deadpond = Hero(
            name="Deadpond", secret_name="<NAME>", team=team_z_force
        )
        hero_rusty_man = Hero(
            name="Rusty-Man", secret_name="<NAME>", age=48, team=team_preventers
        )
        hero_spider_boy = Hero(name="Spider-Boy", secret_name="<NAME>")
        session.add(hero_deadpond)
        session.add(hero_rusty_man)
        session.add(hero_spider_boy)
        session.commit()

        session.refresh(hero_deadpond)
        session.refresh(hero_rusty_man)
        session.refresh(hero_spider_boy)

        print("Created hero:", hero_deadpond)
        print("Created hero:", hero_rusty_man)
        print("Created hero:", hero_spider_boy)

        hero_spider_boy.team = team_preventers
        session.add(hero_spider_boy)
        session.commit()
        session.refresh(hero_spider_boy)
        print("Updated hero:", hero_spider_boy)

        hero_black_lion = Hero(name="<NAME>", secret_name="<NAME>", age=35)
        hero_sure_e = Hero(name="<NAME>", secret_name="Sure-E")
        team_wakaland = Team(
            name="Wakaland",
            headquarters="Wakaland Capital City",
            heroes=[hero_black_lion, hero_sure_e],
        )
        session.add(team_wakaland)
        session.commit()
        session.refresh(team_wakaland)
        print("Team Wakaland:", team_wakaland)

        hero_tarantula = Hero(name="Tarantula", secret_name="<NAME>", age=32)
        hero_dr_weird = Hero(name="<NAME>", secret_name="<NAME>", age=36)
        hero_cap = Hero(
            name="Captain North America", secret_name="<NAME>", age=93
        )

        team_preventers.heroes.append(hero_tarantula)
        team_preventers.heroes.append(hero_dr_weird)
        team_preventers.heroes.append(hero_cap)
        session.add(team_preventers)
        session.commit()
        session.refresh(hero_tarantula)
        session.refresh(hero_dr_weird)
        session.refresh(hero_cap)
        print("Preventers new hero:", hero_tarantula)
        print("Preventers new hero:", hero_dr_weird)
        print("Preventers new hero:", hero_cap)


def select_heroes():
    with Session(engine) as session:
        statement = select(Team).where(Team.name == "Preventers")
        result = session.exec(statement)
        team_preventers = result.one()

        print("Preventers heroes:", team_preventers.heroes)


def update_heroes():
    with Session(engine) as session:
        hero_spider_boy = session.exec(
            select(Hero).where(Hero.name == "Spider-Boy")
        ).one()

        preventers_team = session.exec(
            select(Team).where(Team.name == "Preventers")
        ).one()

        print("Hero Spider-Boy:", hero_spider_boy)
        print("Preventers Team:", preventers_team)
        print("Preventers Team Heroes:", preventers_team.heroes)

        hero_spider_boy.team = None

        print("Spider-Boy without team:", hero_spider_boy)

        print("Preventers Team Heroes again:", preventers_team.heroes)

        session.add(hero_spider_boy)
        session.commit()
        print("After committing")

        session.refresh(hero_spider_boy)
        print("Spider-Boy after commit:", hero_spider_boy)

        print("Preventers Team Heroes after commit:", preventers_team.heroes)


def main():
    create_db_and_tables()
    create_heroes()
    select_heroes()
    update_heroes()


if __name__ == "__main__":
    main()

sqlmodel.Relationship sqlmodel.create_engine sqlmodel.Session sqlmodel.SQLModel.metadata.create_all sqlmodel.Field sqlmodel.select
[((730, 766), 'sqlmodel.create_engine', 'create_engine', (['sqlite_url'], {'echo': '(True)'}), '(sqlite_url, echo=True)\n', (743, 766), False, 'from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select\n'), ((178, 215), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'primary_key': '(True)'}), '(default=None, primary_key=True)\n', (183, 215), False, 'from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select\n'), ((232, 249), 'sqlmodel.Field', 'Field', ([], {'index': '(True)'}), '(index=True)\n', (237, 249), False, 'from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select\n'), ((300, 314), 'sqlmodel.Relationship', 'Relationship', ([], {}), '()\n', (312, 314), False, 'from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select\n'), ((375, 412), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'primary_key': '(True)'}), '(default=None, primary_key=True)\n', (380, 412), False, 'from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select\n'), ((429, 446), 'sqlmodel.Field', 'Field', ([], {'index': '(True)'}), '(index=True)\n', (434, 446), False, 'from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select\n'), ((493, 524), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'index': '(True)'}), '(default=None, index=True)\n', (498, 524), False, 'from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select\n'), ((555, 597), 'sqlmodel.Field', 'Field', ([], {'default': 'None', 'foreign_key': '"""team.id"""'}), "(default=None, foreign_key='team.id')\n", (560, 597), False, 'from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select\n'), ((625, 639), 'sqlmodel.Relationship', 'Relationship', ([], {}), '()\n', (637, 639), False, 'from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select\n'), ((801, 837), 'sqlmodel.SQLModel.metadata.create_all', 'SQLModel.metadata.create_all', (['engine'], {}), '(engine)\n', (829, 837), False, 'from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select\n'), ((870, 885), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (877, 885), False, 'from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select\n'), ((3191, 3206), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (3198, 3206), False, 'from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select\n'), ((3458, 3473), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (3465, 3473), False, 'from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select\n'), ((3239, 3251), 'sqlmodel.select', 'select', (['Team'], {}), '(Team)\n', (3245, 3251), False, 'from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select\n'), ((3538, 3550), 'sqlmodel.select', 'select', (['Hero'], {}), '(Hero)\n', (3544, 3550), False, 'from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select\n'), ((3653, 3665), 'sqlmodel.select', 'select', (['Team'], {}), '(Team)\n', (3659, 3665), False, 'from sqlmodel import Field, Relationship, Session, SQLModel, create_engine, select\n')]