from pydantic import BaseModel

from .server_config import ServerConfig
from .server_utils import Languages, BoardSizePreset

class ClientMessageBase(BaseModel):
    @classmethod
    def get_type(cls) -> str:
        return cls.model_fields["type"].default
    type: str

class RequestAvailableSessionPropertiesClientMessage(ClientMessageBase):
    type: str = "get_available_session_properties"


class NewMultiplayerSessionClientMessage(ClientMessageBase):

    type: str = "new_multiplayer_session"
    lang: Languages | str
    # grid_w can be between 10 and 30
    grid_w: int
    # grid_h can be between 10 and 30
    grid_h: int
    
    # verify that grid_w and grid_h are within bounds
    def validate(self):
        server_config = ServerConfig.get_config()
        if not (server_config.MIN_GRID_SIZE <= self.grid_w <= server_config.MAX_GRID_SIZE):
            raise ValueError(f"grid_w must be between {server_config.MIN_GRID_SIZE} and {server_config.MAX_GRID_SIZE}")
        if not (server_config.MIN_GRID_SIZE <= self.grid_h <= server_config.MAX_GRID_SIZE):
            raise ValueError(f"grid_h must be between {server_config.MIN_GRID_SIZE} and {server_config.MAX_GRID_SIZE}")

class SubscribeSessionClientMessage(ClientMessageBase):
    type: str = "subscribe_session"
    session_id: str

class UpdateLetterClientMessage(ClientMessageBase):
    type: str = "update_letter"
    session_id: str
    row: int
    col: int
    letter: str  # single character string, uppercase A-Z or empty string for clearing the cell