from pydantic import BaseModel, ConfigDict, Field, field_validator


class TableBase(BaseModel):
    room_id: int
    name: str = Field(min_length=1, max_length=80)
    x: float = Field(ge=0)
    y: float = Field(ge=0)
    width: float = Field(gt=0)
    height: float = Field(gt=0)
    shape: str = Field(default="square", pattern="^(square|round)$")
    rotation_degrees: float = Field(default=0, ge=0, le=359.9)
    min_seats: int = Field(gt=0)
    max_seats: int = Field(gt=0)
    join_group: str | None = Field(default=None, max_length=80)
    is_active: bool = True

    @field_validator("max_seats")
    @classmethod
    def validate_seats(cls, value: int, info) -> int:
        min_seats = info.data.get("min_seats")
        if min_seats is not None and value < min_seats:
            raise ValueError("max_seats deve essere maggiore o uguale a min_seats")
        return value


class TableCreate(TableBase):
    pass


class TableUpdate(TableBase):
    pass


class TableRead(TableBase):
    id: int

    model_config = ConfigDict(from_attributes=True)


class TableCombinationRead(BaseModel):
    id: int
    room_id: int
    name: str
    table_ids: list[int]
    min_seats: int
    max_seats: int
    is_active: bool

    model_config = ConfigDict(from_attributes=True)
