"""Configuration and constants for cheatly."""

from dataclasses import dataclass
from pathlib import Path
import shutil


@dataclass(frozen=True)
class LayoutSpec:
    """Single source of truth for panel layout constraints.

    All dimensions and constraints for panel layout are defined here.
    This ensures consistency across rendering, validation, and models.
    """

    panel_width: int = 40
    border_chars: int = 2

    @property
    def usable_width(self) -> int:
        """Width available for content after borders."""
        return self.panel_width - self.border_chars

    full_panel_items: int = 10
    half_panel_items: int = 4

    full_panel_height: int = 12
    half_panel_height: int = 6

    min_key_desc_gap: int = 2
    cmd_line_indent: int = 2


@dataclass(frozen=True)
class CodeLayoutSpec:
    """Single source of truth for code layout constraints.

    All constraints for code display and validation are defined here.
    """

    max_line_width: int = 100
    small_max_lines: int = 10
    medium_max_lines: int = 20
    large_max_lines: int = 40

    min_display_width: int = 60

    def get_max_lines(self, size: str) -> int:
        """Get max lines for a size category."""
        return {
            "small": self.small_max_lines,
            "medium": self.medium_max_lines,
            "large": self.large_max_lines,
        }.get(size, self.medium_max_lines)


class Config:
    """Central configuration for cheatly."""

    LAYOUT = LayoutSpec()
    CODE_LAYOUT = CodeLayoutSpec()

    PACKAGE_DIR = Path(__file__).parent
    SHEETS_DIR = PACKAGE_DIR / "sheets"

    DEFAULT_MIN_WIDTH = LAYOUT.panel_width
    SIDEBAR_WIDTH = LAYOUT.panel_width
    STRICT_MODE = True

    @staticmethod
    def terminal_width() -> int:
        """Get current terminal width.

        Returns:
            Terminal width in columns
        """
        return shutil.get_terminal_size().columns

    @staticmethod
    def terminal_height() -> int:
        """Get current terminal height.

        Returns:
            Terminal height in lines
        """
        return shutil.get_terminal_size().lines
