#!/usr/bin/env python3
"""Draft fresh leads for comparison page."""
import os
import sys
sys.path.insert(0, '/root/alwaysbeselling')

from dotenv import load_dotenv
load_dotenv('/root/alwaysbeselling/.env')

from state.backlog import load_backlog, save_backlog
from capabilities.scoring import score_lead, calculate_priority
from capabilities.enrichment import enrich_lead
from capabilities.drafting import draft_touch
import time

backlog = load_backlog()

# Get fresh leads that aren't drafted yet
fresh = [l for l in backlog if l.get('temporal_category') in ('fresh', 'recent') and not l.get('draft_body')]
fresh.sort(key=lambda x: -x.get('priority', 0))

print(f"Fresh leads without drafts: {len(fresh)}")

# We need fresh leads from different industries
target_naics = ['53', '81', '72', '56', '23', '54', '52', '44']

drafted = 0
for lead in fresh:
    naics = lead.get('naics', '')
    digits = ''.join(c for c in naics if c.isdigit())[:2] if naics else '00'
    
    if digits in target_naics and not lead.get('draft_body'):
        name = lead.get('name', 'Unknown')[:35]
        lead_id = lead.get('id')
        print(f"Drafting: {name} (NAICS {digits})")
        try:
            # Enrich first if not enriched
            if not lead.get('brave_summary'):
                enrich_lead(lead_id)
                time.sleep(0.3)
            draft_touch(lead_id)
            drafted += 1
            time.sleep(1.5)
        except Exception as e:
            print(f"  Error: {e}")
    
    if drafted >= 10:
        break

print(f"\nDrafted {drafted} additional fresh leads")