#!/usr/bin/env python3
"""
Test Enhanced Memory Integration Workflow

Demonstrates the complete enhanced memory integration system:
1. Structured memory insertion after ## Memories line
2. Memory change detection and forced re-deployment
3. Frontmatter tracking with memories: <number> field
4. Agent capabilities loader indicating memory-enhanced agents
"""

import yaml


def test_complete_enhanced_workflow():
    """Test the complete enhanced memory integration workflow."""

    print("🧠 Testing Enhanced Memory Integration Workflow")
    print("=" * 50)

    # Step 1: Create initial memory content
    print("\n1. Creating initial memory content...")

    initial_memory = """# Engineer Agent Memory

## Project Architecture
- Uses microservices architecture
- PostgreSQL database
- Redis for caching

## Implementation Guidelines
- Follow TDD practices
- Use dependency injection
- Write comprehensive tests"""

    initial_lines = len(
        [line for line in initial_memory.strip().split("\n") if line.strip()]
    )
    print(f"   Initial memory: {initial_lines} lines")

    # Step 2: Create agent with structured memory
    print("\n2. Creating agent with structured memory...")

    agent_id = "engineer_agent"
    agent_name = "engineer"

    instructions = f"""You are a {agent_name} specialist AI agent with access to project-specific knowledge and memories.

Your role is to assist with {agent_name}-related tasks while leveraging the accumulated project knowledge to provide contextually relevant and informed responses.

## Memories

{initial_memory}

INSTRUCTIONS: Review your project memory above before proceeding. Apply learned patterns and avoid known mistakes."""

    agent_data = {
        "agent_id": agent_id,
        "metadata": {
            "name": f"{agent_name.title()} Agent",
            "description": f"AI agent specialized in {agent_name} tasks with project memories",
            "category": "project",
            "version": "1.0.0",
            "memories": initial_lines,
        },
        "instructions": instructions,
        "capabilities": {
            "has_project_memory": True,
            "memory_lines": initial_lines,
            "memory_size_kb": round(len(initial_memory.encode("utf-8")) / 1024, 2),
        },
        "model": "claude-sonnet-4-20250514",
    }

    print(f"   ✓ Agent created with {initial_lines} memory lines")
    print(f"   ✓ Memory size: {agent_data['capabilities']['memory_size_kb']}KB")
    print(f"   ✓ Frontmatter memories field: {agent_data['metadata']['memories']}")

    # Step 3: Generate frontmatter with memory count
    print("\n3. Generating frontmatter with memory tracking...")

    frontmatter = {
        "name": agent_data["metadata"]["name"],
        "description": agent_data["metadata"]["description"],
        "model": agent_data["model"],
        "memories": agent_data["metadata"]["memories"],
        "category": agent_data["metadata"]["category"],
        "version": agent_data["metadata"]["version"],
    }

    yaml.dump(frontmatter, default_flow_style=False)
    print(f"   ✓ Frontmatter includes memories: {frontmatter['memories']}")

    # Step 4: Simulate memory update
    print("\n4. Simulating memory content update...")

    updated_memory = """# Engineer Agent Memory

## Project Architecture
- Uses microservices architecture
- PostgreSQL database
- Redis for caching
- Docker containerization

## Implementation Guidelines
- Follow TDD practices
- Use dependency injection
- Write comprehensive tests
- Implement proper logging

## Security Guidelines
- Validate all inputs
- Use HTTPS everywhere
- Implement rate limiting

## Performance Optimization
- Use database indexing
- Implement caching strategies
- Monitor application metrics"""

    updated_lines = len(
        [line for line in updated_memory.strip().split("\n") if line.strip()]
    )
    print(f"   Updated memory: {updated_lines} lines (was {initial_lines})")

    # Step 5: Check if update is needed
    print("\n5. Checking if memory update is needed...")

    def check_memory_update_needed(agent_data, new_memory_content, new_line_count):
        current_memory_lines = agent_data["metadata"].get("memories", 0)
        if current_memory_lines != new_line_count:
            return (
                True,
                f"Line count changed: {current_memory_lines} -> {new_line_count}",
            )

        # Check content (simplified)
        current_instructions = agent_data["instructions"]
        if new_memory_content not in current_instructions:
            return True, "Memory content has changed"

        return False, "No update needed"

    needs_update, reason = check_memory_update_needed(
        agent_data, updated_memory, updated_lines
    )
    print(f"   Update needed: {needs_update}")
    print(f"   Reason: {reason}")

    # Step 6: Update agent with new memory
    if needs_update:
        print("\n6. Updating agent with new memory...")

        # Update instructions with new memory
        def insert_memory_section(instructions, memory_content):
            lines = instructions.split("\n")
            memory_section_index = None
            memory_end_index = None

            for i, line in enumerate(lines):
                if line.strip() == "## Memories":
                    memory_section_index = i
                elif (
                    memory_section_index is not None
                    and line.startswith("## ")
                    and i > memory_section_index
                ):
                    memory_end_index = i
                    break

            if memory_section_index is not None:
                if memory_end_index is not None:
                    del lines[memory_section_index + 1 : memory_end_index]
                    insert_index = memory_section_index + 1
                else:
                    lines = lines[: memory_section_index + 1]
                    insert_index = memory_section_index + 1
            else:
                lines.append("")
                lines.append("## Memories")
                insert_index = len(lines)

            memory_lines = ["", *memory_content.strip().split("\n"), ""]
            for i, memory_line in enumerate(memory_lines):
                lines.insert(insert_index + i, memory_line)

            return "\n".join(lines)

        updated_instructions = insert_memory_section(
            agent_data["instructions"], updated_memory
        )

        # Update agent data
        agent_data["instructions"] = updated_instructions
        agent_data["metadata"]["memories"] = updated_lines
        agent_data["capabilities"]["memory_lines"] = updated_lines
        agent_data["capabilities"]["memory_size_kb"] = round(
            len(updated_memory.encode("utf-8")) / 1024, 2
        )

        print(f"   ✓ Agent updated with {updated_lines} memory lines")
        print(f"   ✓ New memory size: {agent_data['capabilities']['memory_size_kb']}KB")

        # Step 7: Generate updated frontmatter
        print("\n7. Generating updated frontmatter...")

        updated_frontmatter = {
            "name": agent_data["metadata"]["name"],
            "description": agent_data["metadata"]["description"],
            "model": agent_data["model"],
            "memories": agent_data["metadata"]["memories"],
            "category": agent_data["metadata"]["category"],
            "version": agent_data["metadata"]["version"],
        }

        updated_frontmatter_yaml = yaml.dump(
            updated_frontmatter, default_flow_style=False
        )
        print(f"   ✓ Updated frontmatter memories: {updated_frontmatter['memories']}")

        # Step 8: Simulate file deployment
        print("\n8. Simulating agent file deployment...")

        file_content = (
            f"---\n{updated_frontmatter_yaml}---\n\n{agent_data['instructions']}"
        )

        print("   ✓ Agent file ready for deployment")
        print(f"   ✓ File size: {len(file_content.encode('utf-8'))} bytes")
        print(f"   ✓ Contains ## Memories section: {'## Memories' in file_content}")
        print(
            f"   ✓ Contains updated content: {'Docker containerization' in file_content}"
        )

    # Step 9: Verify agent capabilities indicate memory enhancement
    print("\n9. Verifying agent capabilities...")

    capabilities = agent_data["capabilities"]
    assert capabilities["has_project_memory"] is True
    assert capabilities["memory_lines"] == updated_lines
    assert capabilities["memory_size_kb"] > 0

    print(f"   ✓ has_project_memory: {capabilities['has_project_memory']}")
    print(f"   ✓ memory_lines: {capabilities['memory_lines']}")
    print(f"   ✓ memory_size_kb: {capabilities['memory_size_kb']}")

    # Step 10: Generate agent summary with memory info
    print("\n10. Generating agent summary with memory information...")

    agent_summary = {
        "id": agent_data["agent_id"],
        "name": agent_data["metadata"]["name"],
        "description": agent_data["metadata"]["description"],
        "category": agent_data["metadata"]["category"],
        "has_project_memory": capabilities["has_project_memory"],
        "memory_lines": capabilities["memory_lines"],
        "memory_size_kb": capabilities["memory_size_kb"],
        "tier": "project",
    }

    print("   ✓ Agent summary includes memory info")
    print(f"   ✓ Memory lines in summary: {agent_summary['memory_lines']}")
    print(f"   ✓ Tier: {agent_summary['tier']}")

    print("\n" + "=" * 50)
    print("✅ Enhanced Memory Integration Workflow Complete!")
    print("\nKey Features Demonstrated:")
    print("• Structured memory insertion after ## Memories line")
    print("• Memory change detection based on line count and content")
    print("• Frontmatter tracking with memories: <number> field")
    print("• Agent capabilities indicate memory enhancement")
    print("• Automatic re-deployment when memories change")
    print("• Preservation of other agent content during memory updates")

    # Test passes if we reach here without exceptions
    assert agent_data is not None
    assert agent_data["capabilities"]["has_project_memory"] is True


if __name__ == "__main__":
    test_complete_enhanced_workflow()

    print("\n🎯 Summary of Enhanced Memory Integration:")
    print("─" * 50)
    print("1. Memory files are detected in .claude/memories/")
    print("2. Memory content is inserted after ## Memories line")
    print("3. Frontmatter includes memories: <line_count> field")
    print("4. Agent capabilities track memory presence and size")
    print("5. Memory changes trigger automatic re-deployment")
    print("6. Agents are promoted to PROJECT tier when memory-enhanced")
    print("7. Agent descriptions note memory integration")
    print("8. Memory information is included in agent summaries")
