# app/modules/marketplace/tasks/sync_tasks.py
"""
Celery tasks for Letzshop store directory synchronization.

Periodically syncs store information from Letzshop's public GraphQL API.
"""

import logging
from typing import Any

from app.core.celery_config import celery_app
from app.modules.marketplace.services.letzshop import LetzshopStoreSyncService
from app.modules.messaging.services.admin_notification_service import (
    admin_notification_service,
)
from app.modules.task_base import ModuleTask

logger = logging.getLogger(__name__)


@celery_app.task(
    bind=True,
    base=ModuleTask,
    name="app.modules.marketplace.tasks.sync_tasks.sync_store_directory",
    max_retries=2,
    default_retry_delay=300,
    autoretry_for=(Exception,),
    retry_backoff=True,
)
def sync_store_directory(self) -> dict[str, Any]:
    """
    Celery task to sync Letzshop store directory.

    Fetches all stores from Letzshop's public GraphQL API and updates
    the local letzshop_store_cache table.

    This task is scheduled to run daily via the module's scheduled_tasks
    definition.

    Returns:
        dict: Sync statistics including created, updated, and error counts.
    """
    with self.get_db() as db:
        try:
            logger.info("Starting Letzshop store directory sync...")

            sync_service = LetzshopStoreSyncService(db)

            def progress_callback(page: int, fetched: int, total: int):
                """Log progress during sync."""
                logger.info(f"Store sync progress: page {page}, {fetched}/{total} stores")

            stats = sync_service.sync_all_stores(progress_callback=progress_callback)

            logger.info(
                f"Store directory sync completed: "
                f"{stats.get('created', 0)} created, "
                f"{stats.get('updated', 0)} updated, "
                f"{stats.get('errors', 0)} errors"
            )

            # Send admin notification if there were errors
            if stats.get("errors", 0) > 0:
                admin_notification_service.notify_system_info(
                    db=db,
                    title="Letzshop Store Sync Completed with Errors",
                    message=(
                        f"Synced {stats.get('total_fetched', 0)} stores. "
                        f"Errors: {stats.get('errors', 0)}"
                    ),
                    details=stats,
                )

            return stats

        except Exception as e:
            logger.error(f"Store directory sync failed: {e}", exc_info=True)

            # Notify admins of failure
            admin_notification_service.notify_critical_error(
                db=db,
                error_type="Store Directory Sync",
                error_message=f"Failed to sync Letzshop store directory: {str(e)[:200]}",
                details={"error": str(e)},
            )
            raise  # Re-raise for Celery retry
