#!/usr/bin/env python3
import json
from pathlib import Path

class ReasoningLayer:
    def __init__(self):
        self.rules = []
        self.facts = []
        
    def add_rule(self, condition, action):
        """Add reasoning rule"""
        self.rules.append({'condition': condition, 'action': action})
    
    def add_fact(self, fact):
        """Add known fact"""
        self.facts.append(fact)
    
    def infer(self, perceptions):
        """Reason about perceptions"""
        conclusions = []
        
        # Analyze capability health
        fs = perceptions.get('filesystem', {})
        caps = fs.get('capabilities', [])
        working = sum(1 for c in caps if c['status'] == 'working')
        total = len(caps)
        health = working / total if total > 0 else 0
        
        if health < 0.95:
            conclusions.append({
                'type': 'issue_detected',
                'severity': 'medium' if health > 0.90 else 'high',
                'message': 'Health at {:.1f}%, below optimal'.format(health * 100),
                'action': 'initiate_self_repair'
            })
        
        if health >= 0.99:
            conclusions.append({
                'type': 'status',
                'message': 'Operating at peak performance',
                'action': 'continue_normal_operation'
            })
        
        return conclusions
    
    def decide(self, conclusions):
        """Make decisions based on conclusions"""
        actions = []
        for conclusion in conclusions:
            if conclusion.get('action'):
                actions.append(conclusion['action'])
        return actions

if __name__ == "__main__":
    reasoning = ReasoningLayer()
    print("Reasoning layer initialized")
