import uuid

from sqlmodel import Field, SQLModel

from app.db.base import TenantModel


class TableShape:
    """Allowed table shapes."""

    ROUND = "round"
    SQUARE = "square"
    RECTANGLE = "rectangle"


class FloorTableBase(SQLModel):
    label: str = Field(nullable=False)
    x: float = Field(default=0.0, nullable=False)
    y: float = Field(default=0.0, nullable=False)
    width: float = Field(default=80.0, nullable=False)
    height: float = Field(default=80.0, nullable=False)
    shape: str = Field(default=TableShape.ROUND, nullable=False)
    rotation: float = Field(default=0.0, nullable=False)
    slot_count: int = Field(ge=1, default=4, nullable=False)


class FloorTable(FloorTableBase, TenantModel, table=True):
    """A table on the restaurant floor plan.

    Uses ``floor_table`` as the DB table name to avoid the SQL reserved word
    ``table``.  The rest of the codebase refers to this entity simply as
    "table" in route paths, schemas, and API responses.
    """

    __tablename__ = "floor_table"  # pyright: ignore[reportAssignmentType]

    id: str = Field(
        default_factory=lambda: str(uuid.uuid4()),
        primary_key=True,
    )
    zone_id: str = Field(
        foreign_key="zone.id",
        index=True,
        nullable=False,
    )


class FloorTableCreate(FloorTableBase):
    zone_id: str | None = None  # auto-created if omitted


class FloorTableRead(FloorTableBase):
    id: str
    restaurant_id: str
    zone_id: str


class FloorTableUpdate(FloorTableBase):
    zone_id: str | None = None
