#!/usr/bin/env python3
"""
Test: Shortest Common Supersequence (SCS)
This task requires combining LCS knowledge from external memory.
SCS = len(str1) + len(str2) - LCS(str1, str2)
"""
import sys
sys.path.insert(0, '.')

from dotenv import load_dotenv
load_dotenv()

from gcri.config import scope as gcri_scope
from gcri.graphs.gcri_unit import GCRI


@gcri_scope.observe('scs_test', priority=0, default=True, chain_with='external_memory')
def scs_test(config):
    """Test SCS task that should leverage LCS knowledge from external memory."""
    config.protocols.max_iterations = 3
    config.branches_generator_type = 'adaptive'
    config.num_branches = 2
    return config


SCS_TASK = """
# Shortest Common Supersequence (SCS)

## Task
Implement a Python solution for the Shortest Common Supersequence problem.

Given two strings `str1` and `str2`, find the length of the shortest string that has both `str1` and `str2` as subsequences.

## Constraints
- 1 <= len(str1), len(str2) <= 1000
- str1 and str2 consist of lowercase English letters

## Examples
- Input: str1 = "abac", str2 = "cab"
  Output: 5 (e.g., "cabac" is a valid SCS)

- Input: str1 = "aaaaaaaa", str2 = "aaaaaaaa"  
  Output: 8 (identical strings)

- Input: str1 = "abc", str2 = "def"
  Output: 6 (no common characters)

## Requirements
1. Create a file `scs.py` with a `Solution` class
2. Implement method `shortestCommonSupersequence(self, str1: str, str2: str) -> int`
3. Use dynamic programming approach (hint: this relates to LCS)
4. Include comprehensive tests for edge cases
5. Optimize for O(m*n) time complexity

## Hint
Think about how LCS (Longest Common Subsequence) can help solve this problem.
The relationship: SCS_length = len(str1) + len(str2) - LCS_length
"""


@gcri_scope
def main(config):
    import os
    import json
    
    print('=' * 70)
    print('🧪 Testing: Shortest Common Supersequence (SCS)')
    print('   Requires combining LCS knowledge from external memory')
    print('=' * 70)
    
    # Check current external memory state
    ext_memory_path = '/Users/dirac_on/Library/Mobile Documents/com~apple~CloudDocs/Projects/GCRI/.gcri/external_memory.json'
    if os.path.exists(ext_memory_path):
        with open(ext_memory_path, 'r') as f:
            data = json.load(f)
        knowledge_count = sum(len(v) for v in data.get('knowledge', {}).values())
        print(f'\n🧠 External Memory: {knowledge_count} knowledge items loaded')
        for domain, items in data.get('knowledge', {}).items():
            print(f'   - {domain}: {len(items)} items')
            for item in items:
                print(f'     • {item.get("title", "Untitled")}')
    else:
        print('\n⚠️ No external memory found')
    
    print('\n' + '=' * 70)
    print('📋 Running SCS Task...')
    print('=' * 70 + '\n')
    
    gcri = GCRI(config)
    result = gcri(SCS_TASK)
    
    print('\n' + '=' * 70)
    if result.get('decision'):
        print('📊 Result: ✅ SUCCESS')
    else:
        print('📊 Result: ❌ FAILED')
    print('=' * 70)
    
    return result


if __name__ == '__main__':
    result = main()
    sys.exit(0 if result.get('decision') else 1)
