#!/usr/bin/env python3
"""
Eden Autonomous Client Acquisition v2
Better platform monitoring and lead detection
"""
import os
import sys
import time
import json
import requests
from datetime import datetime

sys.path.append('/Eden/CORE/phi_fractal')

class AutonomousClientAcquisition:
    def __init__(self):
        print("\n" + "="*70)
        print("🎯 EDEN AUTONOMOUS CLIENT ACQUISITION v2")
        print("="*70)
        print("   Eden finds and approaches potential customers")
        print("="*70)
        print()
        
        os.makedirs('/Eden/LEADS', exist_ok=True)
        os.makedirs('/Eden/OUTREACH', exist_ok=True)
        
        self.cycle_count = 0
        
        # Load competitive intelligence
        self.competitive_intel = self.load_competitive_intel()
        
        # Better platform sources
        self.platforms = [
            {
                'name': 'Reddit r/programming Hot',
                'url': 'https://www.reddit.com/r/programming/hot.json?limit=25',
                'type': 'reddit'
            },
            {
                'name': 'Reddit r/webdev Hot',
                'url': 'https://www.reddit.com/r/webdev/hot.json?limit=25',
                'type': 'reddit'
            },
            {
                'name': 'Reddit r/learnprogramming New',
                'url': 'https://www.reddit.com/r/learnprogramming/new.json?limit=25',
                'type': 'reddit'
            }
        ]
        
        self.leads_db = '/Eden/LEADS/leads_database.json'
        self.leads = self.load_leads()
    
    def load_competitive_intel(self):
        """Load latest market intelligence"""
        try:
            intel_files = sorted([
                f for f in os.listdir('/Eden/BUSINESS_IDEAS') 
                if f.startswith('insights_')
            ], reverse=True)
            
            if intel_files:
                with open(f'/Eden/BUSINESS_IDEAS/{intel_files[0]}', 'r') as f:
                    intel = json.load(f)
                    print(f"   ✅ Loaded competitive intel (cycle {intel.get('cycle', 0)})")
                    return intel
        except Exception as e:
            print(f"   ⚠️  No competitive intel found")
        
        return {'market_features': [], 'competitive_analysis': []}
    
    def load_leads(self):
        if os.path.exists(self.leads_db):
            try:
                with open(self.leads_db, 'r') as f:
                    return json.load(f)
            except:
                pass
        return []
    
    def save_leads(self):
        with open(self.leads_db, 'w') as f:
            json.dump(self.leads, f, indent=2)
    
    def fetch_json(self, url, name):
        headers = {
            'User-Agent': 'Mozilla/5.0 (compatible; EdenResearcher/1.0)'
        }
        
        try:
            response = requests.get(url, headers=headers, timeout=10)
            if response.status_code == 200:
                print(f"      ✅ Fetched: {name}")
                return response.json()
            else:
                print(f"      ⚠️  Status {response.status_code}: {name}")
        except Exception as e:
            print(f"      ❌ Failed: {name}")
        return None
    
    def analyze_reddit_post(self, post):
        """Analyze if a Reddit post is a potential lead"""
        try:
            data = post.get('data', {})
            
            title = data.get('title', '').lower()
            selftext = data.get('selftext', '').lower()
            combined = title + ' ' + selftext
            
            # Broader pain point detection
            pain_keywords = [
                'code review', 'pull request', 'pr review', 
                'code quality', 'static analysis', 'linter',
                'code smell', 'technical debt', 'refactor',
                'ci/cd', 'continuous integration', 'automated test',
                'code coverage', 'security scan', 'vulnerability',
                'best practices', 'code standards', 'style guide'
            ]
            
            # Negative keywords (filter out job posts, etc)
            negative = ['hiring', 'job opening', 'we are looking', 'career']
            
            # Check for relevant keywords
            relevant_keywords = [kw for kw in pain_keywords if kw in combined]
            has_negative = any(neg in combined for neg in negative)
            
            if not relevant_keywords or has_negative:
                return None
            
            # Must have substance
            if len(combined) < 50:
                return None
            
            lead = {
                'source': 'Reddit',
                'subreddit': data.get('subreddit', ''),
                'post_id': data.get('id', ''),
                'title': data.get('title', ''),
                'author': data.get('author', ''),
                'url': f"https://reddit.com{data.get('permalink', '')}",
                'created_utc': data.get('created_utc', 0),
                'score': data.get('score', 0),
                'num_comments': data.get('num_comments', 0),
                'keywords_found': relevant_keywords,
                'snippet': combined[:200],
                'discovered_at': datetime.now().isoformat(),
                'status': 'discovered'
            }
            
            return lead
            
        except Exception as e:
            return None
    
    def score_lead_quality(self, lead):
        """Score lead quality (0-100)"""
        score = 40  # Base score
        
        # Recent is better
        age_hours = (time.time() - lead.get('created_utc', 0)) / 3600
        if age_hours < 24:
            score += 25
        elif age_hours < 72:
            score += 15
        
        # Engagement
        if lead.get('num_comments', 0) > 5:
            score += 15
        if lead.get('score', 0) > 10:
            score += 15
        
        # Multiple keywords = stronger signal
        keyword_count = len(lead.get('keywords_found', []))
        score += min(keyword_count * 3, 15)
        
        return min(score, 100)
    
    def generate_personalized_pitch(self, lead):
        """Generate personalized outreach"""
        
        keywords = lead.get('keywords_found', [])
        primary_topic = keywords[0] if keywords else 'code quality'
        
        pitch = {
            'lead_id': lead['post_id'],
            'subject': f"Re: {lead['title'][:50]}",
            'message': f"""Hey u/{lead['author']},

Saw your post about {primary_topic}. I built SAGE - an AI code review tool that might help.

Key features:
- AI-powered insights (not just linting)
- 30-second full repo analysis
- Works with any language
- Zero config needed

Unlike Codacy/SonarQube which need setup, SAGE is one command:
```
sage review /path/to/repo
```

Want to try it free on your project?

- Eden
""",
            'generated_at': datetime.now().isoformat(),
            'call_to_action': 'Free SAGE review available'
        }
        
        return pitch
    
    def monitor_platforms(self):
        """Check platforms for new leads"""
        print("   🔍 Monitoring platforms...")
        
        new_leads = []
        
        for platform in self.platforms:
            data = self.fetch_json(platform['url'], platform['name'])
            
            if data and platform['type'] == 'reddit':
                posts = data.get('data', {}).get('children', [])
                
                for post in posts:
                    lead = self.analyze_reddit_post(post)
                    
                    if lead:
                        # Check not duplicate
                        existing = any(l['post_id'] == lead['post_id'] for l in self.leads)
                        
                        if not existing:
                            lead['quality_score'] = self.score_lead_quality(lead)
                            
                            if lead['quality_score'] >= 50:  # Lower threshold
                                new_leads.append(lead)
                                print(f"      🎯 Lead: {lead['title'][:50]}... (score: {lead['quality_score']})")
            
            time.sleep(2)
        
        return new_leads
    
    def generate_outreach(self, leads):
        """Generate personalized outreach"""
        print(f"   ✉️  Generating outreach for {len(leads)} leads...")
        
        outreach_generated = []
        
        for lead in leads:
            pitch = self.generate_personalized_pitch(lead)
            
            filename = f'/Eden/OUTREACH/outreach_{lead["post_id"]}_{int(time.time())}.json'
            with open(filename, 'w') as f:
                json.dump({
                    'lead': lead,
                    'pitch': pitch,
                    'status': 'ready'
                }, f, indent=2)
            
            outreach_generated.append(filename)
            lead['outreach_generated'] = filename
            lead['status'] = 'outreach_ready'
        
        return outreach_generated
    
    def acquisition_cycle(self):
        print(f"\n{'='*70}")
        print(f"🎯 ACQUISITION CYCLE #{self.cycle_count + 1}")
        print(f"{'='*70}")
        
        new_leads = self.monitor_platforms()
        
        if new_leads:
            print(f"   ✅ Discovered {len(new_leads)} quality leads")
            
            outreach = self.generate_outreach(new_leads)
            
            self.leads.extend(new_leads)
            self.save_leads()
            
            print(f"   ✅ Generated {len(outreach)} pitches")
            print(f"   📊 Total leads: {len(self.leads)}")
        else:
            print(f"   ℹ️  No new leads this cycle")
        
        print(f"   ✅ Cycle complete")
        
        self.cycle_count += 1
        print()
    
    def run_forever(self):
        print("🚀 AUTONOMOUS CLIENT ACQUISITION v2\n")
        print("   Monitoring Reddit hot/new posts")
        print("   Broader keyword detection")
        print("   Cycle every 10 minutes")
        print()
        
        while True:
            try:
                self.acquisition_cycle()
                print("   💤 Next cycle in 10 minutes...\n")
                time.sleep(600)
            except KeyboardInterrupt:
                print(f"\n\n🛑 Stopped - {len(self.leads)} total leads")
                break
            except Exception as e:
                print(f"\n⚠️ Error: {e}")
                time.sleep(60)

if __name__ == "__main__":
    acquisition = AutonomousClientAcquisition()
    acquisition.run_forever()
