"""DXF exporter for vector data."""

from __future__ import annotations

from pathlib import Path
from typing import Any

from qgis.core import (
    QgsCoordinateReferenceSystem,
    QgsFeature,
    QgsField,
    QgsFields,
    QgsVectorFileWriter,
    QgsWkbTypes,
)
from qgis.PyQt.QtCore import QMetaType

from sec_interp.core.utils import io as scu_io
from sec_interp.logger_config import get_logger

from .base_exporter import BaseExporter

logger = get_logger(__name__)


class DXFExporter(BaseExporter):
    """Exporter for DXF format."""

    def get_supported_extensions(self) -> list[str]:
        """Get supported vector format extensions."""
        return [".dxf"]

    def export(
        self,
        output_path: Path,
        features_data: list[dict[str, Any]],
        layer_name: str | None = None,
    ) -> bool:
        """Export features to DXF.

        Args:
            output_path: Output file path.
            features_data: List of dicts with 'geometry' and 'attributes' keys.
            layer_name: Optional conceptual layer name.

        Returns:
            True if export successful, False otherwise.

        """
        if not features_data:
            return False

        try:
            geometry_type = self.get_setting("geometry_type", QgsWkbTypes.LineString)
            crs = self.get_setting("crs", QgsCoordinateReferenceSystem("EPSG:4326"))
            symb_mode = self.get_setting(
                "symbology_export", QgsVectorFileWriter.NoSymbology
            )

            fields = self._prepare_fields(features_data)
            writer = scu_io.create_vector_writer(
                output_path,
                crs,
                fields,
                geometry_type,
                layer_name=layer_name,
                symbology_export=symb_mode,
            )

            if writer.hasError() != QgsVectorFileWriter.NoError:
                logger.error(f"Failed to create DXF writer: {writer.errorMessage()}")
                return False

            self._write_features(writer, features_data, fields)

            # Note: writer is closed when object is deleted or goes out of scope
            del writer

        except Exception:
            logger.exception(f"DXF export failed for {output_path}")
            return False
        else:
            return True

    def _write_features(
        self,
        writer: QgsVectorFileWriter,
        features_data: list[dict[str, Any]],
        fields: QgsFields,
    ) -> None:
        """Process and write features to the vector writer.

        Args:
            writer: The QGIS vector file writer instance.
            features_data: List of feature data dictionaries.
            fields: The QGIS field collection for the layer.

        """
        for data in features_data:
            feature = QgsFeature(fields)
            if "geometry" in data:
                feature.setGeometry(data["geometry"])
            if "attributes" in data:
                attrs = data["attributes"]
                feature.setAttributes([attrs.get(field.name()) for field in fields])
            writer.addFeature(feature)

    def _prepare_fields(self, features_data: list[dict[str, Any]]) -> QgsFields:
        """Create fields based on first feature's attributes."""
        fields = QgsFields()
        if not features_data or not isinstance(features_data, list):
            return fields

        first_item = features_data[0]
        if not isinstance(first_item, dict) or "attributes" not in first_item:
            return fields

        first_attrs = first_item["attributes"]
        for key, value in first_attrs.items():
            if isinstance(value, int):
                fields.append(QgsField(key, QMetaType.Type.Int))
            elif isinstance(value, float):
                fields.append(QgsField(key, QMetaType.Type.Double))
            else:
                fields.append(QgsField(key, QMetaType.Type.QString))
        return fields
