from typing import List, Dict

def build_destination_prompt(email_headers: Dict[str, str], rules: List[Dict[str, any]]) -> str:
    """
    Build a prompt for determining the best destination folder for an email.
    
    Args:
        email_headers: Dictionary containing email headers (subject, from, to, date)
        rules: List of dictionaries containing folder rules with name, rule_text, and priority
    
    Returns:
        Formatted prompt string
    """
    
    # Sort rules by priority (highest first) for consistent ordering
    sorted_rules = sorted(rules, key=lambda x: x['priority'], reverse=True)
    
    prompt = '''Determine the best destination folder for the following email based on the user's folder rules and priorities.

Email Details:
- Subject: {subject}
- From: {from_email}
- To: {to_email}
- Date: {date}

Available Folders (listed in priority order - higher priority folders should be preferred):
{folder_rules}

Instructions:
1. Review the email details and all folder rules
2. Select the SINGLE most appropriate destination folder based on the rules and priorities
3. If none of the folders are appropriate, respond with "INBOX" (keep in current location)
4. Respond ONLY with the exact folder name - nothing else

Best destination folder: '''.format(
        subject=email_headers.get('subject', 'N/A'),
        from_email=email_headers.get('from', 'N/A'),
        to_email=email_headers.get('to', 'N/A'),
        date=email_headers.get('date', 'N/A'),
        folder_rules='\n'.join([
            f"- {rule['name']} (Priority: {rule['priority']}): {rule['rule_text']}" 
            for rule in sorted_rules
        ])
    )
    
    return prompt