from typing import Optional

from sqlalchemy import UniqueConstraint
from sqlmodel import Field, Relationship, SQLModel

from db.base import BaseDBModel
from model.item import Item
from model.warehouse import Warehouse


class InventoryEditableFields(SQLModel):
    item_id: int = Field(foreign_key="item.id")
    warehouse_id: int = Field(foreign_key="warehouse.id")
    quantity: float


class Inventory(BaseDBModel, InventoryEditableFields, table=True):
    __table_args__ = (UniqueConstraint("id", "item_id", "warehouse_id"),)
    item: Optional[Item] = Relationship(back_populates="item_inventories")
    warehouse: Optional[Warehouse] = Relationship(back_populates="warehouse_inventories")


class InventoryRead(SQLModel):
    quantity: int
    item: Optional[Item] = None
    warehouse: Optional[Warehouse] = None

sqlmodel.Relationship sqlmodel.Field sqlalchemy.UniqueConstraint
[((281, 309), 'sqlmodel.Field', 'Field', ([], {'foreign_key': '"""item.id"""'}), "(foreign_key='item.id')\n", (286, 309), False, 'from sqlmodel import Field, Relationship, SQLModel\n'), ((334, 367), 'sqlmodel.Field', 'Field', ([], {'foreign_key': '"""warehouse.id"""'}), "(foreign_key='warehouse.id')\n", (339, 367), False, 'from sqlmodel import Field, Relationship, SQLModel\n'), ((558, 605), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""item_inventories"""'}), "(back_populates='item_inventories')\n", (570, 605), False, 'from sqlmodel import Field, Relationship, SQLModel\n'), ((643, 695), 'sqlmodel.Relationship', 'Relationship', ([], {'back_populates': '"""warehouse_inventories"""'}), "(back_populates='warehouse_inventories')\n", (655, 695), False, 'from sqlmodel import Field, Relationship, SQLModel\n'), ((479, 528), 'sqlalchemy.UniqueConstraint', 'UniqueConstraint', (['"""id"""', '"""item_id"""', '"""warehouse_id"""'], {}), "('id', 'item_id', 'warehouse_id')\n", (495, 528), False, 'from sqlalchemy import UniqueConstraint\n')]