# app/modules/marketplace/tasks/export_tasks.py
"""
Celery tasks for product export operations.

Handles exporting store products to various formats (e.g., Letzshop CSV).
"""

import logging
from datetime import UTC, datetime
from pathlib import Path

from app.core.celery_config import celery_app
from app.modules.task_base import ModuleTask
from app.modules.tenancy.models import Store

logger = logging.getLogger(__name__)


@celery_app.task(
    bind=True,
    base=ModuleTask,
    name="app.modules.marketplace.tasks.export_tasks.export_store_products_to_folder",
    max_retries=3,
    default_retry_delay=60,
)
def export_store_products_to_folder(
    self,
    store_id: int,
    triggered_by: str,
    include_inactive: bool = False,
):
    """
    Export all 3 languages (en, fr, de) to disk asynchronously.

    Args:
        store_id: ID of the store to export
        triggered_by: User identifier who triggered the export
        include_inactive: Whether to include inactive products

    Returns:
        dict: Export results per language with file paths
    """
    from app.modules.marketplace.services.letzshop_export_service import (
        letzshop_export_service,
    )

    languages = ["en", "fr", "de"]
    results = {}
    export_dir = None

    with self.get_db() as db:
        # Get store info
        store = db.query(Store).filter(Store.id == store_id).first()
        if not store:
            logger.error(f"Store {store_id} not found for export")
            return {"error": f"Store {store_id} not found"}

        store_code = store.store_code

        # Create export directory
        export_dir = Path(f"exports/letzshop/{store_code}")
        export_dir.mkdir(parents=True, exist_ok=True)

        started_at = datetime.now(UTC)
        logger.info(f"Starting product export for store {store_code} (ID: {store_id})")

        for lang in languages:
            try:
                # Generate CSV content
                csv_content = letzshop_export_service.export_store_products(
                    db=db,
                    store_id=store_id,
                    language=lang,
                    include_inactive=include_inactive,
                )

                # Write to file
                file_name = f"{store_code}_products_{lang}.csv"
                file_path = export_dir / file_name

                with open(file_path, "w", encoding="utf-8") as f:
                    f.write(csv_content)

                results[lang] = {
                    "success": True,
                    "path": str(file_path),
                    "file_name": file_name,
                }

                logger.info(f"Exported {lang} products to {file_path}")

            except Exception as e:
                logger.error(f"Error exporting {lang} products for store {store_id}: {e}")
                results[lang] = {
                    "success": False,
                    "error": str(e),
                }

        # Log the export
        completed_at = datetime.now(UTC)
        duration = (completed_at - started_at).total_seconds()
        success_count = sum(1 for r in results.values() if r.get("success"))

        try:
            letzshop_export_service.log_export(
                db=db,
                store_id=store_id,
                triggered_by=triggered_by,
                records_processed=len(languages),
                records_succeeded=success_count,
                records_failed=len(languages) - success_count,
                duration_seconds=duration,
            )
        except Exception as e:
            logger.error(f"Failed to log export: {e}")

        logger.info(
            f"Product export complete for store {store_code}: "
            f"{success_count}/{len(languages)} languages exported in {duration:.2f}s"
        )

        return {
            "store_id": store_id,
            "store_code": store_code,
            "export_dir": str(export_dir),
            "results": results,
            "duration_seconds": duration,
            "triggered_by": triggered_by,
        }


@celery_app.task(
    bind=True,
    base=ModuleTask,
    name="app.modules.marketplace.tasks.export_tasks.export_marketplace_products",
    max_retries=3,
    default_retry_delay=60,
)
def export_marketplace_products(
    self,
    language: str = "en",
    triggered_by: str = "system",
):
    """
    Export all marketplace products (admin use).

    Args:
        language: Language code for export (en, fr, de)
        triggered_by: User identifier who triggered the export

    Returns:
        dict: Export result with file path
    """
    from app.modules.marketplace.services.letzshop_export_service import (
        letzshop_export_service,
    )

    with self.get_db() as db:
        started_at = datetime.now(UTC)
        logger.info(f"Starting marketplace product export ({language})")

        try:
            # Create export directory
            export_dir = Path("exports/marketplace")
            export_dir.mkdir(parents=True, exist_ok=True)

            # Generate CSV content
            csv_content = letzshop_export_service.export_marketplace_products(
                db=db,
                language=language,
            )

            # Write to file
            timestamp = started_at.strftime("%Y%m%d_%H%M%S")
            file_name = f"marketplace_products_{language}_{timestamp}.csv"
            file_path = export_dir / file_name

            with open(file_path, "w", encoding="utf-8") as f:
                f.write(csv_content)

            completed_at = datetime.now(UTC)
            duration = (completed_at - started_at).total_seconds()

            logger.info(f"Marketplace export complete: {file_path} ({duration:.2f}s)")

            return {
                "success": True,
                "path": str(file_path),
                "file_name": file_name,
                "language": language,
                "duration_seconds": duration,
                "triggered_by": triggered_by,
            }

        except Exception as e:
            logger.error(f"Error exporting marketplace products: {e}")
            return {
                "success": False,
                "error": str(e),
                "language": language,
            }
