"""
Field Type Constants

Field type definitions for dynamic tables and forms.
"""

from enum import Enum
from typing import Dict, Any, List


class FIELD_TYPES:
    """Field Type Identifiers."""
    
    # Basic Types
    TEXT = "text"
    TEXTAREA = "textarea"
    NUMBER = "number"
    DECIMAL = "decimal"
    BOOLEAN = "boolean"
    
    # Date & Time
    DATE = "date"
    TIME = "time"
    DATETIME = "datetime"
    
    # Selection
    SELECT = "select"
    MULTI_SELECT = "multi_select"
    RADIO = "radio"
    CHECKBOX = "checkbox"
    
    # References
    RELATION = "relation"
    LOOKUP = "lookup"
    ROLLUP = "rollup"
    FORMULA = "formula"
    
    # Rich Content
    RICH_TEXT = "rich_text"
    MARKDOWN = "markdown"
    CODE = "code"
    
    # Files
    FILE = "file"
    IMAGE = "image"
    ATTACHMENT = "attachment"
    
    # Contact
    EMAIL = "email"
    PHONE = "phone"
    URL = "url"
    
    # Location
    ADDRESS = "address"
    LOCATION = "location"
    
    # Advanced
    JSON = "json"
    RATING = "rating"
    PROGRESS = "progress"
    CURRENCY = "currency"
    BARCODE = "barcode"
    
    # System
    AUTO_NUMBER = "auto_number"
    CREATED_AT = "created_at"
    UPDATED_AT = "updated_at"
    CREATED_BY = "created_by"
    UPDATED_BY = "updated_by"


# Field type configurations
FIELD_CONFIGS: Dict[str, Dict[str, Any]] = {
    FIELD_TYPES.TEXT: {
        "label": "Text",
        "icon": "text",
        "category": "basic",
        "default_options": {
            "max_length": 255,
            "placeholder": "",
        },
        "validators": ["max_length"],
    },
    FIELD_TYPES.TEXTAREA: {
        "label": "Long Text",
        "icon": "text-align-left",
        "category": "basic",
        "default_options": {
            "max_length": 5000,
            "rows": 4,
        },
        "validators": ["max_length"],
    },
    FIELD_TYPES.NUMBER: {
        "label": "Number",
        "icon": "hash",
        "category": "basic",
        "default_options": {
            "min": None,
            "max": None,
            "allow_negative": True,
        },
        "validators": ["min", "max"],
    },
    FIELD_TYPES.DECIMAL: {
        "label": "Decimal",
        "icon": "decimal",
        "category": "basic",
        "default_options": {
            "precision": 2,
            "min": None,
            "max": None,
        },
        "validators": ["min", "max", "precision"],
    },
    FIELD_TYPES.BOOLEAN: {
        "label": "Checkbox",
        "icon": "checkbox",
        "category": "basic",
        "default_options": {
            "default_value": False,
        },
        "validators": [],
    },
    FIELD_TYPES.DATE: {
        "label": "Date",
        "icon": "calendar",
        "category": "datetime",
        "default_options": {
            "format": "YYYY-MM-DD",
            "include_time": False,
        },
        "validators": ["date_range"],
    },
    FIELD_TYPES.DATETIME: {
        "label": "Date & Time",
        "icon": "calendar-clock",
        "category": "datetime",
        "default_options": {
            "format": "YYYY-MM-DD HH:mm",
            "timezone": "UTC",
        },
        "validators": ["date_range"],
    },
    FIELD_TYPES.SELECT: {
        "label": "Single Select",
        "icon": "list",
        "category": "selection",
        "default_options": {
            "options": [],
            "allow_new": False,
        },
        "validators": ["in_options"],
    },
    FIELD_TYPES.MULTI_SELECT: {
        "label": "Multi Select",
        "icon": "list-check",
        "category": "selection",
        "default_options": {
            "options": [],
            "max_selections": None,
        },
        "validators": ["in_options", "max_selections"],
    },
    FIELD_TYPES.RELATION: {
        "label": "Link to Record",
        "icon": "link",
        "category": "reference",
        "default_options": {
            "target_table": None,
            "multiple": False,
        },
        "validators": ["valid_relation"],
    },
    FIELD_TYPES.FILE: {
        "label": "File",
        "icon": "file",
        "category": "file",
        "default_options": {
            "allowed_types": [],
            "max_size_mb": 10,
            "multiple": False,
        },
        "validators": ["file_type", "file_size"],
    },
    FIELD_TYPES.EMAIL: {
        "label": "Email",
        "icon": "mail",
        "category": "contact",
        "default_options": {},
        "validators": ["email"],
    },
    FIELD_TYPES.URL: {
        "label": "URL",
        "icon": "link-external",
        "category": "contact",
        "default_options": {},
        "validators": ["url"],
    },
    FIELD_TYPES.CURRENCY: {
        "label": "Currency",
        "icon": "currency",
        "category": "advanced",
        "default_options": {
            "currency": "USD",
            "precision": 2,
        },
        "validators": ["min", "max", "precision"],
    },
    FIELD_TYPES.RATING: {
        "label": "Rating",
        "icon": "star",
        "category": "advanced",
        "default_options": {
            "max": 5,
            "icon": "star",
        },
        "validators": ["min", "max"],
    },
    FIELD_TYPES.FORMULA: {
        "label": "Formula",
        "icon": "function",
        "category": "computed",
        "default_options": {
            "formula": "",
            "result_type": "text",
        },
        "validators": ["formula_syntax"],
    },
}


# Field categories
FIELD_CATEGORIES: List[Dict[str, Any]] = [
    {"id": "basic", "label": "Basic", "order": 1},
    {"id": "datetime", "label": "Date & Time", "order": 2},
    {"id": "selection", "label": "Selection", "order": 3},
    {"id": "reference", "label": "References", "order": 4},
    {"id": "file", "label": "Files", "order": 5},
    {"id": "contact", "label": "Contact", "order": 6},
    {"id": "advanced", "label": "Advanced", "order": 7},
    {"id": "computed", "label": "Computed", "order": 8},
    {"id": "system", "label": "System", "order": 9},
]
