# Copyright (c) 2025 Patrick Motsch
# All rights reserved.
"""
Global frontend type definitions for UI rendering.

This module provides a centralized definition of all frontend types used across the application,
including standard types (text, select, etc.) and custom types (userConnection, documentReference, etc.).

Custom types support dynamic option loading via API endpoints.
"""

from enum import Enum
from typing import Dict, Optional


class FrontendType(str, Enum):
    """
    Frontend rendering types for UI components.
    
    Standard Types:
    - Basic input types: text, textarea, number, checkbox, date, datetime, email, timestamp
    - Selection types: select, multiselect
    - Complex types: json, multilingual, file
    
    Custom Types (for Actions):
    - userConnection: User connection selector (dynamic options from /api/options/user.connection)
    - documentReference: Document reference selector (dynamic options from workflow context)
    - workflowAction: Workflow action selector (dynamic options from available actions)
    """
    
    # Standard Types
    TEXT = "text"
    TEXTAREA = "textarea"
    NUMBER = "number"
    CHECKBOX = "checkbox"
    DATE = "date"
    DATETIME = "datetime"
    EMAIL = "email"
    TIMESTAMP = "timestamp"
    SELECT = "select"
    MULTISELECT = "multiselect"
    JSON = "json"
    MULTILINGUAL = "multilingual"
    FILE = "file"
    HIDDEN = "hidden"
    
    # Custom Types for Actions
    USER_CONNECTION = "userConnection"
    """User connection selector - fetches connections for current user from /api/options/user.connection"""
    
    DOCUMENT_REFERENCE = "documentReference"
    """Document reference selector - fetches available document references from workflow context"""
    
    WORKFLOW_ACTION = "workflowAction"
    """Workflow action selector - fetches available actions from workflow context"""
    
    SHAREPOINT_FOLDER = "sharepointFolder"
    """SharePoint folder selector - requires connectionReference parameter in same action to load folders"""

    SHAREPOINT_FILE = "sharepointFile"
    """SharePoint file selector - requires connectionReference parameter"""

    CLICKUP_LIST = "clickupList"
    """ClickUp list selector - requires connectionReference parameter"""

    CLICKUP_TASK = "clickupTask"
    """ClickUp task selector - requires connectionReference parameter"""

    # Complex Structure Types (for graph editor node configs)
    CASE_LIST = "caseList"
    """Case list editor for flow.switch cases"""

    FIELD_BUILDER = "fieldBuilder"
    """Field builder for input.form field definitions"""

    KEY_VALUE_ROWS = "keyValueRows"
    """Key-value row editor for task update entries"""

    CRON = "cron"
    """Cron expression builder"""

    CONDITION = "condition"
    """Structured condition builder for flow.ifElse"""

    MAPPING_TABLE = "mappingTable"
    """Mapping table editor for data.transform"""

    FILTER_EXPRESSION = "filterExpression"
    """Filter expression builder for data.filter"""


# Mapping of custom types to their API endpoint for dynamic options
CUSTOM_TYPE_OPTIONS_API: Dict[FrontendType, str] = {
    FrontendType.USER_CONNECTION: "user.connection",
    FrontendType.DOCUMENT_REFERENCE: "workflow.documentReference",
    FrontendType.WORKFLOW_ACTION: "workflow.action",
    FrontendType.SHAREPOINT_FOLDER: "sharepoint.folder",
    FrontendType.SHAREPOINT_FILE: "sharepoint.file",
    FrontendType.CLICKUP_LIST: "clickup.list",
    FrontendType.CLICKUP_TASK: "clickup.task",
}

def getOptionsApiEndpoint(frontendType: FrontendType) -> Optional[str]:
    """
    Get the API endpoint for fetching dynamic options for a custom frontend type.
    
    Args:
        frontendType: The frontend type to get options API endpoint for
        
    Returns:
        API endpoint string (e.g., "user.connection") or None if not a custom type
    """
    return CUSTOM_TYPE_OPTIONS_API.get(frontendType)


def isCustomType(frontendType: FrontendType) -> bool:
    """
    Check if a frontend type is a custom type that requires dynamic options.
    
    Args:
        frontendType: The frontend type to check
        
    Returns:
        True if it's a custom type, False otherwise
    """
    return frontendType in CUSTOM_TYPE_OPTIONS_API

