from dataclasses import dataclass, field
from enum import Enum
from typing import Optional, Any, Type, List


@dataclass
class FieldMeta:
    type: Any
    required: bool
    readonly: bool
    default: Any
    description: Optional[str] = None
    primary_key: Optional[bool] = False
    enum_class: Optional[Type] = None

    def to_dict(self) -> dict:
        d = {
            "type": self.type,
            "required": self.required,
            "readonly": self.readonly,
            "default": self.default,
            "description": self.description,
            "primary_key": self.primary_key,
        }
        if self.enum_class is not None and issubclass(self.enum_class, Enum):
            d["type"] = "enum"
            d["choices"] = [{"value": m.value, "label": m.name} for m in self.enum_class]
        return d
