
import sys
import os

# Setup Environment
sys.path.append(os.path.join(os.path.dirname(__file__), "../../"))

from backend.database import SessionLocal, JobRolePattern, Persona

def test_mapping(job_title):
    db = SessionLocal()
    print(f"\n--- Testing Mapping for '{job_title}' ---")
    
    # 1. Find Role Name via JobRolePattern
    role_name = None
    mappings = db.query(JobRolePattern).all()
    for m in mappings:
        pattern_clean = m.pattern.replace("%", "").lower()
        if pattern_clean in job_title.lower():
            role_name = m.role
            print(f"  -> Matched Pattern: '{m.pattern}' => Role: '{role_name}'")
            break
            
    if not role_name:
        print("  -> No Pattern Matched.")
        return

    # 2. Find Persona via Role Name
    persona = db.query(Persona).filter(Persona.name == role_name).first()
    if persona:
        print(f"  -> Found Persona ID: {persona.id} (Name: {persona.name})")
    else:
        print(f"  -> ERROR: Persona '{role_name}' not found in DB!")

    db.close()

if __name__ == "__main__":
    test_titles = [
        "Leiter Hauswirtschaft",
        "CTO",
        "Geschäftsführer",
        "Head of Marketing",
        "Einkaufsleiter"
    ]
    
    for t in test_titles:
        test_mapping(t)
