"""
Comparative Benchmarks

Compare Memorable against Memori, Mem0, and Supermemory.
"""

import logging
import asyncio
from typing import Dict, Any, List
from datetime import datetime

logger = logging.getLogger(__name__)


class SystemComparison:
    """
    Compare Memorable with other memory systems.
    """

    def __init__(self, memorable_engine: Any):
        """
        Initialize comparison.
        
        Args:
            memorable_engine: Memorable MemoryEngine instance
        """
        self.memorable = memorable_engine

    async def run_comparison(self) -> Dict[str, Any]:
        """
        Run comprehensive comparison.
        
        Returns:
            Dictionary with comparison results
        """
        logger.info("Running system comparison...")

        results = {
            "memorable": await self._benchmark_memorable(),
            "integration_ease": self._compare_integration_ease(),
            "features": self._compare_features(),
            "performance": await self._compare_performance(),
        }

        return results

    async def _benchmark_memorable(self) -> Dict[str, Any]:
        """Benchmark Memorable performance."""
        import time

        # Test setup time
        start = time.time()
        self.memorable.enable()
        setup_time = time.time() - start

        # Test retrieval latency
        await self.memorable.add_memory("Test memory", "fact")
        start = time.time()
        results = await self.memorable.search_memories("Test", limit=10)
        retrieval_time = time.time() - start

        return {
            "setup_time_ms": setup_time * 1000,
            "retrieval_latency_ms": retrieval_time * 1000,
            "memories_found": len(results),
        }

    def _compare_integration_ease(self) -> Dict[str, Any]:
        """
        Compare integration ease.
        
        Memorable: Interceptor-based (like Memori) - easiest
        Mem0: API-based - requires code changes
        Supermemory: API-based - requires code changes
        """
        return {
            "memorable": {
                "method": "Interceptor",
                "code_changes": "None",
                "setup_steps": 2,
                "score": 10,
            },
            "memori": {
                "method": "Interceptor",
                "code_changes": "None",
                "setup_steps": 2,
                "score": 10,
            },
            "mem0": {
                "method": "API calls",
                "code_changes": "Required",
                "setup_steps": 5,
                "score": 6,
            },
            "supermemory": {
                "method": "API calls",
                "code_changes": "Required",
                "setup_steps": 5,
                "score": 6,
            },
        }

    def _compare_features(self) -> Dict[str, Any]:
        """Compare feature sets."""
        return {
            "memorable": {
                "interceptor": True,
                "graph_support": True,
                "research_backed": True,
                "memory_modes": 3,
                "sql_database": True,
                "multi_hop": True,
                "temporal": True,
                "consolidation": True,
            },
            "memori": {
                "interceptor": True,
                "graph_support": False,
                "research_backed": False,
                "memory_modes": 3,
                "sql_database": True,
                "multi_hop": False,
                "temporal": False,
                "consolidation": False,
            },
            "mem0": {
                "interceptor": False,
                "graph_support": False,
                "research_backed": True,
                "memory_modes": 1,
                "sql_database": False,
                "multi_hop": False,
                "temporal": False,
                "consolidation": True,
            },
            "supermemory": {
                "interceptor": False,
                "graph_support": True,
                "research_backed": False,
                "memory_modes": 1,
                "sql_database": False,
                "multi_hop": True,
                "temporal": False,
                "consolidation": False,
            },
        }

    async def _compare_performance(self) -> Dict[str, Any]:
        """Compare performance characteristics."""
        # Memorable performance (measured)
        memorable_perf = await self._benchmark_memorable()

        # Other systems (from research/claims)
        return {
            "memorable": {
                "retrieval_latency_ms": memorable_perf["retrieval_latency_ms"],
                "setup_time_ms": memorable_perf["setup_time_ms"],
                "token_savings": "50%+ (target)",
            },
            "mem0": {
                "retrieval_latency_ms": "~100ms (from paper)",
                "setup_time_ms": "N/A",
                "token_savings": "90% (from paper)",
            },
            "supermemory": {
                "retrieval_latency_ms": "Claimed 25× faster than Mem0 (unverified)",
                "setup_time_ms": "N/A",
                "token_savings": "N/A",
            },
            "memori": {
                "retrieval_latency_ms": "N/A (not published)",
                "setup_time_ms": "N/A",
                "token_savings": "N/A",
            },
        }


async def generate_comparison_report() -> str:
    """Generate markdown comparison report."""
    from memorable_ai import MemoryEngine

    memory = MemoryEngine(database="sqlite:///comparison.db")
    comparison = SystemComparison(memory)

    results = await comparison.run_comparison()

    report = f"""# System Comparison Report

Generated: {datetime.now().isoformat()}

## Integration Ease

| System | Method | Code Changes | Setup Steps | Score |
|--------|--------|--------------|-------------|-------|
| Memorable | Interceptor | None | 2 | 10/10 |
| Memori | Interceptor | None | 2 | 10/10 |
| Mem0 | API calls | Required | 5 | 6/10 |
| Supermemory | API calls | Required | 5 | 6/10 |

## Features Comparison

| Feature | Memorable | Memori | Mem0 | Supermemory |
|---------|-----------|--------|------|-------------|
| Interceptor | ✅ | ✅ | ❌ | ❌ |
| Graph Support | ✅ | ❌ | ❌ | ✅ |
| Research Backed | ✅ | ❌ | ✅ | ❌ |
| Memory Modes | 3 | 3 | 1 | 1 |
| SQL Database | ✅ | ✅ | ❌ | ❌ |
| Multi-hop | ✅ | ❌ | ❌ | ✅ |
| Temporal | ✅ | ❌ | ❌ | ❌ |
| Consolidation | ✅ | ❌ | ✅ | ❌ |

## Performance

### Memorable
- Retrieval Latency: {results['memorable']['retrieval_latency_ms']:.2f}ms
- Setup Time: {results['memorable']['setup_time_ms']:.2f}ms

### Other Systems
- Mem0: ~100ms (from research paper)
- Supermemory: Claims 25× faster (unverified)
- Memori: Not published

## Conclusion

Memorable combines:
- **Easiest integration** (like Memori)
- **Research-backed techniques** (like Mem0)
- **Graph architecture** (like Supermemory)

Making it the most comprehensive solution.
"""

    return report

