#!/usr/bin/env python3
"""
Debug du matching de workflows - Analyse pourquoi la confiance est faible.
"""

import json
import sys
from pathlib import Path

def load_workflows():
    """Charge les workflows."""
    workflow_dir = Path("geniusia2/data/user_profiles/workflows")
    workflows = []
    
    for file in workflow_dir.glob("workflow_*.json"):
        with open(file, 'r') as f:
            workflows.append(json.load(f))
    
    return workflows

def analyze_workflow(workflow):
    """Analyse un workflow."""
    print(f"\n{'='*60}")
    print(f"Workflow: {workflow['name']}")
    print(f"ID: {workflow['workflow_id']}")
    print(f"Répétitions: {workflow['repetitions']}")
    print(f"Confiance: {workflow['confidence']}")
    print(f"Nombre d'étapes: {len(workflow['steps'])}")
    print(f"{'='*60}")
    
    for i, step in enumerate(workflow['steps']):
        print(f"\nÉtape {i}:")
        print(f"  Type d'action: {step['action_type']}")
        print(f"  Position: {step['position']}")
        print(f"  Fenêtre: {step['window']}")
        print(f"  Description: {step.get('target_description', 'N/A')}")

def main():
    print("\n" + "="*60)
    print("DEBUG DU MATCHING DE WORKFLOWS")
    print("="*60)
    
    # Charger les workflows
    workflows = load_workflows()
    print(f"\n✓ {len(workflows)} workflows chargés")
    
    # Analyser chaque workflow
    for workflow in workflows:
        analyze_workflow(workflow)
    
    # Problème identifié
    print("\n" + "="*60)
    print("PROBLÈME IDENTIFIÉ")
    print("="*60)
    
    print("\nLes workflows enregistrés utilisent:")
    print("  action_type: 'mouse_click'")
    
    print("\nMais les actions capturées utilisent probablement:")
    print("  action_type: 'click' ou autre")
    
    print("\nSolution:")
    print("  1. Normaliser les types d'actions")
    print("  2. OU réduire le seuil de confiance temporairement")
    print("  3. OU enregistrer de nouveaux workflows")
    
    print("\n" + "="*60)
    print("RECOMMANDATIONS")
    print("="*60)
    
    print("\nOption 1: Réduire le seuil (pour tester)")
    print("  Dans config: min_confidence = 0.30")
    
    print("\nOption 2: Enregistrer de nouveaux workflows")
    print("  1. Supprimer les anciens workflows")
    print("  2. Lancer en mode shadow")
    print("  3. Faire des actions dans Calculatrice")
    print("  4. Relancer en mode assist")
    
    print("\nOption 3: Normaliser les types d'actions (fix code)")
    print("  Mapper 'mouse_click' → 'click' dans le matcher")

if __name__ == "__main__":
    main()
