# Info: KCL Desktop task schemas for provisioning (Provisioning)
# Author: Provisioning System
# Release: 1.0.0
# Date: 2025-07-25

import regex

schema User:
    """
    User settings for Desktop environment
    """
    name: str
    group: str = name
    home?: str = "/home/${name}"
    shell?: str = "/bin/bash"

schema DesktopEnvironment:
    """
    Desktop environment configuration
    """
    type: "xfce" | "gnome" | "kde" | "lxde" | "mate" = "xfce"
    display_manager: "lightdm" | "gdm" | "sddm" = "lightdm"
    resolution?: str = "1920x1080"
    theme?: str
    
schema Applications:
    """
    Applications to install with desktop
    """
    editors: [str] = ["zed", "nano", "vim"]
    browsers: [str] = ["firefox"]
    terminals: [str] = ["xfce4-terminal"]
    development: [str] = ["git", "build-essential"]
    media: [str] = ["vlc", "gimp"]
    office: [str] = ["libreoffice"]
    utilities: [str] = ["htop", "curl", "wget", "tree"]
    
schema Graphics:
    """
    Graphics and display configuration
    """
    driver: "nouveau" | "nvidia" | "amd" | "intel" = "intel"
    acceleration: bool = True
    compositing: bool = True

schema VNC:
    """
    VNC server configuration for remote desktop access
    """
    enabled: bool = True
    port: int = 5901
    password?: str
    geometry: str = "1920x1080"
    depth: int = 24
    
    check:
        port > 1024 and port < 65535, "VNC port must be between 1024 and 65535"
        depth in [8, 16, 24, 32], "VNC depth must be 8, 16, 24, or 32"

schema RustDesk:
    """
    RustDesk remote desktop configuration
    """
    enabled: bool = True
    port: int = 21116
    hbbr_port: int = 21117
    custom_server?: str
    password?: str
    permanent_password?: str
    allow_guest: bool = False
    auto_start: bool = True
    
    check:
        port > 1024 and port < 65535, "RustDesk port must be between 1024 and 65535"
        hbbr_port > 1024 and hbbr_port < 65535, "RustDesk hbbr port must be between 1024 and 65535"
        port != hbbr_port, "RustDesk ports must be different"

schema SSH:
    """
    SSH server configuration
    """
    enabled: bool = True
    port: int = 22
    password_auth: bool = True
    key_auth: bool = True
    root_login: "yes" | "no" | "prohibit-password" = "prohibit-password"
    max_auth_tries: int = 3
    client_alive_interval: int = 300
    client_alive_count_max: int = 2
    allowed_users?: [str]
    denied_users?: [str]
    
    check:
        port > 0 and port < 65535, "SSH port must be between 1 and 65535"
        max_auth_tries > 0, "Max auth tries must be positive"
        client_alive_interval >= 0, "Client alive interval must be non-negative"
        client_alive_count_max >= 0, "Client alive count max must be non-negative"

schema DesktopServer:
    """
    Desktop server configuration
    """
    name: str = "desktop"
    run_user: User = {
        name = "desktop"
    }
    desktop_env: DesktopEnvironment = {
        type = "xfce"
    }
    applications: Applications = {}
    graphics: Graphics = {}
    vnc: VNC = {
        enabled = True
    }
    rustdesk: RustDesk = {
        enabled = True
    }
    ssh: SSH = {
        enabled = True
    }
    auto_login: bool = False
    startup_script?: str
    
    check:
        len(run_user.name) > 0, "Check run_user name"
        vnc.enabled or rustdesk.enabled or desktop_env.display_manager != Undefined, "At least one remote access method required"