from pydantic import BaseModel

from .server_config import ServerConfig
from .server_utils import Languages

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

class ServerErrorMessage(ServerMessageBase):
    type: str = "error"
    error_message: str

class AvailableSessionPropertiesServerMessage(ServerMessageBase):
    type: str = "available_session_properties"
    supported_languages: list[str]  # list of language codes, e.g., ["en", "de"]
    min_grid_size: int
    max_grid_size: int
    board_size_presets: dict[str, tuple[int, int]]  # mapping from preset

class SessionCreatedServerMessage(ServerMessageBase):
    type: str = "session_created"
    session_id: str

class SendFullSessionStateServerMessage(ServerMessageBase):
    type: str = "full_session_state"
    session_id: str
    grid: list[list[str]]  # 2D array representing the current grid state
    clues_across: dict[str, str]  # mapping from clue number to clue text for across clues
    clues_down: dict[str, str]    # mapping from clue number to clue text for down clues
    clue_positions_across: dict[str, tuple[int, int]]  # mapping from clue number to its (row, col) position
    clue_positions_down: dict[str, tuple[int, int]]    # mapping from clue number to its (row, col) position
    solved_positions: list[tuple[int, int]]  # list of (row, col) positions that are solved
    solution_word_positions: list[tuple[int, int]]  # list of (row, col) positions that are part of solution word

class LetterUpdateBroadcastServerMessage(ServerMessageBase):
    type: str = "letter_update"
    session_id: str
    row: int
    col: int
    letter: str  # single character string, uppercase A-Z or empty string for clearing the cell
    is_solved: bool
