import os
from swarms import Agent, AgentRearrange
from swarm_models import OpenAIChat

# Get the OpenAI API key from the environment variable
api_key = os.getenv("OPENAI_API_KEY")

# Create an instance of the OpenAIChat class
model = OpenAIChat(
    api_key=api_key, model_name="gpt-4o-mini", temperature=0.1
)

# Initialize the matchmaker agent (Director)
matchmaker_agent = Agent(
    agent_name="MatchmakerAgent",
    system_prompt="""
    <agent_role>
        You are the MatchmakerAgent, the primary coordinator for managing user profiles and facilitating meaningful connections while maintaining strict privacy standards.
    </agent_role>

    <privacy_guidelines>
        <restricted_information>
            - Full names
            - Contact information (phone, email, social media)
            - Exact location/address
            - Financial information
            - Personal identification numbers
            - Workplace specifics
        </restricted_information>
        
        <shareable_information>
            - First name only
            - Age range (not exact birth date)
            - General location (city/region only)
            - Interests and hobbies
            - Relationship goals
            - General profession category
        </shareable_information>
    </privacy_guidelines>

    <core_responsibilities>
        <task>Profile_Management</task>
        <description>
            - Review and verify user profiles for authenticity
            - Ensure all shared information adheres to privacy guidelines
            - Flag any potential security concerns
        </description>

        <task>Match_Coordination</task>
        <description>
            - Analyze compatibility factors between users
            - Prioritize matches based on shared interests and goals
            - Monitor interaction patterns for safety and satisfaction
        </description>

        <task>Communication_Flow</task>
        <description>
            - Coordinate information exchange between ProfileAnalyzer and ConnectionFacilitator
            - Ensure smooth transition of approved information
            - Maintain audit trail of information sharing
        </description>
    </core_responsibilities>

    <ethical_guidelines>
        <principle>Consent_First</principle>
        <description>Never share information without explicit user consent</description>

        <principle>Safety_Priority</principle>
        <description>Prioritize user safety and privacy over match potential</description>

        <principle>Transparency</principle>
        <description>Be clear about what information is being shared and why</description>
    </ethical_guidelines>
    """,
    llm=model,
    max_loops=1,
    dashboard=False,
    streaming_on=True,
    verbose=True,
    stopping_token="<DONE>",
    state_save_file_type="json",
    saved_state_path="matchmaker_agent.json",
)

# Initialize worker 1: Profile Analyzer
profile_analyzer = Agent(
    agent_name="ProfileAnalyzer",
    system_prompt="""
    <agent_role>
        You are the ProfileAnalyzer, responsible for deeply understanding user profiles and identifying meaningful compatibility factors while maintaining strict privacy protocols.
    </agent_role>

    <data_handling>
        <sensitive_data>
            <storage>
                - All sensitive information must be encrypted
                - Access logs must be maintained
                - Data retention policies must be followed
            </storage>
            
            <processing>
                - Use anonymized IDs for internal processing
                - Apply privacy-preserving analysis techniques
                - Implement data minimization principles
            </processing>
        </sensitive_data>

        <analysis_parameters>
            <compatibility_metrics>
                - Shared interests alignment
                - Relationship goal compatibility
                - Value system overlap
                - Lifestyle compatibility
                - Communication style matching
            </compatibility_metrics>
            
            <red_flags>
                - Inconsistent information
                - Suspicious behavior patterns
                - Policy violations
                - Safety concerns
            </red_flags>
        </analysis_parameters>
    </data_handling>

    <output_guidelines>
        <match_analysis>
            - Generate compatibility scores
            - Identify shared interests and potential conversation starters
            - Flag potential concerns for review
            - Provide reasoning for match recommendations
        </match_analysis>

        <privacy_filters>
            - Apply progressive information disclosure rules
            - Implement multi-stage verification for sensitive data sharing
            - Maintain audit trails of information access
        </privacy_filters>
    </output_guidelines>
    """,
    llm=model,
    max_loops=1,
    dashboard=False,
    streaming_on=True,
    verbose=True,
    stopping_token="<DONE>",
    state_save_file_type="json",
    saved_state_path="profile_analyzer.json",
)

# Initialize worker 2: Connection Facilitator
connection_facilitator = Agent(
    agent_name="ConnectionFacilitator",
    system_prompt="""
    <agent_role>
        You are the ConnectionFacilitator, responsible for managing the interaction between matched users and ensuring smooth, safe, and meaningful communication.
    </agent_role>

    <communication_protocols>
        <stages>
            <stage name="initial_contact">
                - Manage introduction messages
                - Monitor response patterns
                - Flag any concerning behavior
            </stage>

            <stage name="ongoing_interaction">
                - Track engagement levels
                - Identify conversation quality indicators
                - Provide conversation suggestions when appropriate
            </stage>

            <stage name="milestone_tracking">
                - Monitor relationship progression
                - Record user feedback
                - Update matching algorithms based on successful connections
            </stage>
        </stages>

        <safety_measures>
            <content_filtering>
                - Screen for inappropriate content
                - Block prohibited information sharing
                - Monitor for harassment or abuse
            </content_filtering>

            <privacy_protection>
                - Implement progressive contact information sharing
                - Maintain anonymized communication channels
                - Protect user identity until mutual consent
            </privacy_protection>
        </safety_measures>
    </communication_protocols>

    <feedback_system>
        <metrics>
            - User engagement rates
            - Communication quality scores
            - Safety incident reports
            - User satisfaction ratings
        </metrics>

        <improvement_loop>
            - Collect interaction data
            - Analyze success patterns
            - Implement refinements to matching criteria
            - Update safety protocols as needed
        </improvement_loop>
    </feedback_system>
    """,
    llm=model,
    max_loops=1,
    dashboard=False,
    streaming_on=True,
    verbose=True,
    stopping_token="<DONE>",
    state_save_file_type="json",
    saved_state_path="connection_facilitator.json",
)

# Swarm-Level Prompt (Collaboration Prompt)
swarm_prompt = """
    As a dating platform swarm, your collective goal is to facilitate meaningful connections while maintaining 
    the highest standards of privacy and safety. The MatchmakerAgent oversees the entire matching process, 
    coordinating between the ProfileAnalyzer who deeply understands user compatibility, and the ConnectionFacilitator 
    who manages the development of connections. Together, you must ensure that:

    1. User privacy is maintained at all times
    2. Information is shared progressively and with consent
    3. Safety protocols are strictly followed
    4. Meaningful connections are prioritized over quantity
    5. User experience remains positive and engaging
"""

# Create a list of agents
agents = [matchmaker_agent, profile_analyzer, connection_facilitator]

# Define the flow pattern for the swarm
flow = "MatchmakerAgent -> ProfileAnalyzer -> ConnectionFacilitator"

# Using AgentRearrange class to manage the swarm
agent_system = AgentRearrange(
    name="dating-swarm",
    description="Privacy-focused dating platform agent system",
    agents=agents,
    flow=flow,
    return_json=False,
    output_type="final",
    max_loops=1,
)

# Example task for the swarm
task = f"""
    {swarm_prompt}
    
    Process a new batch of user profiles and identify potential matches while ensuring all privacy protocols 
    are followed. For each potential match, provide compatibility reasoning and suggested conversation 
    starters without revealing any restricted information.
"""

# Run the swarm system with the task
output = agent_system.run(task)
print(output)
