# services/external/app/services/tenant_deletion_service.py
"""
Tenant Data Deletion Service for External Service
Handles deletion of tenant-specific data for the External service
"""

from typing import Dict
from sqlalchemy import select, func, delete
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.dialects.postgresql import UUID
import structlog

from shared.services.tenant_deletion import (
    BaseTenantDataDeletionService,
    TenantDataDeletionResult
)
from app.models import AuditLog, WeatherData

logger = structlog.get_logger(__name__)


class ExternalTenantDeletionService(BaseTenantDataDeletionService):
    """
    Service for deleting tenant-specific external data

    IMPORTANT NOTE:
    The External service primarily stores SHARED city-wide data (weather, traffic)
    that is NOT tenant-specific. This data is used by ALL tenants and should
    NOT be deleted when a single tenant is removed.

    Tenant-specific data in this service:
    - Audit logs (tenant_id)
    - Tenant-specific weather data (if any exists with tenant_id)

    City-wide data that is NOT deleted (shared across all tenants):
    - CityWeatherData (no tenant_id - city-wide data)
    - CityTrafficData (no tenant_id - city-wide data)
    - TrafficData (no tenant_id - city-wide data)
    - TrafficMeasurementPoint (no tenant_id - reference data)
    - WeatherForecast (no tenant_id - city-wide forecasts)
    """

    def __init__(self, db: AsyncSession):
        self.db = db
        self.service_name = "external"

    async def get_tenant_data_preview(self, tenant_id: str) -> Dict[str, int]:
        """
        Get counts of what would be deleted for a tenant (dry-run)

        Args:
            tenant_id: The tenant ID to preview deletion for

        Returns:
            Dictionary with entity names and their counts
        """
        logger.info("external.tenant_deletion.preview", tenant_id=tenant_id)
        preview = {}

        try:
            # Count tenant-specific weather data (if any)
            weather_count = await self.db.scalar(
                select(func.count(WeatherData.id)).where(
                    WeatherData.tenant_id == tenant_id
                )
            )
            preview["tenant_weather_data"] = weather_count or 0

            # Count audit logs
            audit_count = await self.db.scalar(
                select(func.count(AuditLog.id)).where(
                    AuditLog.tenant_id == tenant_id
                )
            )
            preview["audit_logs"] = audit_count or 0

            # Add informational message about shared data
            logger.info(
                "external.tenant_deletion.preview_complete",
                tenant_id=tenant_id,
                preview=preview,
                note="City-wide data (traffic, weather) is shared and will NOT be deleted"
            )

        except Exception as e:
            logger.error(
                "external.tenant_deletion.preview_error",
                tenant_id=tenant_id,
                error=str(e),
                exc_info=True
            )
            raise

        return preview

    async def delete_tenant_data(self, tenant_id: str) -> TenantDataDeletionResult:
        """
        Permanently delete tenant-specific external data

        NOTE: This only deletes tenant-specific data. City-wide shared data
        (CityWeatherData, CityTrafficData, TrafficData, etc.) is intentionally
        preserved as it's used by all tenants.

        Args:
            tenant_id: The tenant ID to delete data for

        Returns:
            TenantDataDeletionResult with deletion counts and any errors
        """
        logger.info(
            "external.tenant_deletion.started",
            tenant_id=tenant_id,
            note="Only deleting tenant-specific data; city-wide data preserved"
        )
        result = TenantDataDeletionResult(tenant_id=tenant_id, service_name=self.service_name)

        try:
            # Step 1: Delete tenant-specific weather data (if any exists)
            logger.info("external.tenant_deletion.deleting_weather_data", tenant_id=tenant_id)
            weather_result = await self.db.execute(
                delete(WeatherData).where(
                    WeatherData.tenant_id == tenant_id
                )
            )
            result.deleted_counts["tenant_weather_data"] = weather_result.rowcount
            logger.info(
                "external.tenant_deletion.weather_data_deleted",
                tenant_id=tenant_id,
                count=weather_result.rowcount
            )

            # Step 2: Delete audit logs
            logger.info("external.tenant_deletion.deleting_audit_logs", tenant_id=tenant_id)
            audit_result = await self.db.execute(
                delete(AuditLog).where(
                    AuditLog.tenant_id == tenant_id
                )
            )
            result.deleted_counts["audit_logs"] = audit_result.rowcount
            logger.info(
                "external.tenant_deletion.audit_logs_deleted",
                tenant_id=tenant_id,
                count=audit_result.rowcount
            )

            # Commit the transaction
            await self.db.commit()

            # Calculate total deleted
            total_deleted = sum(result.deleted_counts.values())

            # Add informational note about preserved data
            result.deleted_counts["_note"] = "City-wide data preserved (shared across tenants)"

            logger.info(
                "external.tenant_deletion.completed",
                tenant_id=tenant_id,
                total_deleted=total_deleted,
                breakdown=result.deleted_counts,
                preserved_data="CityWeatherData, CityTrafficData, TrafficData (shared)"
            )

            result.success = True

        except Exception as e:
            await self.db.rollback()
            error_msg = f"Failed to delete external data for tenant {tenant_id}: {str(e)}"
            logger.error(
                "external.tenant_deletion.failed",
                tenant_id=tenant_id,
                error=str(e),
                exc_info=True
            )
            result.errors.append(error_msg)
            result.success = False

        return result


def get_external_tenant_deletion_service(db: AsyncSession) -> ExternalTenantDeletionService:
    """
    Factory function to create ExternalTenantDeletionService instance

    Args:
        db: AsyncSession database session

    Returns:
        ExternalTenantDeletionService instance
    """
    return ExternalTenantDeletionService(db)
