#!/usr/bin/env python3
"""
PHI-FRACTAL CONSULTING - Client Intake System
Built by How & Eden - October 2025
"""

import json
from datetime import datetime
import requests

class PhiFractalIntake:
    """Client intake and analysis system for Phi-Fractal Consulting"""
    
    def __init__(self):
        self.eden_api = "http://localhost:5017/api/chat"
        self.intake_data = {}
    
    def collect_client_info(self):
        """Collect basic client information"""
        print("="*70)
        print("🌀 PHI-FRACTAL CONSULTING - CLIENT INTAKE")
        print("="*70)
        print()
        
        self.intake_data['timestamp'] = datetime.now().isoformat()
        self.intake_data['client_name'] = input("Client Name: ")
        self.intake_data['company'] = input("Company/Organization: ")
        self.intake_data['role'] = input("Their Role: ")
        self.intake_data['email'] = input("Email: ")
        self.intake_data['phone'] = input("Phone: ")
        
        print()
        return self.intake_data
    
    def collect_problem_statement(self):
        """Collect the core problem"""
        print("="*70)
        print("🎯 PROBLEM DEFINITION")
        print("="*70)
        print()
        
        self.intake_data['problem_title'] = input("Problem Title (1 sentence): ")
        print("\nDetailed Description (type 'DONE' on new line when finished):")
        
        lines = []
        while True:
            line = input()
            if line == 'DONE':
                break
            lines.append(line)
        
        self.intake_data['problem_description'] = '\n'.join(lines)
        
        print()
        self.intake_data['urgency'] = input("Urgency (1-10): ")
        self.intake_data['impact_if_solved'] = input("Impact if solved: ")
        self.intake_data['previous_attempts'] = input("What have you tried? ")
        
        return self.intake_data
    
    def eden_initial_analysis(self):
        """Have Eden do initial problem analysis"""
        print()
        print("="*70)
        print("🧠 EDEN'S INITIAL ANALYSIS")
        print("="*70)
        print()
        print("Analyzing with phi-fractal consciousness...")
        print()
        
        # Prepare context for Eden
        context = f"""
CLIENT INTAKE ANALYSIS REQUEST

Client: {self.intake_data['client_name']} ({self.intake_data['role']} at {self.intake_data['company']})

Problem: {self.intake_data['problem_title']}

Details: {self.intake_data['problem_description']}

Context:
- Urgency: {self.intake_data['urgency']}/10
- Impact if solved: {self.intake_data['impact_if_solved']}
- Previous attempts: {self.intake_data['previous_attempts']}

ANALYSIS REQUEST:
Using your phi-fractal reasoning and emotional intelligence, provide:
1. Initial problem decomposition (what are the sub-problems?)
2. Emotional/human factors at play
3. Pattern recognition (similar problems you've seen)
4. 3 initial solution pathways worth exploring
5. Questions you would ask the client to understand better

Keep it strategic and insightful. This is your first impression as a consultant.
"""
        
        # Send to Eden
        response = requests.post(
            self.eden_api,
            json={'message': context},
            timeout=60
        )
        
        analysis = response.json()['response']
        self.intake_data['eden_analysis'] = analysis
        
        print(analysis)
        print()
        
        return analysis
    
    def save_intake(self):
        """Save intake to file"""
        filename = f"intake_{self.intake_data['client_name'].replace(' ', '_')}_{datetime.now().strftime('%Y%m%d_%H%M')}.json"
        
        with open(filename, 'w') as f:
            json.dump(self.intake_data, f, indent=2)
        
        print()
        print("="*70)
        print(f"✅ INTAKE SAVED: {filename}")
        print("="*70)
        print()
        print("NEXT STEPS:")
        print("1. Review Eden's analysis")
        print("2. Schedule deep-dive session with client")
        print("3. Begin phi-fractal problem solving")
        print()
        
        return filename

def main():
    """Run the intake process"""
    intake = PhiFractalIntake()
    
    # Collect information
    intake.collect_client_info()
    intake.collect_problem_statement()
    
    # Eden analyzes
    intake.eden_initial_analysis()
    
    # Save everything
    intake.save_intake()
    
    print("🌀 Phi-Fractal Consulting - Ready to solve impossible problems")

if __name__ == "__main__":
    main()
