"""Unified export service - shared between CLI and HTTP API. This module provides export functionality for listings in various formats. """ from dataclasses import dataclass from pathlib import Path from typing import Any from models.listing import QueryParameters from repositories.listing_repository import ListingRepository @dataclass class ExportResult: """Result of an export operation.""" success: bool output_path: str | None # For file exports data: Any | None # For in-memory exports (GeoJSON) record_count: int message: str async def export_to_csv( repository: ListingRepository, output_path: Path, query_parameters: QueryParameters | None = None, ) -> ExportResult: """Export listings to CSV file. Used by: - CLI: export-csv - API: (could be added as download endpoint) """ from csv_exporter import export_to_csv as _export_csv await _export_csv(repository, output_path, query_parameters) listings = await repository.get_listings(query_parameters=query_parameters) return ExportResult( success=True, output_path=str(output_path), data=None, record_count=len(listings), message=f"Exported {len(listings)} listings to {output_path}", ) async def export_to_geojson( repository: ListingRepository, query_parameters: QueryParameters | None = None, output_path: Path | None = None, limit: int | None = None, ) -> ExportResult: """Export listings to GeoJSON format. Args: repository: Database repository query_parameters: Filtering parameters output_path: If provided, write to file. Otherwise return data. limit: Maximum number of listings to export Used by: - CLI: export-immoweb - API: GET /api/listing_geojson """ from ui_exporter import export_immoweb geojson_data = await export_immoweb( repository, output_file=str(output_path) if output_path else None, query_parameters=query_parameters, limit=limit, ) feature_count = len(geojson_data.get("features", [])) if geojson_data else 0 if output_path: return ExportResult( success=True, output_path=str(output_path), data=None, record_count=feature_count, message=f"Exported {feature_count} listings to {output_path}", ) return ExportResult( success=True, output_path=None, data=geojson_data, record_count=feature_count, message=f"Generated GeoJSON with {feature_count} features", )