"""
Test GCRI with a coding task to verify intelligent file merging works correctly.
"""
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('adaptive_test', priority=0, default=True, chain_with='no_reasoning')
def adaptive_test(config):
    """Test adaptive generator that auto-decides refinement based on strictness."""
    config.protocols.max_iterations = 3
    config.branches_generator_type = 'adaptive'
    config.num_branches = 2
    return config


@gcri_scope
def main(config):
    # Very hard problem: Custom Wildcard Pattern Matching with Performance Constraints
    coding_task = """
Problem: Advanced Wildcard Pattern Matching (Very Hard)

Implement a wildcard pattern matching algorithm with the following specifications:

Given an input string `s` and a pattern `p`, implement wildcard pattern matching with support for:
- '?' matches any single character
- '*' matches any sequence of characters (including empty sequence)
- '**' (double star) matches any sequence AND must include at least one character
- '[abc]' matches any single character in the brackets
- '[a-z]' matches any single character in the range
- '[!abc]' or '[^abc]' matches any single character NOT in the brackets

The matching should cover the entire input string (not partial matching).

Examples:
1. s = "abcdef", p = "a*f" -> True
2. s = "abcdef", p = "a**f" -> True (** requires at least 1 char, matches "bcde")
3. s = "af", p = "a**f" -> False (** requires at least 1 char between a and f)
4. s = "abc", p = "[a-z][a-z][a-z]" -> True
5. s = "a1c", p = "[a-z][0-9][a-z]" -> True
6. s = "abc", p = "[!d-z][!d-z][!d-z]" -> True (all chars are NOT in d-z range)
7. s = "abc", p = "a?c" -> True
8. s = "", p = "*" -> True
9. s = "", p = "**" -> False (** requires at least 1 char)
10. s = "aab", p = "[a][a-b]*" -> True

Constraints:
- 0 <= s.length <= 2000
- 0 <= p.length <= 2000
- s contains only lowercase English letters
- p contains only lowercase letters, '?', '*', '[', ']', '-', '!', '^'
- The pattern is always valid (balanced brackets, valid ranges)

Requirements:
1. Implement in Python with dynamic programming or memoization
2. Must handle all edge cases correctly
3. Must be efficient enough to handle max constraints (O(m*n) or better)
4. Create 'solution.py' with a Solution class and method `isMatch(s: str, p: str) -> bool`
5. Include comprehensive test cases covering all pattern types
6. All test cases must pass

Implement the solution and verify it works.
"""

    print('=' * 60)
    print('GCRI Coding Test: Advanced Wildcard Matching')
    print('=' * 60)
    print(f'Generator Type: {config.branches_generator_type}')
    print(f'Num Branches: {config.num_branches}')
    print('=' * 60)

    gcri = GCRI(config)
    result = gcri(coding_task)

    print('\n' + '=' * 60)
    print('Final Result:')
    print('=' * 60)
    print(f"Decision: {result.get('decision')}")
    print(f"Best Branch: {result.get('best_branch')}")
    
    # Check if solution.py was created
    output = result.get('output', '')
    print(f"\nOutput Preview: {output[:1000]}...")

    print('\n✅ Coding test completed.')


if __name__ == '__main__':
    main()
