"""
Defines the data structures for managing export profiles.
"""

from dataclasses import dataclass, field
from typing import List, Dict, Any

# Using slots for performance, similar to data_structures.py
DC_KWARGS = {"slots": True}


@dataclass(**DC_KWARGS)
class ExportField:
    """
    Represents a single data field to be exported to a CSV column.

    Attributes:
        column_name (str): The name of the column in the CSV file header.
        data_path (str): A dot-separated path to access the value within
                         the DataBatch object. E.g., "main_header.ge_header.mode.master_mode".
        translate_with_enum (bool): If True, the numeric value will be translated
                                    to its string representation using a corresponding Enum.
                                    Defaults to False.
    """

    column_name: str
    data_path: str
    translate_with_enum: bool = False


@dataclass(**DC_KWARGS)
class ExportProfile:
    """
    Represents a full export profile, including its name and the list of fields.
    """

    name: str
    fields: List[ExportField] = field(default_factory=list)

    def to_dict(self) -> Dict[str, Any]:
        """Converts the profile to a dictionary for JSON serialization."""
        return {
            "name": self.name,
            "fields": [
                {
                    "column_name": f.column_name,
                    "data_path": f.data_path,
                    "translate_with_enum": f.translate_with_enum,
                }
                for f in self.fields
            ],
        }

    @staticmethod
    def from_dict(data: Dict[str, Any]) -> "ExportProfile":
        """Creates an ExportProfile instance from a dictionary."""
        name = data.get("name", "Unnamed Profile")
        fields_data = data.get("fields", [])

        fields = []
        for field_data in fields_data:
            # Ensure backward compatibility with old configs that don't have the flag
            translate_flag = field_data.get("translate_with_enum", False)
            fields.append(
                ExportField(
                    column_name=field_data.get("column_name", ""),
                    data_path=field_data.get("data_path", ""),
                    translate_with_enum=translate_flag,
                )
            )
        return ExportProfile(name=name, fields=fields)
