<?php
if ( ! defined( 'ABSPATH' ) ) exit;

/**
 * VMO Deep Research System - PHASE 3 COMPLETE IMPLEMENTATION
 * 
 * Performs comprehensive competitive intelligence, market research, 
 * and strategic analysis for business and website strategy.
 * 
 * ✅ VERSION 2.0 - Full Phase 3 Specification
 * - 10-12 individual competitor deep dives
 * - Per-competitor website analysis
 * - Per-competitor SEO benchmarking
 * - Comparative matrices
 * - Gap analysis & strategic advantage mapping
 * - 8-chunk execution for reliability (up to 20 minutes)
 * - Competitor review intelligence & customer complaint analysis
 */

if (!defined('ABSPATH')) exit;

class VMO_Deep_Research {
    private static $instance = null;
    
    private $api_key;
    private $serpapi_key;
    private $discovered_competitors = [];
    private $skip_internal_token_deduction = false;
    
    // Total chunks for complete Phase 3 - SPLIT INTO MICRO-CHUNKS TO PREVENT NGINX TIMEOUTS
    const TOTAL_CHUNKS = 29; // Split ALL chunks to 1 API call max to prevent nginx 504 timeouts

    public static function get_instance() {
        if (self::$instance instanceof self) {
            return self::$instance;
        }
        self::$instance = new self();
        return self::$instance;
    }
    
    public function __construct() {
        if (!(self::$instance instanceof self)) {
            self::$instance = $this;
        }
        $this->api_key = get_option('vmo_openai_api_key');
        $this->serpapi_key = get_option('vmo_serpapi_key');
    }

    /**
     * Perform a live web search using SerpAPI when configured, with an HTML search fallback.
     */
    public function perform_live_search($query, array $options = array()) {
        $query = trim((string) $query);
        if ($query === '') {
            return array(
                'query' => '',
                'source' => 'none',
                'results' => array(),
                'retrieved_at' => current_time('mysql'),
            );
        }

        $limit = max(1, min(10, (int) ($options['limit'] ?? 5)));
        $location = sanitize_text_field($options['location'] ?? '');
        $page_scan_limit = max(0, min(3, (int) ($options['page_scan_limit'] ?? 0)));
        $cache_key = 'vmo_live_search_' . md5(strtolower($query . '|' . $location . '|' . $limit . '|' . $page_scan_limit));
        $cached = get_transient($cache_key);
        if (is_array($cached) && isset($cached['results'])) {
            return $cached;
        }

        $payload = !empty($this->serpapi_key)
            ? $this->perform_serpapi_search($query, $limit, $location)
            : $this->perform_duckduckgo_html_search($query, $limit);

        $payload['query'] = $query;
        $payload['results'] = $this->enrich_search_results_with_pages($payload['results'] ?? array(), $page_scan_limit);
        $payload['retrieved_at'] = current_time('mysql');

        set_transient($cache_key, $payload, 6 * HOUR_IN_SECONDS);

        return $payload;
    }

    private function perform_serpapi_search($query, $limit = 5, $location = '') {
        $url = add_query_arg(array(
            'engine' => 'google',
            'q' => $query,
            'num' => $limit,
            'api_key' => $this->serpapi_key,
        ), 'https://serpapi.com/search.json');

        if ($location !== '') {
            $url = add_query_arg('location', $location, $url);
        }

        $response = wp_remote_get($url, array(
            'timeout' => 30,
            'headers' => array(
                'Accept' => 'application/json',
            ),
        ));

        if (is_wp_error($response)) {
            error_log('VMO Deep Research: SerpAPI search failed: ' . $response->get_error_message());
            return array(
                'source' => 'serpapi_error',
                'results' => array(),
                'error' => $response->get_error_message(),
            );
        }

        $body = json_decode(wp_remote_retrieve_body($response), true);
        $organic_results = is_array($body['organic_results'] ?? null) ? $body['organic_results'] : array();
        $results = array();

        foreach (array_slice($organic_results, 0, $limit) as $item) {
            $results[] = $this->normalize_search_result(
                $item['title'] ?? '',
                $item['link'] ?? '',
                $item['snippet'] ?? '',
                (int) ($item['position'] ?? (count($results) + 1)),
                'serpapi'
            );
        }

        return array(
            'source' => 'serpapi',
            'results' => array_values(array_filter($results)),
        );
    }

    private function perform_duckduckgo_html_search($query, $limit = 5) {
        $response = wp_remote_get(
            'https://html.duckduckgo.com/html/?q=' . rawurlencode($query),
            array(
                'timeout' => 30,
                'user-agent' => 'Mozilla/5.0 (compatible; VMODeepResearch/1.0)',
            )
        );

        if (is_wp_error($response)) {
            error_log('VMO Deep Research: DuckDuckGo fallback search failed: ' . $response->get_error_message());
            return array(
                'source' => 'fallback_search_error',
                'results' => array(),
                'error' => $response->get_error_message(),
            );
        }

        $html = wp_remote_retrieve_body($response);
        if (!is_string($html) || trim($html) === '') {
            return array(
                'source' => 'fallback_search_empty',
                'results' => array(),
            );
        }

        libxml_use_internal_errors(true);
        $doc = new DOMDocument();
        $doc->loadHTML('<?xml encoding="UTF-8">' . $html);
        $xpath = new DOMXPath($doc);
        $nodes = $xpath->query('//div[contains(@class, "result")]');
        $results = array();

        if ($nodes instanceof DOMNodeList) {
            foreach ($nodes as $index => $node) {
                if ($index >= $limit) {
                    break;
                }

                $title_node = $xpath->query('.//a[contains(@class, "result__a")]', $node);
                $snippet_node = $xpath->query('.//*[contains(@class, "result__snippet")]', $node);

                $title = ($title_node instanceof DOMNodeList && $title_node->length > 0)
                    ? trim($title_node->item(0)->textContent)
                    : '';
                $url = ($title_node instanceof DOMNodeList && $title_node->length > 0)
                    ? trim((string) $title_node->item(0)->getAttribute('href'))
                    : '';
                $snippet = ($snippet_node instanceof DOMNodeList && $snippet_node->length > 0)
                    ? trim($snippet_node->item(0)->textContent)
                    : '';

                $result = $this->normalize_search_result($title, $url, $snippet, $index + 1, 'duckduckgo_html');
                if (!empty($result)) {
                    $results[] = $result;
                }
            }
        }

        libxml_clear_errors();

        return array(
            'source' => 'duckduckgo_html',
            'results' => $results,
        );
    }

    private function normalize_search_result($title, $url, $snippet, $position, $source) {
        $url = esc_url_raw((string) $url);
        if ($url === '') {
            return array();
        }

        $host = wp_parse_url($url, PHP_URL_HOST);

        return array(
            'title' => sanitize_text_field((string) $title),
            'url' => $url,
            'domain' => sanitize_text_field((string) $host),
            'snippet' => sanitize_text_field((string) $snippet),
            'position' => (int) $position,
            'source' => sanitize_text_field((string) $source),
        );
    }

    private function enrich_search_results_with_pages(array $results, $page_scan_limit = 0) {
        if ($page_scan_limit <= 0) {
            return $results;
        }

        $enriched = array();
        foreach ($results as $index => $result) {
            if ($index < $page_scan_limit) {
                $result['page_snapshot'] = $this->build_search_result_page_snapshot($result['url'] ?? '');
            }
            $enriched[] = $result;
        }

        return $enriched;
    }

    private function build_search_result_page_snapshot($url) {
        if (empty($url) || !function_exists('vmo_website_scanner')) {
            return array();
        }

        $scanner = vmo_website_scanner();
        if (!is_object($scanner) || !method_exists($scanner, 'fetch_url') || !method_exists($scanner, 'extract_content')) {
            return array();
        }

        $fetch = $scanner->fetch_url($url);
        if (is_wp_error($fetch) || empty($fetch['html'])) {
            return array();
        }

        $content = $scanner->extract_content($fetch['html']);
        $headings = array();
        foreach (array_slice($content['headings'] ?? array(), 0, 5) as $heading) {
            if (!empty($heading['text'])) {
                $headings[] = sanitize_text_field($heading['text']);
            }
        }

        return array(
            'title' => sanitize_text_field($content['title'] ?? ''),
            'meta_description' => sanitize_text_field($content['meta']['description'] ?? ''),
            'headings' => $headings,
            'excerpt' => sanitize_text_field(substr((string) ($content['text_content'] ?? ''), 0, 500)),
        );
    }

    private function build_live_search_evidence(array $queries, array $options = array()) {
        $limit = max(1, min(6, (int) ($options['limit'] ?? 4)));
        $page_scan_limit = max(0, min(2, (int) ($options['page_scan_limit'] ?? 1)));
        $location = sanitize_text_field($options['location'] ?? '');
        $max_results_per_query = max(1, min(5, (int) ($options['max_results_per_query'] ?? 3)));

        $searches = array();
        $blocks = array();
        foreach (array_values(array_unique(array_filter(array_map('trim', $queries)))) as $query) {
            $search = $this->perform_live_search($query, array(
                'limit' => $limit,
                'location' => $location,
                'page_scan_limit' => $page_scan_limit,
            ));
            $searches[] = $search;

            if (!empty($search['results'])) {
                $blocks[] = 'QUERY: ' . $query . "\n" . $this->format_search_results_for_prompt($search['results'], $max_results_per_query);
            }
        }

        return array(
            'text' => implode("\n\n", $blocks),
            'searches' => $searches,
            'result_count' => array_sum(array_map(function ($search) {
                return is_array($search['results'] ?? null) ? count($search['results']) : 0;
            }, $searches)),
        );
    }

    private function format_search_results_for_prompt(array $results, $max_results = 3) {
        $lines = array();
        foreach (array_slice($results, 0, $max_results) as $result) {
            $line = ($result['position'] ?? '?') . '. ' . ($result['title'] ?? 'Untitled');
            $line .= "\nURL: " . ($result['url'] ?? '');
            if (!empty($result['snippet'])) {
                $line .= "\nSnippet: " . $result['snippet'];
            }
            if (!empty($result['page_snapshot']['meta_description'])) {
                $line .= "\nMeta Description: " . $result['page_snapshot']['meta_description'];
            }
            if (!empty($result['page_snapshot']['headings'])) {
                $line .= "\nHeadings: " . implode(' | ', array_slice($result['page_snapshot']['headings'], 0, 3));
            }
            if (!empty($result['page_snapshot']['excerpt'])) {
                $line .= "\nPage Excerpt: " . $result['page_snapshot']['excerpt'];
            }
            $lines[] = $line;
        }

        return implode("\n\n", $lines);
    }
    
    /**
     * ✅ PHASE 3 COMPLETE: Comprehensive Deep Research
     * 
     * 8-CHUNK ARCHITECTURE (prevents timeouts, enables progress tracking):
     * 
     * Chunk 1: Core Foundation (4 calls, ~3 min)
     *   - Vision/intent analysis
     *   - Business structure validation
     *   - Target market intelligence
     *   - Target audience personas
     * 
     * Chunk 2: Competitor Discovery (3 calls, ~3 min)
     *   - Identify 10-12 competitors
     *   - Industry landscape overview
     *   - Market positioning map
     * 
     * Chunk 3: Competitor Business Analysis (3 calls, ~3 min)
     *   - Competitors 1-4 business model deep dive
     *   - Competitors 5-8 business model deep dive
     *   - Competitors 9-12 business model deep dive
     * 
     * Chunk 4: Competitor Website Analysis (3 calls, ~3 min)
     *   - Competitors 1-4 website/UX analysis
     *   - Competitors 5-8 website/UX analysis
     *   - Competitors 9-12 website/UX analysis
     * 
     * Chunk 5: Competitor SEO & Content (3 calls, ~3 min)
     *   - Competitors 1-4 SEO strategy
     *   - Competitors 5-8 SEO strategy
     *   - Competitors 9-12 SEO strategy
     * 
     * Chunk 6: Market Strategy (4 calls, ~3 min)
     *   - Market trends
     *   - Pricing strategy
     *   - Marketing channels
     *   - Operations analysis
     * 
     * Chunk 7: Technical & Compliance (4 calls, ~3 min)
     *   - Technology stack recommendations
     *   - Legal compliance requirements
     *   - Keyword research
     *   - Brand positioning
     * 
     * Chunk 8: Synthesis & Strategic Output (4 calls, ~3 min)
     *   - Comparative matrix generation
     *   - Market gap analysis
     *   - Strategic advantage mapping
     *   - Final recommendations & success metrics
     * 
     * Total: ~28 API calls, ~20-24 minutes, bulletproof execution
     */
    public function conduct_deep_research($business_data, $chunk = 0) {
        try {
            if (!is_array($business_data)) {
                $business_data = [];
            }

            $this->skip_internal_token_deduction = !empty($business_data['_skip_internal_token_deduction']);
            unset($business_data['_skip_internal_token_deduction']);

            error_log('🔍 VMO Phase 3: Starting COMPREHENSIVE research (chunk=' . $chunk . ')...');
            error_log('🔍 VMO Phase 3: Business data: ' . json_encode(array_keys($business_data ?: [])));
            
            // Extended execution time per chunk - UNLIMITED to prevent timeouts
            if (function_exists('set_time_limit')) {
                @set_time_limit(0); // Unlimited - deep research must never timeout
            }
            if (function_exists('ignore_user_abort')) {
                @ignore_user_abort(true); // Continue even if user navigates away
            }
            
            $user_id = get_current_user_id();
            $existing_results = $this->get_existing_research_results($user_id);
            
            // Initialize or continue research results
            $research_results = is_array($existing_results) ? $existing_results : [
                'research_timestamp' => current_time('mysql'),
                'research_framework' => 'phase3-complete-v2',
                'business_context' => $business_data,
                'chunk_completed' => 0,
                'competitors_list' => [],
                'competitor_analyses' => [],
                'comparative_matrix' => [],
                'market_gaps' => [],
                'strategic_advantages' => [],
                'research_summary' => ''
            ];
            
            // Determine which chunk to run
            $run_chunk = $chunk > 0 ? $chunk : (($research_results['chunk_completed'] ?? 0) + 1);
            
            error_log("🔍 VMO Phase 3: Executing chunk {$run_chunk} of " . self::TOTAL_CHUNKS);
            
            switch ($run_chunk) {
                // ===== FOUNDATION (Chunks 1-4): Split from original chunk 1 =====
                case 1:
                    $research_results = $this->execute_chunk_01_vision($business_data, $research_results);
                    break;
                case 2:
                    $research_results = $this->execute_chunk_02_structure($business_data, $research_results);
                    break;
                case 3:
                    $research_results = $this->execute_chunk_03_target_market($business_data, $research_results);
                    break;
                case 4:
                    $research_results = $this->execute_chunk_04_audience($business_data, $research_results);
                    break;
                    
                // ===== COMPETITOR DISCOVERY (Chunks 5-6): Split from original chunk 2 =====
                case 5:
                    $research_results = $this->execute_chunk_05_discover_competitors($business_data, $research_results);
                    break;
                case 6:
                    $research_results = $this->execute_chunk_06_industry_positioning($business_data, $research_results);
                    break;
                    
                // ===== COMPETITOR BUSINESS (Chunks 7-9): Split from original chunk 3 =====
                case 7:
                    $research_results = $this->execute_chunk_07_business_batch1($business_data, $research_results);
                    break;
                case 8:
                    $research_results = $this->execute_chunk_08_business_batch2($business_data, $research_results);
                    break;
                case 9:
                    $research_results = $this->execute_chunk_09_business_batch3($business_data, $research_results);
                    break;
                    
                // ===== COMPETITOR WEBSITE (Chunks 10-12): Split from original chunk 4 =====
                case 10:
                    $research_results = $this->execute_chunk_10_website_batch1($business_data, $research_results);
                    break;
                case 11:
                    $research_results = $this->execute_chunk_11_website_batch2($business_data, $research_results);
                    break;
                case 12:
                    $research_results = $this->execute_chunk_12_website_batch3($business_data, $research_results);
                    break;
                    
                // ===== COMPETITOR SEO (Chunks 13-15): Split from original chunk 5 =====
                case 13:
                    $research_results = $this->execute_chunk_13_seo_batch1($business_data, $research_results);
                    break;
                case 14:
                    $research_results = $this->execute_chunk_14_seo_batch2($business_data, $research_results);
                    break;
                case 15:
                    $research_results = $this->execute_chunk_15_seo_batch3($business_data, $research_results);
                    break;
                    
                // ===== MARKET STRATEGY (Chunks 16-19): Split from original chunk 6 =====
                case 16:
                    $research_results = $this->execute_chunk_16_market_trends($business_data, $research_results);
                    break;
                case 17:
                    $research_results = $this->execute_chunk_17_pricing($business_data, $research_results);
                    break;
                case 18:
                    $research_results = $this->execute_chunk_18_marketing($business_data, $research_results);
                    break;
                case 19:
                    $research_results = $this->execute_chunk_19_operations($business_data, $research_results);
                    break;
                    
                // ===== TECHNICAL (Chunks 20-23): Split from original chunk 7 =====
                case 20:
                    $research_results = $this->execute_chunk_20_tech_stack($business_data, $research_results);
                    break;
                case 21:
                    $research_results = $this->execute_chunk_21_legal($business_data, $research_results);
                    break;
                case 22:
                    $research_results = $this->execute_chunk_22_keywords($business_data, $research_results);
                    break;
                case 23:
                    $research_results = $this->execute_chunk_23_brand($business_data, $research_results);
                    break;
                    
                // ===== SYNTHESIS (Chunks 24-25): Final analysis =====
                case 24:
                    $research_results = $this->execute_chunk_24_synthesis($business_data, $research_results);
                    break;
                case 25:
                    $research_results = $this->execute_chunk_25_final_summary($business_data, $research_results);
                    break;
                    
                // ===== COMPETITOR REVIEW INTELLIGENCE (Chunk 26: batch 1, Chunk 29: batch 2) =====
                case 26:
                    $research_results = $this->execute_chunk_26_review_intelligence($business_data, $research_results);
                    break;
                    
                // ===== SPLIT CHUNKS 27-29: Extracted from chunks 6, 24, 26 to prevent timeouts =====
                case 27:
                    $research_results = $this->execute_chunk_27_market_positioning($business_data, $research_results);
                    break;
                case 28:
                    $research_results = $this->execute_chunk_28_structured_market_intel($business_data, $research_results);
                    break;
                case 29:
                    $research_results = $this->execute_chunk_29_review_intelligence_batch2($business_data, $research_results);
                    break;
                    
                default:
                    error_log('✅ VMO Phase 3: All chunks already completed');
                    break;
            }
            
            // Persist after each chunk
            $this->store_research_results($user_id, $research_results);
            
            $is_complete = ($research_results['chunk_completed'] ?? 0) >= self::TOTAL_CHUNKS;
            
            error_log('✅ VMO Phase 3: Chunk ' . $run_chunk . ' done. Complete: ' . ($is_complete ? 'YES' : 'NO'));
            
            return array_merge($research_results, [
                'is_complete' => $is_complete,
                'current_chunk' => $run_chunk,
                'total_chunks' => self::TOTAL_CHUNKS,
                'progress_percent' => round(($run_chunk / self::TOTAL_CHUNKS) * 100)
            ]);
            
        } catch (\Throwable $e) {
            error_log('❌ VMO Phase 3 ERROR: ' . $e->getMessage());
            error_log('❌ Stack: ' . $e->getTraceAsString());
            
            // ✅ BULLETPROOF: Never throw - return valid response with error flag
            // This ensures the AJAX handler ALWAYS returns valid JSON
            if (!isset($research_results) || !is_array($research_results)) {
                $research_results = [];
            }
            $run_chunk = isset($run_chunk) ? intval($run_chunk) : 1;
            if ($run_chunk < 1) {
                $run_chunk = 1;
            }
            $research_results['chunk_completed'] = $run_chunk;
            $research_results['chunk_error'] = $e->getMessage();
            
            // Save what we have even on error
            try {
                $this->store_research_results($user_id ?? 0, $research_results);
            } catch (\Throwable $store_error) {
                error_log('⚠️ VMO Phase 3: Failed to store partial results after chunk error: ' . $store_error->getMessage());
            }
            
            $is_complete = ($research_results['chunk_completed'] ?? 0) >= self::TOTAL_CHUNKS;
            
            return array_merge($research_results, [
                'is_complete' => $is_complete,
                'current_chunk' => $run_chunk,
                'total_chunks' => self::TOTAL_CHUNKS,
                'progress_percent' => round(($run_chunk / self::TOTAL_CHUNKS) * 100),
                'error' => 'Chunk ' . $run_chunk . ' had an error but research continues',
                'can_retry' => true
            ]);
        }
    }
    
    // =========================================================================
    // CHUNK EXECUTION METHODS - 25 CHUNKS (1 API call each to prevent timeout)
    // =========================================================================
    
    // ===== FOUNDATION CHUNKS (1-4) =====
    
    /**
     * CHUNK 1: Vision & Intent Analysis (1 API call)
     */
    private function execute_chunk_01_vision($business_data, $results) {
        error_log('📊 Chunk 1/25: Vision & Intent Analysis');
        $results['vision_analysis'] = $this->safe_analysis('vision_and_intent', $business_data);
        $results['chunk_completed'] = 1;
        error_log('✅ Chunk 1 complete');
        return $results;
    }
    
    /**
     * CHUNK 2: Business Structure Analysis (1 API call)
     */
    private function execute_chunk_02_structure($business_data, $results) {
        error_log('📊 Chunk 2/25: Business Structure Analysis');
        $results['structure_analysis'] = $this->safe_analysis('business_structure', $business_data);
        $results['chunk_completed'] = 2;
        error_log('✅ Chunk 2 complete');
        return $results;
    }
    
    /**
     * CHUNK 3: Target Market Intelligence (1 API call)
            "company_name": "",
    private function execute_chunk_03_target_market($business_data, $results) {
        error_log('📊 Chunk 3/25: Target Market Intelligence');
        $results['target_market_intelligence'] = $this->safe_analysis('target_market_intelligence', $business_data);
            "website": "",
        error_log('✅ Chunk 3 complete');
        return $results;
    }
    
    /**
            "evidence_source": ""
     */
    private function execute_chunk_04_audience($business_data, $results) {
        error_log('📊 Chunk 4/25: Target Audience Analysis');
        $results['target_audience_analysis'] = $this->safe_analysis('target_audience', $business_data);
        $results['chunk_completed'] = 4;
        error_log('✅ Chunk 4 complete');
        return $results;
    }
    
    // ===== COMPETITOR DISCOVERY CHUNKS (5-6) =====
    
    /**
     * CHUNK 5: Discover Competitors (1 API call)
     */
    private function execute_chunk_05_discover_competitors($business_data, $results) {
        error_log('📊 Chunk 5/25: Discovering Competitors');
        $competitors = $this->discover_competitors($business_data);
        $results['competitors_list'] = $competitors;
        $results['chunk_completed'] = 5;
        error_log('✅ Chunk 5 complete - Found ' . count($competitors) . ' competitors');
        return $results;
    }
    
    /**
     * CHUNK 6: Industry & Market Positioning (1 API call)
     */
    private function execute_chunk_06_industry_positioning($business_data, $results) {
        error_log('📊 Chunk 6/29: Industry Insights (1 API call)');
        // ✅ BOTTLENECK FIX: Split from 2 API calls to 1. Market positioning moved to chunk 27.
        $results['industry_insights'] = $this->safe_analysis('industry_insights', $business_data);
        $results['chunk_completed'] = 6;
        error_log('✅ Chunk 6 complete');
        return $results;
    }
    
    // ===== COMPETITOR BUSINESS ANALYSIS CHUNKS (7-9) =====
    
    /**
     * CHUNK 7: Competitor Business Batch 1 (1 API call)
     */
    private function execute_chunk_07_business_batch1($business_data, $results) {
        error_log('📊 Chunk 7/25: Competitor Business Batch 1');
        $competitors = $results['competitors_list'] ?? [];
        if (empty($competitors)) {
            $results['competitor_business_batch_1'] = $this->safe_analysis('competitors', $business_data);
        } else {
            $batch = array_slice($competitors, 0, 4);
            $results['competitor_business_batch_1'] = $this->analyze_competitors_business_batch($business_data, $batch, '1-4');
        }
        $results['chunk_completed'] = 7;
        error_log('✅ Chunk 7 complete');
        return $results;
    }
    
    /**
     * CHUNK 8: Competitor Business Batch 2 (1 API call)
     */
    private function execute_chunk_08_business_batch2($business_data, $results) {
        error_log('📊 Chunk 8/25: Competitor Business Batch 2');
        $competitors = $results['competitors_list'] ?? [];
        if (count($competitors) > 4) {
            $batch = array_slice($competitors, 4, 4);
            $results['competitor_business_batch_2'] = $this->analyze_competitors_business_batch($business_data, $batch, '5-8');
        } else {
            $results['competitor_business_batch_2'] = ['analysis' => 'Batch 2 skipped - fewer than 5 competitors', 'timestamp' => current_time('mysql')];
        }
        $results['chunk_completed'] = 8;
        error_log('✅ Chunk 8 complete');
        return $results;
    }
    
    /**
     * CHUNK 9: Competitor Business Batch 3 (1 API call)
     */
    private function execute_chunk_09_business_batch3($business_data, $results) {
        error_log('📊 Chunk 9/25: Competitor Business Batch 3');
        $competitors = $results['competitors_list'] ?? [];
        if (count($competitors) > 8) {
            $batch = array_slice($competitors, 8, 4);
            $results['competitor_business_batch_3'] = $this->analyze_competitors_business_batch($business_data, $batch, '9-12');
        } else {
            $results['competitor_business_batch_3'] = ['analysis' => 'Batch 3 skipped - fewer than 9 competitors', 'timestamp' => current_time('mysql')];
        }
        $results['chunk_completed'] = 9;
        error_log('✅ Chunk 9 complete');
        return $results;
    }
    
    // ===== COMPETITOR WEBSITE ANALYSIS CHUNKS (10-12) =====
    
    /**
     * CHUNK 10: Competitor Website Batch 1 (1 API call)
     */
    private function execute_chunk_10_website_batch1($business_data, $results) {
        error_log('📊 Chunk 10/25: Competitor Website Batch 1');
        $competitors = $results['competitors_list'] ?? [];
        if (empty($competitors)) {
            $results['competitor_website_batch_1'] = $this->analyze_website_best_practices($business_data);
        } else {
            $batch = array_slice($competitors, 0, 4);
            $results['competitor_website_batch_1'] = $this->analyze_competitors_website_batch($business_data, $batch, '1-4');
        }
        $results['chunk_completed'] = 10;
        error_log('✅ Chunk 10 complete');
        return $results;
    }
    
    /**
     * CHUNK 11: Competitor Website Batch 2 (1 API call)
     */
    private function execute_chunk_11_website_batch2($business_data, $results) {
        error_log('📊 Chunk 11/25: Competitor Website Batch 2');
        $competitors = $results['competitors_list'] ?? [];
        if (count($competitors) > 4) {
            $batch = array_slice($competitors, 4, 4);
            $results['competitor_website_batch_2'] = $this->analyze_competitors_website_batch($business_data, $batch, '5-8');
        } else {
            $results['competitor_website_batch_2'] = ['analysis' => 'Batch 2 skipped - fewer than 5 competitors', 'timestamp' => current_time('mysql')];
        }
        $results['chunk_completed'] = 11;
        error_log('✅ Chunk 11 complete');
        return $results;
    }
    
    /**
     * CHUNK 12: Competitor Website Batch 3 (1 API call)
     */
    private function execute_chunk_12_website_batch3($business_data, $results) {
        error_log('📊 Chunk 12/25: Competitor Website Batch 3');
        $competitors = $results['competitors_list'] ?? [];
        if (count($competitors) > 8) {
            $batch = array_slice($competitors, 8, 4);
            $results['competitor_website_batch_3'] = $this->analyze_competitors_website_batch($business_data, $batch, '9-12');
        } else {
            $results['competitor_website_batch_3'] = ['analysis' => 'Batch 3 skipped - fewer than 9 competitors', 'timestamp' => current_time('mysql')];
        }
        $results['chunk_completed'] = 12;
        error_log('✅ Chunk 12 complete');
        return $results;
    }
    
    // ===== COMPETITOR SEO ANALYSIS CHUNKS (13-15) =====
    
    /**
     * CHUNK 13: Competitor SEO Batch 1 (1 API call)
     */
    private function execute_chunk_13_seo_batch1($business_data, $results) {
        error_log('📊 Chunk 13/25: Competitor SEO Batch 1');
        $competitors = $results['competitors_list'] ?? [];
        if (empty($competitors)) {
            $results['competitor_seo_batch_1'] = $this->safe_analysis('keyword_research', $business_data);
        } else {
            $batch = array_slice($competitors, 0, 4);
            $results['competitor_seo_batch_1'] = $this->analyze_competitors_seo_batch($business_data, $batch, '1-4');
        }
        $results['chunk_completed'] = 13;
        error_log('✅ Chunk 13 complete');
        return $results;
    }
    
    /**
     * CHUNK 14: Competitor SEO Batch 2 (1 API call)
     */
    private function execute_chunk_14_seo_batch2($business_data, $results) {
        error_log('📊 Chunk 14/25: Competitor SEO Batch 2');
        $competitors = $results['competitors_list'] ?? [];
        if (count($competitors) > 4) {
            $batch = array_slice($competitors, 4, 4);
            $results['competitor_seo_batch_2'] = $this->analyze_competitors_seo_batch($business_data, $batch, '5-8');
        } else {
            $results['competitor_seo_batch_2'] = ['analysis' => 'Batch 2 skipped - fewer than 5 competitors', 'timestamp' => current_time('mysql')];
        }
        $results['chunk_completed'] = 14;
        error_log('✅ Chunk 14 complete');
        return $results;
    }
    
    /**
     * CHUNK 15: Competitor SEO Batch 3 (1 API call)
     */
    private function execute_chunk_15_seo_batch3($business_data, $results) {
        error_log('📊 Chunk 15/25: Competitor SEO Batch 3');
        $competitors = $results['competitors_list'] ?? [];
        if (count($competitors) > 8) {
            $batch = array_slice($competitors, 8, 4);
            $results['competitor_seo_batch_3'] = $this->analyze_competitors_seo_batch($business_data, $batch, '9-12');
        } else {
            $results['competitor_seo_batch_3'] = ['analysis' => 'Batch 3 skipped - fewer than 9 competitors', 'timestamp' => current_time('mysql')];
        }
        $results['chunk_completed'] = 15;
        error_log('✅ Chunk 15 complete');
        return $results;
    }
    
    // ===== MARKET STRATEGY CHUNKS (16-19) =====
    
    /**
     * CHUNK 16: Market Trends (1 API call)
     */
    private function execute_chunk_16_market_trends($business_data, $results) {
        error_log('📊 Chunk 16/25: Market Trends Analysis');
        $results['market_trends'] = $this->safe_analysis('market_trends', $business_data);
        $results['chunk_completed'] = 16;
        error_log('✅ Chunk 16 complete');
        return $results;
    }
    
    /**
     * CHUNK 17: Pricing Strategy (1 API call)
     */
    private function execute_chunk_17_pricing($business_data, $results) {
        error_log('📊 Chunk 17/25: Pricing Strategy Analysis');
        $results['pricing_analysis'] = $this->safe_analysis('pricing_strategy', $business_data);
        $results['chunk_completed'] = 17;
        error_log('✅ Chunk 17 complete');
        return $results;
    }
    
    /**
     * CHUNK 18: Marketing Strategy (1 API call)
     */
    private function execute_chunk_18_marketing($business_data, $results) {
        error_log('📊 Chunk 18/25: Marketing Strategy Analysis');
        $results['marketing_strategy'] = $this->safe_analysis('marketing_strategy', $business_data);
        $results['chunk_completed'] = 18;
        error_log('✅ Chunk 18 complete');
        return $results;
    }
    
    /**
     * CHUNK 19: Operations & Delivery (1 API call)
     */
    private function execute_chunk_19_operations($business_data, $results) {
        error_log('📊 Chunk 19/25: Operations & Delivery Analysis');
        $results['operations_analysis'] = $this->safe_analysis('operations_and_delivery', $business_data);
        $results['chunk_completed'] = 19;
        error_log('✅ Chunk 19 complete');
        return $results;
    }
    
    // ===== TECHNICAL CHUNKS (20-23) =====
    
    /**
     * CHUNK 20: Technology Stack (1 API call)
     */
    private function execute_chunk_20_tech_stack($business_data, $results) {
        error_log('📊 Chunk 20/25: Technology Stack Analysis');
        $results['technology_stack'] = $this->safe_analysis('technology_stack', $business_data);
        $results['chunk_completed'] = 20;
        error_log('✅ Chunk 20 complete');
        return $results;
    }
    
    /**
     * CHUNK 21: Legal & Compliance (1 API call)
     */
    private function execute_chunk_21_legal($business_data, $results) {
        error_log('📊 Chunk 21/25: Legal & Compliance Analysis');
        $results['legal_compliance'] = $this->safe_analysis('legal_compliance', $business_data);
        $results['chunk_completed'] = 21;
        error_log('✅ Chunk 21 complete');
        return $results;
    }
    
    /**
     * CHUNK 22: Keyword Research (1 API call)
     */
    private function execute_chunk_22_keywords($business_data, $results) {
        error_log('📊 Chunk 22/25: Keyword Research');
        $results['keyword_research'] = $this->safe_analysis('keyword_research', $business_data);
        $results['chunk_completed'] = 22;
        error_log('✅ Chunk 22 complete');
        return $results;
    }
    
    /**
     * CHUNK 23: Brand Positioning (1 API call)
     */
    private function execute_chunk_23_brand($business_data, $results) {
        error_log('📊 Chunk 23/25: Brand Positioning Analysis');
        $results['brand_positioning'] = $this->safe_analysis('brand_positioning', $business_data);
        $results['chunk_completed'] = 23;
        error_log('✅ Chunk 23 complete');
        return $results;
    }
    
    // ===== SYNTHESIS CHUNKS (24-25) =====
    
    /**
     * CHUNK 24: Synthesis & Strategic Analysis (2 API calls)
     * ✅ ENHANCED: Now includes structured market intelligence with financial data
     */
    /**
     * CHUNK 24: Synthesis - LIGHTWEIGHT VERSION (1 API call max)
     * ✅ OPTIMIZED: Reduced from 5 API calls to 1 to prevent nginx 504 timeouts
     */
    private function execute_chunk_24_synthesis($business_data, $results) {
        error_log('📊 Chunk 24/29: Synthesis & Strategic Analysis (1 API call)');
        
        // Compile all competitor data for synthesis (no API call - just data gathering)
        $competitor_insights = [];
        for ($i = 1; $i <= 3; $i++) {
            if (!empty($results["competitor_business_batch_{$i}"])) {
                $competitor_insights[] = $results["competitor_business_batch_{$i}"]['analysis'] ?? '';
            }
            if (!empty($results["competitor_website_batch_{$i}"])) {
                $competitor_insights[] = $results["competitor_website_batch_{$i}"]['analysis'] ?? '';
            }
            if (!empty($results["competitor_seo_batch_{$i}"])) {
                $competitor_insights[] = $results["competitor_seo_batch_{$i}"]['analysis'] ?? '';
            }
        }
        
        // ✅ SINGLE API CALL: Combined synthesis (instead of 4 separate calls)
        // ✅ BOTTLENECK FIX: Structured market intel moved to chunk 28 (was 2 API calls, now 1)
        try {
            $combined_synthesis = $this->generate_combined_synthesis($business_data, $results, $competitor_insights);
            $results['comparative_matrix'] = ['analysis' => $combined_synthesis, 'source' => 'combined'];
            $results['market_gaps'] = ['analysis' => 'See comparative matrix for gap analysis', 'source' => 'combined'];
            $results['strategic_advantages'] = ['analysis' => 'See comparative matrix for strategic advantages', 'source' => 'combined'];
            $results['opportunities_threats'] = ['analysis' => 'Opportunities and threats analyzed in combined synthesis', 'source' => 'combined'];
        } catch (\Throwable $e) {
            error_log('⚠️ Chunk 24: Synthesis failed, using fallback: ' . $e->getMessage());
            $results['comparative_matrix'] = ['analysis' => 'See research details for competitor comparison.', 'source' => 'fallback'];
            $results['market_gaps'] = ['analysis' => 'Market gaps identified in competitor analysis.', 'source' => 'fallback'];
            $results['strategic_advantages'] = ['analysis' => 'Strategic advantages available in research dashboard.', 'source' => 'fallback'];
            $results['opportunities_threats'] = ['analysis' => 'SWOT analysis available in dashboard.', 'source' => 'fallback'];
        }
        
        $results['chunk_completed'] = 24;
        error_log('✅ Chunk 24 complete (1 API call only)');
        return $results;
    }
    
    /**
     * ✅ NEW: Combined synthesis - single API call instead of 4
     * Generates all synthesis data in one request to prevent timeouts
     */
    private function generate_combined_synthesis($business_data, $results, $competitor_insights) {
        $business_name = $business_data['business_name'] ?? 'Your Business';
        $industry = $business_data['industry'] ?? 'your industry';
        
        // Get competitor names
        $competitors = $results['competitors_list'] ?? [];
        $competitor_names = [];
        foreach (array_slice($competitors, 0, 8) as $competitor) {
            if (is_array($competitor) && !empty($competitor['name'])) {
                $competitor_names[] = $competitor['name'];
            }
        }
        $competitor_list = !empty($competitor_names) ? implode(', ', $competitor_names) : 'competitors identified in research';
        
        $prompt = "Provide a BRIEF strategic synthesis for {$business_name} in {$industry}.

Competitors analyzed: {$competitor_list}

In 300 words or less, summarize:
1. TOP 3 MARKET GAPS (opportunities competitors miss)
2. TOP 3 COMPETITIVE ADVANTAGES for {$business_name}
3. TOP 3 THREATS to watch
4. RECOMMENDED POSITIONING (one sentence)

Be specific and actionable. Use bullet points.";

        return $this->call_openai_analysis($prompt, 1500, 'pro');
    }
    
    /**
     * CHUNK 25: Final Summary - ULTRA-LIGHT VERSION (NO API CALLS)
     * ✅ ENHANCED: AI-generated leads with 3-tier fallback (primary → secondary → tertiary)
     * Uses an API call to find REAL leads, with template fallback if the call fails
     */
    private function execute_chunk_25_final_summary($business_data, $results) {
        error_log('📊 Chunk 25/29: Generating Final Research Summary + AI-Powered Leads');
        
        $business_name = $business_data['business_name'] ?? 'Your Business';
        $industry = $business_data['industry'] ?? 'general';
        $location = $business_data['location'] ?? 'United States';
        $target_market = $business_data['target_market'] ?? $business_data['target_audience'] ?? '';
        $offerings = $business_data['offerings'] ?? $business_data['services'] ?? '';

        $results['success_metrics'] = [
            'analysis' => "Success metrics for {$business_name}: Track website traffic, conversion rates, customer acquisition costs, and revenue growth. Monitor competitor movements and market trends monthly.",
            'source' => 'ai_enhanced',
            'generated_at' => current_time('mysql')
        ];

        // ✅ AI-POWERED LEADS: Generate real, actionable leads via GPT
        error_log('🎯 Chunk 25: Generating AI-powered leads with 3-tier hierarchy...');
        $leads_saved = 0;

        try {
            $ai_leads = $this->generate_ai_tiered_leads($business_data, $results);
            $structured_leads = [];
            $is_template = false;

            if (!empty($ai_leads) && is_array($ai_leads)) {
                $structured_leads = $ai_leads;
                error_log('✅ Chunk 25: AI returned ' . count($structured_leads) . ' tiered leads');
            } else {
                $structured_leads = [];
                $is_template = true;
                error_log('⚠️ Chunk 25: AI leads failed; no template leads will be saved to CRM');
            }

            $leads_result = [
                'raw_analysis' => $is_template
                    ? 'Template-based leads generated. Use the CRM to refine with AI later.'
                    : 'AI-generated tiered leads (primary, secondary, and tertiary) ready for outreach.',
                'structured_leads' => $structured_leads,
                'total_count' => count($structured_leads),
                'generated_at' => current_time('mysql'),
                'is_template' => $is_template
            ];
            $results['potential_leads'] = $leads_result;

            if (!empty($structured_leads)) {
                $leads_saved = $this->save_leads_to_crm(get_current_user_id(), $leads_result, $business_data);
            }
            error_log("✅ Chunk 25: Generated and saved {$leads_saved} leads to CRM");
        } catch (\Throwable $e) {
            error_log('⚠️ Chunk 25: Lead generation error; no template leads will be saved to CRM: ' . $e->getMessage());
            $results['potential_leads'] = [
                'raw_analysis' => 'Real lead generation needs attention. Template leads were intentionally not saved to CRM.',
                'structured_leads' => [],
                'total_count' => 0,
                'is_template' => true
            ];
        }

        $results['leads_generated_count'] = $leads_saved;

        // Generate research summary
        $results['research_summary'] = $this->generate_static_research_summary($business_data, $results);

        $results['chunk_completed'] = 25;
        error_log('✅ Chunk 25 complete — ' . $leads_saved . ' leads saved');
        return $results;
    }

    /**
     * Public real-only lead generation used by final consultation orchestration.
     */
    public function generate_leads($user_id, $business_data, array $options = array()) {
        $business_data = is_array($business_data) ? $business_data : array();
        $research_results = is_array($options['research_results'] ?? null) ? $options['research_results'] : array();
        $structured_leads = $this->generate_ai_tiered_leads($business_data, $research_results);

        if (empty($structured_leads) || !is_array($structured_leads)) {
            return array(
                'status' => 'needs_attention',
                'saved_count' => 0,
                'lead_source' => 'real_ai_generation',
                'message' => 'Real lead generation returned no verified leads. No template contacts were saved.',
                'generated_at' => current_time('mysql'),
            );
        }

        $saved_count = $this->save_leads_to_crm($user_id, array(
            'structured_leads' => $structured_leads,
            'total_count' => count($structured_leads),
            'is_template' => false,
        ), $business_data);

        return array(
            'status' => $saved_count > 0 ? 'ready' : 'needs_attention',
            'saved_count' => $saved_count,
            'lead_source' => 'real_ai_generation',
            'lead_verified' => $saved_count > 0,
            'tiers' => array_values(array_unique(array_filter(array_map(function($lead) {
                return is_array($lead) ? sanitize_key($lead['lead_source'] ?? '') : '';
            }, $structured_leads)))),
            'generated_at' => current_time('mysql'),
        );
    }

    /**
     * ✅ AI-powered tiered lead generation
     * Generates PRIMARY leads (real company/person names with contact hints),
     * SECONDARY leads (organizations, associations, groups that contain ideal clients),
     * and TERTIARY leads (institutions, directories, platforms where ideal clients gather).
     * ALWAYS returns at least 10-15 leads across all tiers.
     */
    private function generate_ai_tiered_leads($business_data, $results = []) {
        $business_name = $business_data['business_name'] ?? 'Your Business';
        $industry = $business_data['industry'] ?? 'general';
        $location = $business_data['location'] ?? 'United States';
        $target_market = $business_data['target_market'] ?? $business_data['target_audience'] ?? '';
        $offerings = $business_data['offerings'] ?? $business_data['services'] ?? '';
        $city = $this->extract_city($location);
        $state = $this->extract_state($location);

        $lead_queries = array_filter(array(
            trim($industry . ' companies ' . $location),
            trim($target_market . ' organizations ' . $location),
            trim($offerings . ' buyers ' . $location),
            trim($industry . ' associations ' . $state),
            trim($industry . ' business directory ' . $location),
        ));
        $live_evidence = $this->build_live_search_evidence($lead_queries, array(
            'limit' => 4,
            'location' => $location,
            'page_scan_limit' => 1,
            'max_results_per_query' => 3,
        ));
        $live_evidence_text = trim((string)($live_evidence['text'] ?? ''));

        // Gather competitor context for lead discovery
        $competitors = $results['competitors_list'] ?? [];
        $competitor_names = [];
        foreach (array_slice(is_array($competitors) ? $competitors : [], 0, 5) as $c) {
            if (is_array($c) && !empty($c['name'])) $competitor_names[] = $c['name'];
            elseif (is_string($c)) $competitor_names[] = $c;
        }
        $competitor_text = !empty($competitor_names) ? implode(', ', $competitor_names) : 'competitors in the space';

        $prompt = "You are a lead generation specialist. Generate actionable leads for this business.

BUSINESS: {$business_name}
INDUSTRY: {$industry}
LOCATION: {$location} (city: {$city}, state: {$state})
TARGET MARKET: {$target_market}
OFFERINGS: {$offerings}
KNOWN COMPETITORS: {$competitor_text}

LIVE SEARCH EVIDENCE TO USE FIRST:
{$live_evidence_text}

Generate leads in THREE TIERS. EVERY tier must have results — you MUST return at least 3-5 leads per tier.

TIER 1 — PRIMARY LEADS (5+ ideal clients/companies):
People or companies who would directly buy these products/services.
Use REAL names of actual companies, organizations, or known entities in {$location} or the broader market.
For each: full name (or company name), title, company, city, state, WHY they are ideal, and a suggested outreach approach.
If privacy prevents naming individuals, name the COMPANY and the job title of the decision-maker to contact.

TIER 2 — SECONDARY LEADS (5+ groups/organizations that contain ideal clients):
Associations, Facebook groups, LinkedIn groups, societies, fraternities/sororities, alumni networks, industry bodies,
Meetup groups, Slack communities, Reddit communities, online forums, professional networks.
For each: organization name, approximate member count if known, why their members are ideal clients, and how to engage.

TIER 3 — TERTIARY LEADS (5+ institutions/platforms/directories):
Universities, schools, coworking spaces, incubators, accelerators, trade shows, conferences,
business directories, platforms (like Thumbtack, Yelp, Angi, Clutch, G2), online marketplaces.
For each: institution/platform name, location, why their users/members match the ideal client profile, how to leverage.

Return ONLY valid JSON (no markdown code blocks) in this exact structure:
{
    \"leads\": [
        {
            \"tier\": \"primary\",
            \"full_name\": \"Company or Person Name\",
            \"company\": \"Company Name\",
            \"job_title\": \"Decision Maker Title\",
            \"city\": \"{$city}\",
            \"state\": \"{$state}\",
            \"contact_type\": \"B2B or B2C\",
            \"primary_category\": \"{$industry}\",
            \"why_ideal\": \"Why this is a perfect lead and outreach suggestion\",
            \"website\": \"https://example.com or empty string\",
            \"source_url\": \"https://example.com/source-page or empty string\",
            \"lead_score\": 85,
            \"priority_level\": \"high\",
            \"lead_temperature\": \"Hot\",
            \"outreach_method\": \"Email/LinkedIn/Phone/In-Person\"
        }
    ]
}

CRITICAL RULES:
- NEVER return 0 leads. Minimum 25 leads total across all 3 tiers.
- Use the LIVE SEARCH EVIDENCE first when choosing names. Then fill any remaining gaps with well-known real companies, organizations, or institutions from your broader knowledge.
- Do not claim a direct contact exists unless the evidence or common public knowledge supports it.
- For {$location}: include local options. Also include national/online options.
- Leave email/phone blank (empty string) — do NOT invent contact details.
- Include the best public website/source URL when it is visible in the evidence.
- lead_score: 70-95 for primary, 60-80 for secondary, 50-70 for tertiary.
- priority_level: 'high' for primary, 'medium' for secondary, 'low' for tertiary.";

        $response = $this->call_openai_analysis($prompt, 4000);
        if (empty($response)) return null;

        $parsed = $this->parse_json_from_response($response);
        if (!$parsed || empty($parsed['leads'])) return null;

        $leads = [];
        foreach ($parsed['leads'] as $lead) {
            if (!is_array($lead)) continue;
            $tier = strtolower($lead['tier'] ?? 'tertiary');
            $tier_label = ($tier === 'primary') ? '🎯 Primary' : (($tier === 'secondary') ? '🏢 Secondary' : '🏛️ Tertiary');
            $leads[] = [
                'full_name' => sanitize_text_field($lead['full_name'] ?? '[' . ucfirst($tier) . ' Lead]'),
                'company' => sanitize_text_field($lead['company'] ?? ''),
                'job_title' => sanitize_text_field($lead['job_title'] ?? ''),
                'email' => '',
                'phone' => '',
                'city' => sanitize_text_field($lead['city'] ?? $city),
                'state' => sanitize_text_field($lead['state'] ?? $state),
                'contact_type' => sanitize_text_field($lead['contact_type'] ?? 'B2B'),
                'primary_category' => sanitize_text_field($lead['primary_category'] ?? $industry),
                'why_ideal' => sanitize_text_field(($tier_label . ' — ') . ($lead['why_ideal'] ?? 'Relevant to your target market.')),
                'lead_score' => min(100, max(0, intval($lead['lead_score'] ?? 65))),
                'priority_level' => sanitize_text_field($lead['priority_level'] ?? 'medium'),
                'lead_temperature' => sanitize_text_field($lead['lead_temperature'] ?? 'Warm'),
                'source' => 'ai_tiered_lead_gen',
                'lead_source' => in_array($tier, array('primary', 'secondary', 'tertiary'), true) ? $tier : 'tertiary',
                'lead_verified' => true,
                'outreach_method' => sanitize_text_field($lead['outreach_method'] ?? 'Email'),
                'website' => esc_url_raw($lead['website'] ?? ($lead['source_url'] ?? '')),
                'source_url' => esc_url_raw($lead['source_url'] ?? ($lead['website'] ?? '')),
            ];
        }

        error_log('✅ AI tiered leads: ' . count($leads) . ' total');
        return $leads;
    }
    
    // ===== COMPETITOR REVIEW INTELLIGENCE (Chunk 26) =====
    
    /**
     * CHUNK 26: Competitor Review Intelligence (1 API call)
     * ✅ Analyzes competitor customer complaints, 1-star review themes,
     *    negative sentiment patterns, and opportunities to solve pain points.
     *    Uses GPT's training knowledge of review patterns — NO external API.
     */
    private function execute_chunk_26_review_intelligence($business_data, $results) {
        error_log('📊 Chunk 26/29: Competitor Review Intelligence — Batch 1 (competitors 1-5)');
        
        $competitors = $results['competitors_list'] ?? [];
        if (is_string($competitors)) {
            $decoded_competitors = json_decode($competitors, true);
            if (is_array($decoded_competitors)) {
                $competitors = $decoded_competitors;
            } else {
                $competitors = array_values(array_filter(array_map('trim', preg_split('/[\n,;]+/', $competitors))));
            }
        }
        if (!is_array($competitors)) {
            $competitors = [];
        }

        $business_name = $business_data['business_name'] ?? 'Your Business';
        $industry = $business_data['industry'] ?? 'general';
        
        if (empty($competitors)) {
            $results['competitor_review_intelligence'] = [
                'analysis' => 'No competitors identified — review intelligence skipped.',
                'timestamp' => current_time('mysql')
            ];
        } else {
            // ✅ BOTTLENECK FIX: Only analyze first 5 competitors (batch 1 of 2)
            // Batch 2 (competitors 6-10) moved to chunk 29 to prevent timeout
            $competitor_names = [];
            foreach (array_slice($competitors, 0, 5) as $c) {
                if (is_array($c) && !empty($c['name'])) {
                    $competitor_names[] = $c['name'];
                } elseif (is_string($c)) {
                    $competitor_names[] = $c;
                }
            }
            $competitor_list = implode(', ', $competitor_names);
            $review_queries = array();
            foreach ($competitor_names as $competitor_name) {
                $review_queries[] = $competitor_name . ' reviews complaints';
            }
            $live_evidence = $this->build_live_search_evidence($review_queries, array(
                'limit' => 3,
                'page_scan_limit' => 1,
                'max_results_per_query' => 2,
            ));
            
            $prompt = "You are a competitive intelligence analyst specializing in customer sentiment and review analysis.

INDUSTRY: {$industry}
BUSINESS: {$business_name}
COMPETITORS TO ANALYZE (Batch 1): {$competitor_list}

Based on the live review evidence below and your knowledge of these companies, their products/services, and common customer experiences in the {$industry} industry, provide a DETAILED COMPETITOR REVIEW INTELLIGENCE REPORT.

LIVE REVIEW SEARCH EVIDENCE:
{$live_evidence['text']}

For EACH competitor, analyze:

## [COMPETITOR NAME] — Customer Complaint Intelligence

### 1-Star Review Themes (Top 5)
- List the most common complaints and negative experiences customers report
- Include specific pain points (e.g., billing issues, poor support, quality problems, hidden fees)
- Rate prevalence: 🔴 Very Common | 🟡 Common | 🟢 Occasional

### Customer Hate Points
- What do customers HATE most about this competitor?
- What makes customers leave/cancel/switch?
- What promises do they break?

### Service Failures
- Common service breakdowns (response times, quality, reliability)
- Support/communication gaps
- Delivery/fulfillment issues

### Negative Sentiment Patterns
- Recurring language in negative reviews
- Emotional triggers (frustration, betrayal, disappointment)
- Unmet expectations vs. marketing claims

Be extremely specific and actionable. Base your analysis on common knowledge about these companies and typical customer experiences in the {$industry} industry.";
            
            $results['competitor_review_intelligence'] = [
                'analysis' => $this->call_openai_analysis($prompt, 4000, 'pro'),
                'competitors_analyzed' => $competitor_names,
                'competitor_count' => count($competitor_names),
                'batch' => '1_of_2',
                'timestamp' => current_time('mysql')
            ];
        }
        
        $results['chunk_completed'] = 26;
        
        error_log('✅ Chunk 26 complete - Review Intelligence Batch 1 done');
        return $results;
    }
    
    /**
     * CHUNK 27: Market Positioning (extracted from old chunk 6 to prevent dual-API-call timeout)
     * ✅ BOTTLENECK FIX: Was second API call in chunk 6, now its own chunk (1 API call)
     */
    private function execute_chunk_27_market_positioning($business_data, $results) {
        error_log('📊 Chunk 27/29: Market Positioning (1 API call)');
        $competitors = $results['competitors_list'] ?? [];
        $results['market_positioning'] = $this->analyze_market_positioning($business_data, $competitors);
        $results['chunk_completed'] = 27;
        error_log('✅ Chunk 27 complete');
        return $results;
    }
    
    /**
     * CHUNK 28: Structured Market Intelligence (extracted from old chunk 24 to prevent dual-API-call timeout)
     * ✅ BOTTLENECK FIX: Was second API call in chunk 24, now its own chunk (1 API call)
     */
    private function execute_chunk_28_structured_market_intel($business_data, $results) {
        error_log('📊 Chunk 28/29: Structured Market Intelligence (1 API call)');
        $competitors_list = $results['competitors_list'] ?? [];
        
        try {
            $structured_intel = $this->analyze_structured_market_intelligence($business_data, $competitors_list);
            $results['structured_market_intel'] = $structured_intel;
        } catch (\Throwable $e) {
            error_log('⚠️ Chunk 28: Structured market intel failed, using fallback: ' . $e->getMessage());
            $results['structured_market_intel'] = [
                'industry_confirmation' => $business_data['industry'] ?? 'Your Industry',
                'competitors_analyzed' => count($competitors_list),
                'market_data' => 'Available in research dashboard',
                'generated_at' => current_time('mysql')
            ];
        }
        
        $results['chunk_completed'] = 28;
        error_log('✅ Chunk 28 complete');
        return $results;
    }
    
    /**
     * CHUNK 29: Review Intelligence Batch 2 — Competitors 6-10 + Strategic Opportunities
     * ✅ BOTTLENECK FIX: Was part of chunk 26 (ALL 10 competitors + strategy in one 6000-token call)
     * Now split: Batch 1 (chunk 26) = competitors 1-5, Batch 2 (chunk 29) = competitors 6-10 + strategy
     */
    private function execute_chunk_29_review_intelligence_batch2($business_data, $results) {
        error_log('📊 Chunk 29/29: Competitor Review Intelligence — Batch 2 (competitors 6-10 + Strategy)');
        
        $competitors = $results['competitors_list'] ?? [];
        if (is_string($competitors)) {
            $decoded_competitors = json_decode($competitors, true);
            if (is_array($decoded_competitors)) {
                $competitors = $decoded_competitors;
            } else {
                $competitors = array_values(array_filter(array_map('trim', preg_split('/[\n,;]+/', $competitors))));
            }
        }
        if (!is_array($competitors)) {
            $competitors = [];
        }
        
        $business_name = $business_data['business_name'] ?? 'Your Business';
        $industry = $business_data['industry'] ?? 'general';
        
        // Get competitors 6-10 (batch 2)
        $batch2_competitors = array_slice($competitors, 5, 5);
        
        if (empty($batch2_competitors)) {
            // No batch 2 competitors — just generate strategic summary from batch 1
            $results['competitor_review_intelligence_batch2'] = [
                'analysis' => 'Fewer than 6 competitors — batch 2 skipped. See batch 1 for full analysis.',
                'batch' => '2_of_2',
                'timestamp' => current_time('mysql')
            ];
        } else {
            $competitor_names = [];
            foreach ($batch2_competitors as $c) {
                if (is_array($c) && !empty($c['name'])) {
                    $competitor_names[] = $c['name'];
                } elseif (is_string($c)) {
                    $competitor_names[] = $c;
                }
            }
            $competitor_list = implode(', ', $competitor_names);
            $review_queries = array();
            foreach ($competitor_names as $competitor_name) {
                $review_queries[] = $competitor_name . ' reviews complaints';
            }
            $live_evidence = $this->build_live_search_evidence($review_queries, array(
                'limit' => 3,
                'page_scan_limit' => 1,
                'max_results_per_query' => 2,
            ));
            
            $prompt = "You are a competitive intelligence analyst specializing in customer sentiment and review analysis.

INDUSTRY: {$industry}
BUSINESS: {$business_name}
COMPETITORS TO ANALYZE (Batch 2): {$competitor_list}

Based on the live review evidence below and your knowledge of these companies, provide a DETAILED COMPETITOR REVIEW INTELLIGENCE REPORT.

LIVE REVIEW SEARCH EVIDENCE:
{$live_evidence['text']}

For EACH competitor, analyze:

## [COMPETITOR NAME] — Customer Complaint Intelligence

### 1-Star Review Themes (Top 5)
- Most common complaints and negative experiences
- Specific pain points (billing issues, poor support, quality problems, hidden fees)
- Prevalence: 🔴 Very Common | 🟡 Common | 🟢 Occasional

### Customer Hate Points
- What customers HATE most about this competitor
- What makes customers leave/cancel/switch

### Service Failures  
- Common service breakdowns
- Support/communication gaps

---

## STRATEGIC OPPORTUNITIES FOR {$business_name}

After analyzing ALL competitors (both batches), provide:

### Pain Points You Can Solve
- Top 10 customer problems across all competitors that {$business_name} can solve
- For each: the problem, which competitors have it, and how to solve it better

### Competitive Positioning from Reviews
- How to position {$business_name} as the answer to competitor weaknesses
- Marketing messages that directly address competitor failures
- Trust-building strategies based on competitor trust gaps

### Review-Based Feature Priorities
- Features/services customers wish competitors offered
- Quality standards competitors fail to meet
- Support/communication improvements that would win customers

Be extremely specific and actionable.";
            
            $results['competitor_review_intelligence_batch2'] = [
                'analysis' => $this->call_openai_analysis($prompt, 4000, 'pro'),
                'competitors_analyzed' => $competitor_names,
                'competitor_count' => count($competitor_names),
                'batch' => '2_of_2',
                'timestamp' => current_time('mysql')
            ];
        }
        
        $results['chunk_completed'] = 29;
        $results['research_complete'] = true;
        $results['research_completed_at'] = current_time('mysql');
        
        error_log('✅ Chunk 29 complete - PHASE 3 RESEARCH FULLY COMPLETED!');
        return $results;
    }
    
    /**
     * ✅ ENHANCED: Static research summary that uses structured market intel
     * Compiles existing research data into a summary with actual market values
     */
    private function generate_static_research_summary($business_data, $results) {
        $business_name = $business_data['business_name'] ?? 'Your Business';
        $industry = $business_data['industry'] ?? 'your industry';
        $location = $business_data['location'] ?? 'your region';
        $leads_count = $results['leads_generated_count'] ?? 0;
        
        // Extract competitor names if available
        $competitors = $results['competitors_list'] ?? [];
        if (is_string($competitors)) {
            $decoded_competitors = json_decode($competitors, true);
            $competitors = is_array($decoded_competitors)
                ? $decoded_competitors
                : array_values(array_filter(array_map('trim', preg_split('/[\n,;]+/', $competitors))));
        }
        if (!is_array($competitors)) {
            $competitors = [];
        }
        $competitor_count = count($competitors);
        $competitor_names = [];
        foreach (array_slice($competitors, 0, 5) as $competitor) {
            if (is_array($competitor) && !empty($competitor['name'])) {
                $competitor_names[] = $competitor['name'];
            } elseif (is_string($competitor)) {
                $competitor_names[] = $competitor;
            }
        }
        $competitor_list = !empty($competitor_names) ? implode(', ', $competitor_names) : 'key industry players';
        
        // ✅ ENHANCED: Extract structured market intel with actual values
        $market_intel = $results['structured_market_intel'] ?? [];
        $global_market_size = $market_intel['global_market_size'] ?? '';
        $global_market_year = $market_intel['global_market_year'] ?? '';
        $local_market_size = $market_intel['local_market_size'] ?? '';
        $market_growth = $market_intel['market_growth_rate'] ?? '';
        $market_projection = $market_intel['market_projection'] ?? '';
        $industry_confirmed = $market_intel['industry_confirmation'] ?? $industry;
        
        // Build market size section with actual values
        $market_section = "";
        if (!empty($global_market_size) && $global_market_size !== 'Data not available') {
            $market_section .= "- **Global Market Value:** {$global_market_size}";
            if (!empty($global_market_year)) $market_section .= " ({$global_market_year})";
            $market_section .= "\n";
        }
        if (!empty($local_market_size) && $local_market_size !== 'Data not available') {
            $market_section .= "- **{$location} Market Value:** {$local_market_size}\n";
        }
        if (!empty($market_growth)) {
            $market_section .= "- **Annual Growth Rate:** {$market_growth}\n";
        }
        if (!empty($market_projection)) {
            $market_section .= "- **Future Projection:** {$market_projection}\n";
        }
        
        // Fallback if no market data
        if (empty($market_section)) {
            $market_section = "- Market analysis completed - see detailed research dashboard\n";
        }
        
        // ✅ Extract top earners/competitors with financials
        $top_competitors = $market_intel['top_competitors'] ?? [];
        $top_earners_section = "";
        if (!empty($top_competitors) && is_array($top_competitors)) {
            foreach (array_slice($top_competitors, 0, 3) as $idx => $comp) {
                $name = is_array($comp) ? ($comp['name'] ?? 'Competitor') : $comp;
                $revenue = is_array($comp) ? ($comp['annual_revenue'] ?? '') : '';
                $position = is_array($comp) ? ($comp['market_position'] ?? '') : '';
                $top_earners_section .= "- **{$name}**";
                if (!empty($revenue)) $top_earners_section .= " - {$revenue}";
                if (!empty($position)) $top_earners_section .= " ({$position})";
                $top_earners_section .= "\n";
            }
        }
        
        // ✅ Extract target market intel
        $ideal_target = $market_intel['ideal_target_market'] ?? [];
        $target_section = "";
        if (!empty($ideal_target) && is_array($ideal_target)) {
            if (!empty($ideal_target['primary'])) {
                $target_section .= "- **Primary Target:** {$ideal_target['primary']}\n";
            }
            if (!empty($ideal_target['secondary'])) {
                $target_section .= "- **Secondary Target:** {$ideal_target['secondary']}\n";
            }
            if (!empty($ideal_target['bulk_buyers'])) {
                $target_section .= "- **B2B/Bulk Opportunities:** {$ideal_target['bulk_buyers']}\n";
            }
        }
        
        return "## Research Summary for {$business_name}

### Market Overview
Your business operates in **{$industry_confirmed}**, serving the {$location} market.

### 💰 Industry & Market Value
{$market_section}

### 🏆 Top Industry Players
" . (!empty($top_earners_section) ? $top_earners_section : "We've analyzed {$competitor_count} competitors: {$competitor_list}\n") . "

### 🎯 Target Market Intelligence
" . (!empty($target_section) ? $target_section : "- Target market intelligence has been compiled and is available in your dashboard\n") . "

### 📊 Competitive Analysis Complete
We've analyzed **{$competitor_count} competitors** including: {$competitor_list}. Your research dashboard contains detailed breakdowns of their:
- Website strategies and design patterns
- SEO rankings and keyword strategies  
- Pricing and positioning
- Strengths and weaknesses

### 📋 CRM & Leads
**{$leads_count} qualified leads** have been generated and saved to your CRM dashboard. These are companies and contacts that match your ideal customer profile.

*Research completed: " . current_time('mysql') . "*";
    }
    
    /**
     * ✅ NEW: Generate COMPREHENSIVE research summary with real market data
     * Unlike the basic summary, this includes actual numbers, competitor names, and insights
     */
    private function generate_comprehensive_research_summary($business_data, $results) {
        $business_name = $business_data['business_name'] ?? 'Your Business';
        $industry = $business_data['industry'] ?? 'your industry';
        $location = $business_data['location'] ?? 'your region';
        
        // Extract competitor names from research results
        $competitors = $results['competitors_list'] ?? [];
        $competitor_names = [];
        foreach ($competitors as $competitor) {
            if (is_array($competitor) && !empty($competitor['name'])) {
                $competitor_names[] = $competitor['name'];
            } elseif (is_string($competitor)) {
                $competitor_names[] = $competitor;
            }
        }
        $competitor_list = !empty($competitor_names) ? implode(', ', array_slice($competitor_names, 0, 5)) : 'key competitors identified';
        
        // Extract market insights from various research chunks
        $market_trends = $results['market_trends']['analysis'] ?? '';
        $target_market = $results['target_market_intelligence']['analysis'] ?? '';
        $industry_insights = $results['industry_insights']['analysis'] ?? '';
        $opportunities = $results['market_gaps']['analysis'] ?? ($results['opportunities_threats']['analysis'] ?? '');
        $leads_count = $results['leads_generated_count'] ?? 0;
        
        $prompt = "Create a comprehensive executive summary of this deep market research for {$business_name} in {$industry}.

RESEARCH DATA COMPILED:

COMPETITORS ANALYZED: {$competitor_list}

MARKET & INDUSTRY INSIGHTS:
{$market_trends}

{$industry_insights}

TARGET MARKET INTELLIGENCE:
{$target_market}

STRATEGIC OPPORTUNITIES:
{$opportunities}

LEADS GENERATED: {$leads_count} initial leads have been identified and saved

LOCATION FOCUS: {$location}

TASK: Create an executive summary (300-400 words) that includes:

1. **MARKET OVERVIEW** (MUST include specific data):
   - Global/regional market size estimate (use real industry figures or reasonable estimates)
   - Market growth rate/trend
   - Key market drivers

2. **COMPETITIVE LANDSCAPE** (MUST name actual competitors):
   - List the top 3-5 competitors by name
   - What they do well vs. where they fall short
   - Your client's unique positioning opportunity

3. **TOP INDUSTRY PLAYERS** (for aspirational context):
   - Name 2-3 top earners/leaders in this industry globally
   - What makes them successful

4. **STRATEGIC OPPORTUNITIES**:
   - 3-4 specific, actionable opportunities based on gaps found
   - How the client can differentiate

5. **INITIAL LEADS**:
   - Mention that {$leads_count} initial target leads have been generated
   - These are available in the CRM dashboard

Use specific numbers, names, and actionable insights. Avoid generic statements.
Format with clear section headers and bullet points for readability.";

        return $this->call_openai_analysis($prompt, 2000);
    }
    
    // =========================================================================
    // LEGACY CHUNK METHODS (DEPRECATED - kept for reference)
    // =========================================================================
    
    /**
     * @deprecated Old chunk 1 - now split into chunks 1-4
     */
    private function execute_chunk_1_foundation($business_data, $results) {
        error_log('📊 Chunk 1: Core Foundation Analysis');
        
        $results['vision_analysis'] = $this->safe_analysis('vision_and_intent', $business_data);
        $results['structure_analysis'] = $this->safe_analysis('business_structure', $business_data);
        $results['target_market_intelligence'] = $this->safe_analysis('target_market_intelligence', $business_data);
        $results['target_audience_analysis'] = $this->safe_analysis('target_audience', $business_data);
        $results['chunk_completed'] = 1;
        
        error_log('✅ Chunk 1 complete');
        return $results;
    }
    
    /**
     * @deprecated Old chunk 2 - now split into chunks 5-6
     */
    private function execute_chunk_2_competitor_discovery($business_data, $results) {
        error_log('📊 Chunk 2: Competitor Discovery');
        
        // Discover competitors
        $competitors = $this->discover_competitors($business_data);
        $results['competitors_list'] = $competitors;
        
        // Industry landscape
        $results['industry_insights'] = $this->safe_analysis('industry_insights', $business_data);
        
        // Market positioning overview
        $results['market_positioning'] = $this->analyze_market_positioning($business_data, $competitors);
        
        $results['chunk_completed'] = 2;
        error_log('✅ Chunk 2 complete - Found ' . count($competitors) . ' competitors');
        return $results;
    }
    
    /**
     * CHUNK 3: Competitor Business Model Analysis
     * Deep dive into each competitor's business strategy
     */
    private function execute_chunk_3_competitor_business($business_data, $results) {
        error_log('📊 Chunk 3: Competitor Business Analysis');
        
        $competitors = $results['competitors_list'] ?? [];
        
        if (empty($competitors)) {
            error_log('⚠️ No competitors found, using generic analysis');
            $results['competitor_business_analysis'] = $this->safe_analysis('competitors', $business_data);
        } else {
            // Batch analyze in groups of 4
            $batch1 = array_slice($competitors, 0, 4);
            $batch2 = array_slice($competitors, 4, 4);
            $batch3 = array_slice($competitors, 8, 4);
            
            $results['competitor_business_batch_1'] = $this->analyze_competitors_business_batch($business_data, $batch1, '1-4');
            $results['competitor_business_batch_2'] = $this->analyze_competitors_business_batch($business_data, $batch2, '5-8');
            $results['competitor_business_batch_3'] = $this->analyze_competitors_business_batch($business_data, $batch3, '9-12');
        }
        
        $results['chunk_completed'] = 3;
        error_log('✅ Chunk 3 complete');
        return $results;
    }
    
    /**
     * CHUNK 4: Competitor Website Analysis
     * Analyze website structure, UX, and conversion strategies
     */
    private function execute_chunk_4_competitor_website($business_data, $results) {
        error_log('📊 Chunk 4: Competitor Website Analysis');
        
        $competitors = $results['competitors_list'] ?? [];
        
        if (empty($competitors)) {
            $results['competitor_website_analysis'] = $this->analyze_website_best_practices($business_data);
        } else {
            $batch1 = array_slice($competitors, 0, 4);
            $batch2 = array_slice($competitors, 4, 4);
            $batch3 = array_slice($competitors, 8, 4);
            
            $results['competitor_website_batch_1'] = $this->analyze_competitors_website_batch($business_data, $batch1, '1-4');
            $results['competitor_website_batch_2'] = $this->analyze_competitors_website_batch($business_data, $batch2, '5-8');
            $results['competitor_website_batch_3'] = $this->analyze_competitors_website_batch($business_data, $batch3, '9-12');
        }
        
        $results['chunk_completed'] = 4;
        error_log('✅ Chunk 4 complete');
        return $results;
    }
    
    /**
     * CHUNK 5: Competitor SEO Batch 1 (SPLIT - 1 API call only)
     */
    private function execute_chunk_5_seo_batch1($business_data, $results) {
        error_log('📊 Chunk 5: Competitor SEO Batch 1');
        
        $competitors = $results['competitors_list'] ?? [];
        
        if (empty($competitors)) {
            // If no competitors, do keyword research instead
            $results['competitor_seo_batch_1'] = $this->safe_analysis('keyword_research', $business_data);
        } else {
            $batch1 = array_slice($competitors, 0, 4);
            $results['competitor_seo_batch_1'] = $this->analyze_competitors_seo_batch($business_data, $batch1, '1-4');
        }
        
        $results['chunk_completed'] = 5;
        error_log('✅ Chunk 5 complete - SEO Batch 1');
        return $results;
    }
    
    /**
     * CHUNK 6: Competitor SEO Batch 2 (SPLIT - 1 API call only)
     */
    private function execute_chunk_6_seo_batch2($business_data, $results) {
        error_log('📊 Chunk 6: Competitor SEO Batch 2');
        
        $competitors = $results['competitors_list'] ?? [];
        
        if (count($competitors) > 4) {
            $batch2 = array_slice($competitors, 4, 4);
            $results['competitor_seo_batch_2'] = $this->analyze_competitors_seo_batch($business_data, $batch2, '5-8');
        } else {
            $results['competitor_seo_batch_2'] = ['analysis' => 'Batch 2 not needed - fewer than 5 competitors', 'timestamp' => current_time('mysql')];
        }
        
        $results['chunk_completed'] = 6;
        error_log('✅ Chunk 6 complete - SEO Batch 2');
        return $results;
    }
    
    /**
     * CHUNK 7: Competitor SEO Batch 3 (SPLIT - 1 API call only)
     */
    private function execute_chunk_7_seo_batch3($business_data, $results) {
        error_log('📊 Chunk 7: Competitor SEO Batch 3');
        
        $competitors = $results['competitors_list'] ?? [];
        
        if (count($competitors) > 8) {
            $batch3 = array_slice($competitors, 8, 4);
            $results['competitor_seo_batch_3'] = $this->analyze_competitors_seo_batch($business_data, $batch3, '9-12');
        } else {
            $results['competitor_seo_batch_3'] = ['analysis' => 'Batch 3 not needed - fewer than 9 competitors', 'timestamp' => current_time('mysql')];
        }
        
        $results['chunk_completed'] = 7;
        error_log('✅ Chunk 7 complete - SEO Batch 3');
        return $results;
    }
    
    /**
     * CHUNK 8: Market Trends & Pricing (SPLIT - 2 API calls)
     */
    private function execute_chunk_8_market_part1($business_data, $results) {
        error_log('📊 Chunk 8: Market Trends & Pricing');
        
        $results['market_trends'] = $this->safe_analysis('market_trends', $business_data);
        $results['pricing_analysis'] = $this->safe_analysis('pricing_strategy', $business_data);
        
        $results['chunk_completed'] = 8;
        error_log('✅ Chunk 8 complete - Market Part 1');
        return $results;
    }
    
    /**
     * CHUNK 9: Marketing & Operations (SPLIT - 2 API calls)
     */
    private function execute_chunk_9_market_part2($business_data, $results) {
        error_log('📊 Chunk 9: Marketing & Operations');
        
        $results['marketing_strategy'] = $this->safe_analysis('marketing_strategy', $business_data);
        $results['operations_analysis'] = $this->safe_analysis('operations_and_delivery', $business_data);
        
        $results['chunk_completed'] = 9;
        error_log('✅ Chunk 9 complete - Market Part 2');
        return $results;
    }
    
    /**
     * CHUNK 10: Tech Stack & Legal (SPLIT - 2 API calls)
     */
    private function execute_chunk_10_technical_part1($business_data, $results) {
        error_log('📊 Chunk 10: Tech Stack & Legal');
        
        $results['technology_stack'] = $this->safe_analysis('technology_stack', $business_data);
        $results['legal_compliance'] = $this->safe_analysis('legal_compliance', $business_data);
        
        $results['chunk_completed'] = 10;
        error_log('✅ Chunk 10 complete - Technical Part 1');
        return $results;
    }
    
    /**
     * CHUNK 11: Keyword Research & Brand (SPLIT - 2 API calls)
     */
    private function execute_chunk_11_technical_part2($business_data, $results) {
        error_log('📊 Chunk 11: Keyword Research & Brand Positioning');
        
        $results['keyword_research'] = $this->safe_analysis('keyword_research', $business_data);
        $results['brand_positioning'] = $this->safe_analysis('brand_positioning', $business_data);
        
        $results['chunk_completed'] = 11;
        error_log('✅ Chunk 11 complete - Technical Part 2');
        return $results;
    }
    
    /**
     * CHUNK 12: Comparative Matrix & Gap Analysis (2 API calls)
     */
    private function execute_chunk_12_matrix_gaps($business_data, $results) {
        error_log('📊 Chunk 12: Comparative Matrix & Gap Analysis');
        
        // Generate comparative matrix (1 API call)
        $results['comparative_matrix'] = $this->generate_comparative_matrix($business_data, $results);
        
        // Identify market gaps (1 API call)
        $results['market_gaps'] = $this->synthesize_market_gaps($business_data, $results);
        
        $results['chunk_completed'] = 12;
        error_log('✅ Chunk 12 complete - Matrix & Gaps');
        return $results;
    }
    
    /**
     * CHUNK 13: Strategic Advantages & Success Metrics (2 API calls)
     */
    private function execute_chunk_13_strategic($business_data, $results) {
        error_log('📊 Chunk 13: Strategic Advantages & Metrics');
        
        // Map strategic advantages (1 API call)
        $results['strategic_advantages'] = $this->map_strategic_advantages($business_data, $results);
        
        // Success metrics (1 API call)
        $results['success_metrics'] = $this->safe_analysis('success_metrics', $business_data);
        
        $results['chunk_completed'] = 13;
        error_log('✅ Chunk 13 complete - Strategic');
        return $results;
    }
    
    /**
     * CHUNK 14: Opportunities & Threats (1 API call)
     */
    private function execute_chunk_14_opportunities($business_data, $results) {
        error_log('📊 Chunk 14: Opportunities & Threats Analysis');
        
        $results['opportunities_threats'] = $this->safe_analysis('opportunities_threats', $business_data);
        
        $results['chunk_completed'] = 14;
        error_log('✅ Chunk 14 complete - Opportunities');
        return $results;
    }
    
    /**
     * CHUNK 15: Final Research Summary (1 API call)
     */
    private function execute_chunk_15_final_summary($business_data, $results) {
        error_log('📊 Chunk 15: Generating Final Research Summary');
        
        // Generate comprehensive research summary
        $results['research_summary'] = $this->generate_research_summary($business_data, $results);
        
        $results['chunk_completed'] = 15;
        $results['research_complete'] = true;
        error_log('✅ Chunk 15 complete - Final Summary DONE!');
        return $results;
    }
    
    /**
     * LEGACY: Old chunk 5 - keeping for reference, now split into chunks 5, 6, 7
     * @deprecated Use execute_chunk_5_seo_batch1, execute_chunk_6_seo_batch2, execute_chunk_7_seo_batch3
     */
    private function execute_chunk_5_competitor_seo_LEGACY($business_data, $results) {
        // This function is deprecated - kept for reference only
        return $results;
    }
    
    /**
     * DEPRECATED: Old chunk 10 - now handled by chunks 14-15
     * @deprecated Use execute_chunk_14_opportunities and execute_chunk_15_final_summary
     */
    private function execute_chunk_10_summary_DEPRECATED($business_data, $results) {
        // This function is deprecated - kept for reference only
        return $results;
    }
    
    /**
     * DEPRECATED: Original execute_chunk_8_synthesis - now split into chunks 12, 13, 14, 15
     * @deprecated
     */
    private function execute_chunk_8_synthesis_DEPRECATED($business_data, $results) {
        // This function is deprecated - kept for reference only
        return $results;
    }
    
    // =========================================================================
    // COMPETITOR DISCOVERY & ANALYSIS METHODS
    // =========================================================================
    
    /**
     * Detect the primary business category for smarter analysis
     */
    /**
     * ✅ ENHANCED: Detect ALL business categories for multi-service businesses
     * Returns array with primary_category and secondary_categories
     */
    private function detect_business_categories($business_data) {
        $search_text = strtolower(implode(' ', [
            $business_data['industry'] ?? '',
            $business_data['business_type'] ?? '',
            $business_data['offerings'] ?? '',
            $business_data['services'] ?? '',
            $business_data['business_name'] ?? '',
            $business_data['product_description'] ?? ''
        ]));
        
        $detected_categories = [];
        
        // Website/App Builder indicators (HIGH PRIORITY - if present, likely the core offering)
        $website_builder_indicators = [
            'website builder', 'web builder', 'page builder', 'site builder', 
            'creates website', 'build website', 'generates website', 'ai website',
            'no-code website', 'drag and drop', 'wordpress theme', 'landing page builder',
            'app builder', 'app development', 'web development'
        ];
        foreach ($website_builder_indicators as $indicator) {
            if (strpos($search_text, $indicator) !== false) {
                $detected_categories['website_builder'] = true;
                break;
            }
        }
        
        // Software/SaaS/Plugin indicators
        $software_indicators = [
            'software', 'saas', 'platform', 'plugin', 'extension', 'api', 
            'cloud', 'digital product', 'wordpress plugin', 'online tool'
        ];
        foreach ($software_indicators as $indicator) {
            if (strpos($search_text, $indicator) !== false) {
                $detected_categories['software_platform'] = true;
                break;
            }
        }
        
        // CRM indicators
        $crm_indicators = ['crm', 'customer relationship', 'lead management', 'sales pipeline', 'contact management'];
        foreach ($crm_indicators as $indicator) {
            if (strpos($search_text, $indicator) !== false) {
                $detected_categories['crm'] = true;
                break;
            }
        }
        
        // Email marketing indicators
        $email_indicators = ['email marketing', 'email campaign', 'newsletter', 'bulk email', 'email automation', 'drip campaign'];
        foreach ($email_indicators as $indicator) {
            if (strpos($search_text, $indicator) !== false) {
                $detected_categories['email_marketing'] = true;
                break;
            }
        }
        
        // SEO indicators
        $seo_indicators = ['seo', 'search engine', 'keyword', 'ranking', 'organic traffic', 'search optimization'];
        foreach ($seo_indicators as $indicator) {
            if (strpos($search_text, $indicator) !== false) {
                $detected_categories['seo'] = true;
                break;
            }
        }
        
        // Phone/Communication indicators
        $phone_indicators = ['phone', 'voip', 'call', 'sms', 'twilio', 'receptionist', 'business phone', 'virtual phone'];
        foreach ($phone_indicators as $indicator) {
            if (strpos($search_text, $indicator) !== false) {
                $detected_categories['phone_communication'] = true;
                break;
            }
        }
        
        // Payment/E-commerce indicators
        $payment_indicators = ['payment', 'checkout', 'stripe', 'invoicing', 'billing', 'e-commerce', 'ecommerce', 'online store'];
        foreach ($payment_indicators as $indicator) {
            if (strpos($search_text, $indicator) !== false) {
                $detected_categories['payments_ecommerce'] = true;
                break;
            }
        }
        
        // Business automation indicators
        $automation_indicators = ['automation', 'workflow', 'automate', 'task automation', 'business process'];
        foreach ($automation_indicators as $indicator) {
            if (strpos($search_text, $indicator) !== false) {
                $detected_categories['automation'] = true;
                break;
            }
        }
        
        // Analytics indicators
        $analytics_indicators = ['analytics', 'dashboard', 'reporting', 'insights', 'metrics', 'tracking'];
        foreach ($analytics_indicators as $indicator) {
            if (strpos($search_text, $indicator) !== false) {
                $detected_categories['analytics'] = true;
                break;
            }
        }
        
        // Restaurant/Food indicators
        $food_indicators = ['restaurant', 'cafe', 'food', 'catering', 'bakery', 'bar', 'grill', 'kitchen', 'dining'];
        foreach ($food_indicators as $indicator) {
            if (strpos($search_text, $indicator) !== false) {
                $detected_categories['food_service'] = true;
                break;
            }
        }
        
        // E-commerce/Retail indicators  
        $retail_indicators = ['store', 'shop', 'retail', 'products', 'merchandise', 'clothing', 'fashion'];
        foreach ($retail_indicators as $indicator) {
            if (strpos($search_text, $indicator) !== false) {
                $detected_categories['retail'] = true;
                break;
            }
        }
        
        // Professional services indicators
        $professional_indicators = ['consulting', 'law', 'legal', 'accounting', 'financial', 'advisory', 'coaching', 'agency'];
        foreach ($professional_indicators as $indicator) {
            if (strpos($search_text, $indicator) !== false) {
                $detected_categories['professional_services'] = true;
                break;
            }
        }
        
        // Determine PRIMARY category (priority order - what's the CORE offering)
        $primary_priority = [
            'website_builder',      // If it builds websites, that's foundational
            'software_platform',    // Software/SaaS platform
            'food_service',         // Restaurant/food
            'retail',               // Retail store
            'professional_services', // Consulting, legal, etc.
            'crm',                  // CRM as primary
            'email_marketing',      // Email marketing company
            'seo',                  // SEO agency
            'payments_ecommerce',   // Payment processor
            'phone_communication',  // VoIP provider
            'automation',           // Automation platform
            'analytics'             // Analytics company
        ];
        
        $primary_category = 'general';
        $secondary_categories = [];
        
        foreach ($primary_priority as $cat) {
            if (!empty($detected_categories[$cat])) {
                if ($primary_category === 'general') {
                    $primary_category = $cat;
                } else {
                    $secondary_categories[] = $cat;
                }
            }
        }
        
        error_log("🔍 Multi-category detection: PRIMARY={$primary_category}, SECONDARY=" . implode(',', $secondary_categories));
        
        return [
            'primary' => $primary_category,
            'secondary' => $secondary_categories,
            'all_detected' => array_keys($detected_categories)
        ];
    }
    
    /**
     * Legacy single-category detection (for backward compatibility)
     */
    private function detect_business_category($business_data) {
        $categories = $this->detect_business_categories($business_data);
        return $categories['primary'];
    }
    
    /**
     * Check if business is a software/SaaS product
     */
    private function is_software_product($business_data) {
        $categories = $this->detect_business_categories($business_data);
        return in_array($categories['primary'], ['website_builder', 'software_platform']) || 
               in_array('software_platform', $categories['secondary']);
    }
    
    /**
     * ✅ ENHANCED: Discover PRIMARY and SECONDARY competitors
     * Now properly handles multi-service businesses that compete in multiple categories
     */
    private function discover_competitors($business_data) {
        $industry = $business_data['industry'] ?? 'general';
        $business_type = $business_data['business_type'] ?? 'service';
        $location = $business_data['location'] ?? '';
        $offerings = $business_data['offerings'] ?? $business_data['services'] ?? '';
        $business_name = $business_data['business_name'] ?? '';
        
        // ✅ NEW: Detect ALL business categories (primary + secondary)
        $categories = $this->detect_business_categories($business_data);
        $primary_category = $categories['primary'];
        $secondary_categories = $categories['secondary'];
        
        error_log("🔍 Multi-Category Competitor Discovery:");
        error_log("   PRIMARY: {$primary_category}");
        error_log("   SECONDARY: " . implode(', ', $secondary_categories));
        
        // Build enhanced prompt that finds PRIMARY and SECONDARY competitors
        $prompt = $this->build_multi_category_competitor_prompt($business_data, $primary_category, $secondary_categories);

        $live_evidence = $this->build_live_search_evidence(array(
            trim($industry . ' competitors ' . $location),
            trim($offerings . ' competitors ' . $location),
            trim($industry . ' companies ' . $location),
        ), array(
            'location' => $location,
            'limit' => 5,
            'page_scan_limit' => 1,
            'max_results_per_query' => 3,
        ));

        if (!empty($live_evidence['text'])) {
            $prompt .= "\n\nLIVE SEARCH EVIDENCE (CURRENT MARKET DATA - USE THIS AS YOUR PRIMARY EVIDENCE BASE):\n";
            $prompt .= $live_evidence['text'];
            $prompt .= "\n\nOnly include competitors that are supported by the live search evidence above unless they are obvious national incumbents in this category.";
        }
        
        $response = $this->call_openai_analysis($prompt, 6000);
        
        // Parse competitors from response (now includes primary/secondary classification)
        $competitors = $this->parse_competitors_list($response);
        
        return $competitors;
    }
    
    /**
     * ✅ NEW: Build competitor prompt for multi-category businesses
     * Finds PRIMARY competitors (core offering) and SECONDARY competitors (additional features)
     */
    private function build_multi_category_competitor_prompt($business_data, $primary_category, $secondary_categories) {
        $industry = $business_data['industry'] ?? 'general';
        $business_type = $business_data['business_type'] ?? 'service';
        $location = $business_data['location'] ?? '';
        $offerings = $business_data['offerings'] ?? $business_data['services'] ?? '';
        $business_name = $business_data['business_name'] ?? '';
        
        // Get human-readable category names
        $primary_label = $this->get_category_label($primary_category);
        $secondary_labels = array_map([$this, 'get_category_label'], $secondary_categories);
        $secondary_list = !empty($secondary_labels) ? implode(', ', $secondary_labels) : 'None detected';
        
        // Build specific guidance for each detected category
        $primary_guidance = $this->get_category_competitor_examples($primary_category);
        $secondary_guidance = '';
        foreach ($secondary_categories as $sec_cat) {
            $secondary_guidance .= $this->get_category_competitor_examples($sec_cat) . "\n";
        }
        
        return "As a competitive intelligence expert, analyze this MULTI-SERVICE BUSINESS and identify BOTH primary and secondary competitors.

═══════════════════════════════════════════════════════════════
BUSINESS BEING ANALYZED:
═══════════════════════════════════════════════════════════════
- Business Name: {$business_name}
- Industry: {$industry}
- Business Type: {$business_type}
- Location: {$location}
- What They Offer: {$offerings}

═══════════════════════════════════════════════════════════════
DETECTED COMPETITIVE CATEGORIES:
═══════════════════════════════════════════════════════════════
🎯 PRIMARY CATEGORY: {$primary_label}
   (This is the CORE offering - the foundation of the business)

📦 SECONDARY CATEGORIES: {$secondary_list}
   (Additional features/services that also have their own competitors)

═══════════════════════════════════════════════════════════════
IMPORTANT: MULTI-CATEGORY COMPETITOR ANALYSIS
═══════════════════════════════════════════════════════════════

This business offers MULTIPLE services, so it competes with DIFFERENT companies in DIFFERENT areas:

🔵 PRIMARY COMPETITORS (6-8 companies):
   Businesses that compete with the CORE offering ({$primary_label}).
   These are the companies a customer would DIRECTLY compare when choosing the main product/service.
   
   {$primary_guidance}

🟣 SECONDARY COMPETITORS (4-6 companies):
   Businesses that compete with the ADDITIONAL features/services.
   A customer might use these alternatives for specific features even if using another primary solution.
   
   {$secondary_guidance}

═══════════════════════════════════════════════════════════════
OUTPUT FORMAT - LIST ALL COMPETITORS:
═══════════════════════════════════════════════════════════════

## PRIMARY COMPETITORS (Core {$primary_label} Competition)

**PRIMARY 1: [Company Name]**
- Website: [URL]
- Competitor Type: PRIMARY
- What They Offer: [How they compete with the core offering]
- Target Customer: [Who they serve]
- Why Primary Competitor: [Why this is a direct core competitor]

(Repeat for 6-8 PRIMARY competitors)

## SECONDARY COMPETITORS (Feature-Level Competition)

**SECONDARY 1: [Company Name]**
- Website: [URL]  
- Competitor Type: SECONDARY
- Competes With Feature: [Which secondary category - CRM, Email, SEO, etc.]
- What They Offer: [Their specific offering in that category]
- Why Secondary Competitor: [Why a customer might choose them for this specific feature]

(Repeat for 4-6 SECONDARY competitors)

═══════════════════════════════════════════════════════════════
RULES:
═══════════════════════════════════════════════════════════════
- Use REAL, VERIFIABLE company names only
- PRIMARY competitors MUST compete with the CORE offering (e.g., if core is website building, include Wix, Squarespace, Webflow, NOT HubSpot)
- SECONDARY competitors compete with additional features (e.g., if CRM is secondary, include HubSpot, Salesforce)
- Do NOT put CRM competitors as PRIMARY if the core offering is website building
- Include a MIX of large players and smaller challengers in each category
- Companies like Zoho One or monday.com that offer multiple features can be listed as secondary if they don't match the primary";
    }
    
    /**
     * Get category-specific guidance for competitor discovery (legacy - kept for compatibility)
     */
    private function get_category_guidance($category, $industry) {
        switch ($category) {
            case 'software':
                return "
📌 SOFTWARE/SAAS SPECIFIC GUIDANCE:
- Find OTHER SOFTWARE PRODUCTS in the same category
- If it's a website builder → compare to Wix, Squarespace, Webflow, Elementor, etc.
- If it's a CRM → compare to Salesforce, HubSpot, Pipedrive, etc.
- If it's project management → compare to Asana, Monday, Trello, etc.
- DO NOT include consulting firms, IT services companies, or development agencies
- These should be products a user would evaluate on G2, Capterra, or Product Hunt";

            case 'ecommerce':
                return "
📌 E-COMMERCE/RETAIL SPECIFIC GUIDANCE:
- Find other online stores or retailers selling similar products
- Consider both online and brick-and-mortar competitors
- Include marketplace competitors (Amazon, eBay sellers) if relevant
- Compare by product category, price point, and target demographic";

            case 'food_service':
                return "
📌 RESTAURANT/FOOD SERVICE SPECIFIC GUIDANCE:
- Find other restaurants/food businesses in the same cuisine and price range
- Consider both local competitors and chains
- Compare by food type, dining style, and target customer
- If location-based, prioritize local/regional competitors";

            case 'professional_services':
                return "
📌 PROFESSIONAL SERVICES SPECIFIC GUIDANCE:
- Find other firms offering the same professional services
- Compare by specialization, target client size, and geographic focus
- Include both local firms and national/international competitors
- Consider boutique vs. large firm competitors";

            default:
                return "
📌 GENERAL GUIDANCE:
- Focus on businesses that offer THE SAME core product or service
- Compare by target customer, price point, and geographic reach
- A good test: Would a customer visit both websites before deciding?
- Include a mix of sizes: large players, similar-sized competitors, and emerging alternatives";
        }
    }
    
    /**
     * ✅ NEW: Get human-readable label for category
     */
    private function get_category_label($category) {
        $labels = [
            'website_builder' => 'Website/App Builder',
            'software_platform' => 'Software/SaaS Platform',
            'crm' => 'CRM & Customer Management',
            'email_marketing' => 'Email Marketing',
            'seo' => 'SEO & Search Optimization',
            'phone_communication' => 'Business Phone/VoIP',
            'payments_ecommerce' => 'Payments & E-commerce',
            'automation' => 'Business Automation',
            'analytics' => 'Analytics & Reporting',
            'food_service' => 'Restaurant/Food Service',
            'retail' => 'Retail/E-commerce Store',
            'professional_services' => 'Professional Services',
            'general' => 'General Business'
        ];
        return $labels[$category] ?? ucfirst(str_replace('_', ' ', (string) $category));
    }
    
    /**
     * ✅ NEW: Get specific competitor examples for each category
     * This helps the AI understand exactly what kind of competitors to find
     */
    private function get_category_competitor_examples($category) {
        switch ($category) {
            case 'website_builder':
                return "WEBSITE BUILDER competitors include:
   - Wix, Squarespace, Webflow, WordPress.com, Elementor
   - Bolt.new, Framer, Duda, Carrd, Weebly
   - GoDaddy Website Builder, Hostinger Website Builder
   - AI builders: 10Web, Durable, Mixo, Zyro
   These are tools that CREATE/GENERATE websites.";
                
            case 'software_platform':
                return "SOFTWARE PLATFORM competitors - depends on the specific software type.
   Look for other SaaS products in the same category.";
                
            case 'crm':
                return "CRM competitors include:
   - Salesforce, HubSpot CRM, Pipedrive, Zoho CRM
   - Freshsales, monday CRM, Copper, Insightly
   - Close, Keap (Infusionsoft), Less Annoying CRM";
                
            case 'email_marketing':
                return "EMAIL MARKETING competitors include:
   - Mailchimp, Brevo (Sendinblue), MailerLite, ConvertKit
   - Constant Contact, AWeber, GetResponse, ActiveCampaign
   - Klaviyo, Drip, Campaign Monitor";
                
            case 'seo':
                return "SEO TOOL competitors include:
   - SEMrush, Ahrefs, Moz, Ubersuggest
   - SE Ranking, Serpstat, SpyFu, Mangools
   - Screaming Frog, Surfer SEO, Clearscope";
                
            case 'phone_communication':
                return "BUSINESS PHONE/VOIP competitors include:
   - RingCentral, Grasshopper, Dialpad, OpenPhone
   - Google Voice, Vonage, 8x8, Nextiva
   - Twilio, Aircall, JustCall, CloudTalk";
                
            case 'payments_ecommerce':
                return "PAYMENT/E-COMMERCE competitors include:
   - Stripe, Square, PayPal, Shopify Payments
   - WooCommerce, BigCommerce, Magento
   - Gumroad, Lemon Squeezy, Paddle";
                
            case 'automation':
                return "AUTOMATION competitors include:
   - Zapier, Make (Integromat), n8n, Workato
   - Microsoft Power Automate, IFTTT, Tray.io
   - Pabbly Connect, Integrately";
                
            case 'analytics':
                return "ANALYTICS competitors include:
   - Google Analytics, Mixpanel, Amplitude, Heap
   - Hotjar, FullStory, Plausible, Fathom
   - Databox, Klipfolio, Geckoboard";
                
            case 'food_service':
                return "Compare to other restaurants/food businesses in same cuisine and price range.";
                
            case 'retail':
                return "Compare to other online stores selling similar products.";
                
            case 'professional_services':
                return "Compare to other firms offering same professional services.";
                
            default:
                return "Find businesses offering the same core product or service.";
        }
    }
    
    /**
     * ✅ ENHANCED: Parse competitor list from AI response
     * Now handles PRIMARY and SECONDARY competitor classifications
     */
    private function parse_competitors_list($response) {
        $competitors = [];
        
        // Try to extract PRIMARY competitors first
        preg_match_all('/\*\*PRIMARY\s*\d+:\s*([^\*]+)\*\*/i', $response, $primary_matches);
        if (!empty($primary_matches[1])) {
            foreach ($primary_matches[1] as $index => $name) {
                $competitors[] = [
                    'name' => trim($name),
                    'index' => $index + 1,
                    'competitor_type' => 'PRIMARY',
                    'raw_data' => $this->extract_competitor_block_by_type($response, 'PRIMARY', $index + 1)
                ];
            }
        }
        
        // Then extract SECONDARY competitors
        preg_match_all('/\*\*SECONDARY\s*\d+:\s*([^\*]+)\*\*/i', $response, $secondary_matches);
        if (!empty($secondary_matches[1])) {
            foreach ($secondary_matches[1] as $index => $name) {
                $competitors[] = [
                    'name' => trim($name),
                    'index' => count($competitors) + 1,
                    'competitor_type' => 'SECONDARY',
                    'raw_data' => $this->extract_competitor_block_by_type($response, 'SECONDARY', $index + 1)
                ];
            }
        }
        
        // Fallback: Try old format if new format didn't work
        if (count($competitors) < 5) {
            preg_match_all('/\*\*COMPETITOR\s*\d+:\s*([^\*]+)\*\*/i', $response, $matches);
            if (!empty($matches[1])) {
                foreach ($matches[1] as $index => $name) {
                    $competitors[] = [
                        'name' => trim($name),
                        'index' => $index + 1,
                        'competitor_type' => 'PRIMARY', // Default to primary if format is ambiguous
                        'raw_data' => $this->extract_competitor_block($response, $index + 1)
                    ];
                }
            }
        }
        
        // Log what we found
        $primary_count = count(array_filter($competitors, fn($c) => $c['competitor_type'] === 'PRIMARY'));
        $secondary_count = count(array_filter($competitors, fn($c) => $c['competitor_type'] === 'SECONDARY'));
        error_log("✅ Parsed competitors: {$primary_count} PRIMARY, {$secondary_count} SECONDARY");
        
        // If parsing returned fewer than 6, do NOT pad with generic "Competitor N" placeholders.
        // Just use whatever real competitors the AI returned.
        if (count($competitors) < 6) {
            error_log('⚠️ Competitor parsing returned ' . count($competitors) . ' of expected 6+ — using available results only');
        }
        
        return array_slice($competitors, 0, 14); // Allow up to 14 competitors (8 primary + 6 secondary)
    }
    
    /**
     * ✅ NEW: Extract competitor block by type (PRIMARY or SECONDARY)
     */
    private function extract_competitor_block_by_type($response, $type, $index) {
        $pattern = '/\*\*' . $type . '\s*' . $index . ':[^\*]+\*\*(.*?)(?=\*\*(?:PRIMARY|SECONDARY)\s*\d+:|##|$)/is';
        if (preg_match($pattern, $response, $match)) {
            return trim($match[1]);
        }
        return '';
    }
    
    /**
     * Extract a specific competitor's data block from response
     */
    private function extract_competitor_block($response, $index) {
        $pattern = '/\*\*COMPETITOR\s*' . $index . ':[^\*]+\*\*(.*?)(?=\*\*COMPETITOR\s*\d+:|$)/is';
        if (preg_match($pattern, $response, $match)) {
            return trim($match[1]);
        }
        return '';
    }
    
    /**
     * Analyze market positioning based on discovered competitors
     */
    private function analyze_market_positioning($business_data, $competitors) {
        $competitor_names = array_column($competitors, 'name');
        $competitor_list = implode(', ', array_slice($competitor_names, 0, 10));
        
        $industry = $business_data['industry'] ?? 'general';
        $business_name = $business_data['business_name'] ?? 'This business';
        
        $prompt = "As a strategic positioning expert, analyze the market positioning landscape:

BUSINESS: {$business_name}
INDUSTRY: {$industry}
IDENTIFIED COMPETITORS: {$competitor_list}

MARKET POSITIONING ANALYSIS:

1. **Market Segmentation**:
   - How is this market segmented (price, quality, niche, geography)?
   - Which segments are oversaturated?
   - Which segments have opportunity?

2. **Positioning Map**:
   - Where do competitors cluster (premium vs budget, specialized vs generalist)?
   - Where are the white spaces (underserved positioning)?
   - Recommended positioning for the client

3. **Competitive Intensity**:
   - Overall competitiveness (High/Medium/Low)
   - Barriers to entry
   - Switching costs for customers

4. **Positioning Recommendations**:
   - Optimal positioning strategy
   - Differentiation opportunities
   - Positioning to AVOID

Provide actionable positioning strategy.";

        return [
            'analysis' => $this->call_openai_analysis($prompt, 3000, 'pro'),
            'competitor_count' => count($competitors),
            'timestamp' => current_time('mysql')
        ];
    }
    
    /**
     * Batch analyze competitor business models (3-4 at a time)
     */
    private function analyze_competitors_business_batch($business_data, $competitors, $batch_label) {
        if (empty($competitors)) {
            return ['analysis' => 'No competitors in this batch', 'timestamp' => current_time('mysql')];
        }
        
        $competitor_info = '';
        foreach ($competitors as $c) {
            $competitor_info .= "- {$c['name']}: {$c['raw_data']}\n";
        }
        
        $industry = $business_data['industry'] ?? 'general';
        
        $prompt = "As a business model analyst, perform DEEP ANALYSIS of these competitors (batch {$batch_label}):

INDUSTRY: {$industry}
COMPETITORS TO ANALYZE:
{$competitor_info}

For EACH competitor, analyze:

## [COMPETITOR NAME]

### Business Model Analysis
- **Revenue Model**: How they make money (subscription, one-time, freemium, etc.)
- **Pricing Strategy**: Price points, tiers, psychological tactics
- **Value Proposition**: Core promise to customers
- **Target Customer**: Specific customer profile

### Competitive Strategy
- **Differentiation**: What makes them unique
- **Market Position**: Premium/mid/budget positioning
- **Growth Strategy**: How they're expanding

### Strengths & Weaknesses
- **Key Strengths**: 3-4 competitive advantages
- **Key Weaknesses**: 3-4 vulnerabilities to exploit
- **Threats They Pose**: Risk to our client

### Opportunity Analysis
- **What They Do Better**: Learn from them
- **What They Do Poorly**: Opportunity to beat them
- **Gaps in Their Offering**: Unmet customer needs

Provide detailed, actionable analysis for each competitor.";

        return [
            'analysis' => $this->call_openai_analysis($prompt, 5000),
            'competitors_analyzed' => array_column($competitors, 'name'),
            'batch' => $batch_label,
            'timestamp' => current_time('mysql')
        ];
    }
    
    /**
     * Batch analyze competitor websites (3-4 at a time)
     */
    private function analyze_competitors_website_batch($business_data, $competitors, $batch_label) {
        if (empty($competitors)) {
            return ['analysis' => 'No competitors in this batch', 'timestamp' => current_time('mysql')];
        }
        
        $competitor_names = implode(', ', array_column($competitors, 'name'));
        $industry = $business_data['industry'] ?? 'general';
        
        $prompt = "As a UX strategist and website analyst, analyze the WEBSITES of these competitors (batch {$batch_label}):

INDUSTRY: {$industry}
COMPETITOR WEBSITES TO ANALYZE: {$competitor_names}

For EACH competitor website, analyze:

## [COMPETITOR NAME] Website Analysis

### Site Architecture
- **Page Structure**: Key pages and hierarchy
- **Navigation**: Menu organization and user flow
- **Funnel Design**: How they guide visitors to conversion

### Conversion Strategy
- **Primary CTA**: Main call-to-action and placement
- **Lead Capture**: Forms, popups, chat, booking systems
- **Trust Elements**: Reviews, testimonials, certifications, guarantees
- **Urgency/Scarcity**: Tactics used to drive action

### User Experience
- **First Impression**: Above-the-fold effectiveness
- **Mobile Experience**: Mobile-first or responsive quality
- **Page Speed**: Fast/Medium/Slow perception
- **Content Clarity**: How well they communicate value

### Design & Branding
- **Visual Style**: Modern/Traditional/Minimal/Bold
- **Brand Consistency**: Logo, colors, typography coherence
- **Imagery Quality**: Photos, graphics, video usage

### Technology Indicators
- **Likely CMS**: WordPress, Shopify, Custom, etc.
- **Key Integrations**: Chat, booking, payments visible
- **Tracking**: Analytics, pixels, remarketing indicators

### Website Strengths & Weaknesses
- **What Works Well**: Elements to learn from
- **What's Weak**: Opportunities to beat them
- **Missing Elements**: Features they lack

Provide specific, actionable website intelligence.";

        return [
            'analysis' => $this->call_openai_analysis($prompt, 5000),
            'competitors_analyzed' => array_column($competitors, 'name'),
            'batch' => $batch_label,
            'timestamp' => current_time('mysql')
        ];
    }
    
    /**
     * Batch analyze competitor SEO strategies (3-4 at a time)
     */
    private function analyze_competitors_seo_batch($business_data, $competitors, $batch_label) {
        if (empty($competitors)) {
            return ['analysis' => 'No competitors in this batch', 'timestamp' => current_time('mysql')];
        }
        
        $competitor_names = implode(', ', array_column($competitors, 'name'));
        $industry = $business_data['industry'] ?? 'general';
        $location = $business_data['location'] ?? '';
        
        $prompt = "As an SEO and content strategy expert, analyze the SEO approach of these competitors (batch {$batch_label}):

INDUSTRY: {$industry}
LOCATION: {$location}
COMPETITORS TO ANALYZE: {$competitor_names}

For EACH competitor, analyze:

## [COMPETITOR NAME] SEO Analysis

### Keyword Strategy
- **Primary Keywords**: What terms they likely target
- **Long-Tail Focus**: Specific phrases they rank for
- **Local SEO**: Location-based optimization
- **Keyword Gaps**: Terms they're missing

### Content Strategy
- **Blog Presence**: Frequency, topics, quality
- **Content Types**: Articles, videos, guides, case studies
- **Content Depth**: Thin vs comprehensive content
- **Authority Content**: Thought leadership pieces

### Technical SEO Indicators
- **Site Structure**: URL patterns, hierarchy
- **Schema Usage**: Rich snippets, structured data
- **Page Speed**: Core Web Vitals perception
- **Mobile Optimization**: Mobile-first approach

### Link Building Signals
- **Authority Level**: Perceived domain strength
- **Content Linkability**: Shareable/linkable assets
- **Brand Mentions**: Industry presence

### SEO Strengths & Weaknesses
- **What They Do Well**: SEO advantages
- **Where They're Weak**: Opportunities to outrank
- **Content Gaps**: Topics they don't cover

### Recommended Counter-Strategy
- **Keywords to Target**: Beat them on these terms
- **Content to Create**: Fill their gaps
- **Technical Improvements**: Where to outperform

Provide actionable SEO intelligence for each competitor.";

        return [
            'analysis' => $this->call_openai_analysis($prompt, 5000),
            'competitors_analyzed' => array_column($competitors, 'name'),
            'batch' => $batch_label,
            'timestamp' => current_time('mysql')
        ];
    }
    
    /**
     * Analyze website best practices when competitors aren't available
     */
    private function analyze_website_best_practices($business_data) {
        $industry = $business_data['industry'] ?? 'general';
        $business_type = $business_data['business_type'] ?? 'service';
        
        $prompt = "As a website strategy expert, provide comprehensive website best practices for:

INDUSTRY: {$industry}
BUSINESS TYPE: {$business_type}

Analyze:
1. Optimal site structure and page hierarchy
2. Must-have pages and their content
3. Conversion optimization best practices
4. Trust and credibility elements
5. Mobile optimization requirements
6. Technology stack recommendations
7. Common website mistakes to avoid
8. Competitive advantages through website design

Provide specific, actionable recommendations.";

        return [
            'analysis' => $this->call_openai_analysis($prompt, 4000),
            'timestamp' => current_time('mysql')
        ];
    }
    
    // =========================================================================
    // SYNTHESIS METHODS (CHUNK 8)
    // =========================================================================
    
    /**
     * Generate side-by-side comparative matrix
     */
    private function generate_comparative_matrix($business_data, $results) {
        $competitors = $results['competitors_list'] ?? [];
        $competitor_names = array_column($competitors, 'name');
        $competitor_list = implode(', ', array_slice($competitor_names, 0, 10));
        
        // Gather competitor analysis summaries
        $business_insights = $results['competitor_business_batch_1']['analysis'] ?? '';
        $website_insights = $results['competitor_website_batch_1']['analysis'] ?? '';
        
        $industry = $business_data['industry'] ?? 'general';
        $business_name = $business_data['business_name'] ?? 'Client Business';
        
        $prompt = "As a competitive strategy consultant, create a COMPARATIVE MATRIX:

BUSINESS: {$business_name}
INDUSTRY: {$industry}
COMPETITORS: {$competitor_list}

Using the research insights, create a comprehensive comparison:

## COMPARATIVE MATRIX

### Pricing Comparison Table
| Competitor | Price Range | Pricing Model | Free Trial | Money-Back Guarantee |
|------------|-------------|---------------|------------|---------------------|
(Fill for each competitor)

### Feature Comparison Table
| Feature | Our Client | Comp 1 | Comp 2 | Comp 3 | Comp 4 | Comp 5 |
|---------|------------|--------|--------|--------|--------|--------|
(Key features comparison)

### Strengths/Weaknesses Summary
| Competitor | Top 3 Strengths | Top 3 Weaknesses |
|------------|-----------------|------------------|
(For each competitor)

### Website Quality Comparison
| Competitor | Design Score | UX Score | SEO Score | Conversion Optimization |
|------------|--------------|----------|-----------|------------------------|
(1-10 rating for each)

### Market Position Summary
| Competitor | Position | Target Market | Differentiation |
|------------|----------|---------------|-----------------|

### KEY INSIGHTS
1. What ALL competitors do well (industry standards)
2. What MOST competitors do poorly (opportunities)
3. What NO ONE does well (major opportunity)
4. Recommended competitive advantages for our client

Provide structured, actionable competitive intelligence.";

        return [
            'matrix' => $this->call_openai_analysis($prompt, 5000),
            'competitors_compared' => count($competitors),
            'timestamp' => current_time('mysql')
        ];
    }
    
    /**
     * Synthesize market gaps from all research
     */
    private function synthesize_market_gaps($business_data, $results) {
        $industry = $business_data['industry'] ?? 'general';
        $business_name = $business_data['business_name'] ?? 'Client';
        
        // Compile key findings
        $market_trends = $results['market_trends']['analysis'] ?? '';
        $competitor_weaknesses = '';
        if (!empty($results['competitor_business_batch_1']['analysis'])) {
            $competitor_weaknesses = substr($results['competitor_business_batch_1']['analysis'], 0, 2000);
        }
        
        $prompt = "As a market opportunity analyst, identify MARKET GAPS based on comprehensive research:

BUSINESS: {$business_name}
INDUSTRY: {$industry}

RESEARCH CONTEXT (summarized):
- Market Trends: Multiple growth opportunities identified
- Competitor Analysis: Weaknesses and gaps discovered across 10-12 competitors

## MARKET GAP ANALYSIS

### 1. UNMET CUSTOMER NEEDS
Identify 5-7 customer needs that competitors FAIL to address:
- Need: [Description]
- Evidence: [Why this is unmet]
- Opportunity Size: [High/Medium/Low]
- Difficulty to Address: [Easy/Medium/Hard]

### 2. SERVICE/PRODUCT GAPS
What offerings are missing from the market:
- Gap: [Description]
- Why It Exists: [Barriers or oversight]
- Revenue Potential: [Estimate]

### 3. EXPERIENCE GAPS
Where customer experience falls short industry-wide:
- Gap: [Description]  
- Impact: [Customer frustration points]
- Solution: [How to fill it]

### 4. TECHNOLOGY GAPS
Where competitors lag in technology/automation:
- Gap: [Description]
- Advantage: [What tech solution provides]

### 5. PRICING/BUSINESS MODEL GAPS
Underserved price points or business model innovations:
- Gap: [Description]
- Opportunity: [How to exploit]

### 6. CONTENT/MARKETING GAPS
Information and marketing opportunities competitors miss:
- Gap: [Description]
- Content Strategy: [How to fill]

### PRIORITIZED OPPORTUNITY LIST
Rank top 10 gaps by:
1. [Gap] - Impact: X, Difficulty: Y, Priority: Z
(List all 10)

Provide specific, actionable gap analysis.";

        return [
            'analysis' => $this->call_openai_analysis($prompt, 4500),
            'timestamp' => current_time('mysql')
        ];
    }
    
    /**
     * Map strategic advantages for the business
     */
    private function map_strategic_advantages($business_data, $results) {
        $industry = $business_data['industry'] ?? 'general';
        $business_name = $business_data['business_name'] ?? 'Client';
        $offerings = $business_data['offerings'] ?? '';
        
        $market_gaps = $results['market_gaps']['analysis'] ?? '';
        
        $prompt = "As a strategic advisor, map out COMPETITIVE ADVANTAGES for:

BUSINESS: {$business_name}
INDUSTRY: {$industry}
OFFERINGS: {$offerings}

Based on the market gaps and competitor weaknesses identified, define:

## STRATEGIC ADVANTAGE MAPPING

### 1. IMMEDIATE ADVANTAGES (Launch with these)
Quick wins that differentiate from day one:
- **Advantage**: [Description]
- **How to Implement**: [Specific action]
- **Competitor Weakness Exploited**: [Which gap]
- **Customer Benefit**: [Value to customer]

(List 5-7 immediate advantages)

### 2. SPEED ADVANTAGES
How to move faster than competitors:
- Faster response time
- Faster delivery
- Faster onboarding
- Faster results

### 3. EXPERIENCE ADVANTAGES
Superior customer experience differentiators:
- Pre-purchase experience
- Purchase experience
- Post-purchase experience
- Support experience

### 4. TECHNOLOGY ADVANTAGES
Tech-enabled competitive edges:
- Automation opportunities
- AI-powered features
- Integration advantages
- Mobile/accessibility advantages

### 5. PRICING ADVANTAGES
Strategic pricing differentiators:
- Value-based positioning
- Pricing model innovation
- Risk reversal strategies
- Bundle opportunities

### 6. CONTENT/SEO ADVANTAGES
Information and visibility advantages:
- Content gaps to fill
- Keywords to dominate
- Authority-building strategy

### 7. WEBSITE ADVANTAGES
Website design and UX advantages:
- Conversion optimization opportunities
- Trust-building elements
- User flow improvements

### STRATEGIC PRIORITY MATRIX
| Advantage | Impact | Effort | Priority | Timeline |
|-----------|--------|--------|----------|----------|
(Top 15 advantages ranked)

### IMPLEMENTATION ROADMAP
- Week 1-2: [Quick wins]
- Month 1: [Foundation advantages]
- Month 2-3: [Growth advantages]
- Month 4-6: [Dominance advantages]

Provide specific, actionable strategic advantages.";

        return [
            'analysis' => $this->call_openai_analysis($prompt, 5000),
            'timestamp' => current_time('mysql')
        ];
    }
    
    /**
     * Combined opportunities and threats analysis
     */
    private function analyze_opportunities_threats($business_data, $results) {
        $industry = $business_data['industry'] ?? 'general';
        
        $prompt = "As a strategic risk analyst, provide comprehensive OPPORTUNITIES & THREATS:

INDUSTRY: {$industry}

## OPPORTUNITIES (Ranked by Impact)

### Market Opportunities
1. [Opportunity] - Probability: X%, Impact: High/Medium
(List 5-7)

### Technology Opportunities
1. [Opportunity]
(List 3-5)

### Competitive Opportunities
1. [Opportunity] - Based on competitor weaknesses
(List 5-7)

## THREATS (Ranked by Risk)

### Market Threats
1. [Threat] - Probability: X%, Severity: High/Medium
- Mitigation Strategy: [Action]
(List 5-7)

### Competitive Threats
1. [Threat]
- Mitigation: [Action]
(List 3-5)

### Technology/Disruption Threats
1. [Threat]
- Mitigation: [Action]
(List 3-5)

### Economic/External Threats
1. [Threat]
- Mitigation: [Action]
(List 3-5)

## RISK MITIGATION PLAN
Priority actions to address top 5 threats.

Provide actionable intelligence.";

        return [
            'analysis' => $this->call_openai_analysis($prompt, 4000),
            'timestamp' => current_time('mysql')
        ];
    }
    
    /**
     * Generate comprehensive Phase 3 summary
     */
    private function generate_phase3_summary($business_data, $results) {
        $business_name = $business_data['business_name'] ?? 'Client Business';
        $industry = $business_data['industry'] ?? 'general';
        $competitors_count = count($results['competitors_list'] ?? []);
        
        $prompt = "As a strategic consultant, create an EXECUTIVE SUMMARY of Phase 3 Deep Research:

BUSINESS: {$business_name}
INDUSTRY: {$industry}
COMPETITORS ANALYZED: {$competitors_count}

## PHASE 3 RESEARCH EXECUTIVE SUMMARY

### Research Overview
- Total competitors analyzed
- Research methodology
- Key data sources considered

### Market Landscape Summary
(3-4 sentences on market conditions)

### Competitive Position Summary
(3-4 sentences on competitive environment)

### Top 5 Key Findings
1. [Most important insight]
2. [Second most important]
3. [Third]
4. [Fourth]
5. [Fifth]

### Top 5 Opportunities Identified
1. [Biggest opportunity]
2-5. [Others]

### Top 5 Threats to Address
1. [Biggest threat]
2-5. [Others]

### Top 5 Recommended Actions
1. [Highest priority action]
2-5. [Others]

### Strategic Recommendations
- Positioning: [Recommended market position]
- Differentiation: [Key differentiators to emphasize]
- Pricing: [Pricing strategy recommendation]
- Website: [Website strategy recommendation]
- Marketing: [Marketing priority recommendation]

### Next Steps
What should be implemented based on this research.

Provide a clear, actionable executive summary.";

        return $this->call_openai_analysis($prompt, 3500);
    }
    
    /**
     * Get existing research results for a user (for chunked continuation)
     */
    private function get_existing_research_results($user_id) {
        global $wpdb;
        
        try {
            $table = $wpdb->prefix . 'vmo_deep_research';
            
            // Check if table exists (suppress errors for clean check)
            $wpdb->suppress_errors(true);
            $table_exists = $wpdb->get_var("SHOW TABLES LIKE '{$table}'") === $table;
            $wpdb->suppress_errors(false);
            
            if (!$table_exists) {
                return null;
            }
            
            $row = $wpdb->get_row($wpdb->prepare(
                "SELECT research_data FROM {$table} WHERE user_id = %d ORDER BY created_at DESC LIMIT 1",
                $user_id
            ));
            
            if ($row && !empty($row->research_data)) {
                $data = json_decode($row->research_data, true);
                // Only return if it's a chunked result that's not complete (8 chunks for Phase 3)
                if (is_array($data) && isset($data['chunk_completed']) && $data['chunk_completed'] < self::TOTAL_CHUNKS) {
                    error_log('🔍 VMO Phase 3: Found existing partial results, chunk_completed=' . $data['chunk_completed'] . ' of ' . self::TOTAL_CHUNKS);
                    return $data;
                }
            }
            
            return null;
        } catch (\Throwable $e) {
             error_log('❌ VMO Deep Research: Error getting existing results: ' . $e->getMessage());
             return null;
        }
    }

    /**
     * ✅ PUBLIC: Get the latest research data available (Complete or Partial)
     * Used by other system components (Wizard, Website Generator) to avoid re-running research.
     */
    public function get_latest_research($user_id) {
        global $wpdb;
        try {
            $table = $wpdb->prefix . 'vmo_deep_research';
            $wpdb->suppress_errors(true);
            $table_exists = $wpdb->get_var("SHOW TABLES LIKE '{$table}'") === $table;
            $wpdb->suppress_errors(false);
            
            if (!$table_exists) return null;
            
            $row = $wpdb->get_row($wpdb->prepare(
                "SELECT research_data FROM {$table} WHERE user_id = %d ORDER BY created_at DESC LIMIT 1",
                $user_id
            ));
            
            if ($row && !empty($row->research_data)) {
                return json_decode($row->research_data, true);
            }
            return null;
        } catch (\Throwable $e) {
            return null;
        }
    }
    
    /**
     * LEGACY: Original all-at-once method for backward compatibility
     * ⚠️ May timeout on slow connections - prefer conduct_deep_research with chunking
     */
    public function conduct_deep_research_full($business_data) {
        error_log('🔍 VMO Deep Research: Starting FULL research (legacy mode)...');
        
        // Increase execution time for long research - UNLIMITED
        if (function_exists('set_time_limit')) {
            @set_time_limit(0); // Unlimited - deep research must never timeout
        }
        if (function_exists('ignore_user_abort')) {
            @ignore_user_abort(true); // Continue even if user navigates away
        }
        
        $research_results = [
            'vision_analysis' => $this->safe_analysis('vision_and_intent', $business_data),
            'structure_analysis' => $this->safe_analysis('business_structure', $business_data),
            'target_market_intelligence' => $this->safe_analysis('target_market_intelligence', $business_data),
            'competitor_landscape' => $this->safe_analysis('competitors', $business_data),
            'market_trends' => $this->safe_analysis('market_trends', $business_data),
            'operations_analysis' => $this->safe_analysis('operations_and_delivery', $business_data),
            'brand_positioning' => $this->safe_analysis('brand_positioning', $business_data),
            'marketing_strategy' => $this->safe_analysis('marketing_strategy', $business_data),
            'keyword_research' => $this->safe_analysis('keyword_research', $business_data),
            'legal_compliance' => $this->safe_analysis('legal_compliance', $business_data),
            'technology_stack' => $this->safe_analysis('technology_stack', $business_data),
            'success_metrics' => $this->safe_analysis('success_metrics', $business_data),
            'industry_insights' => $this->safe_analysis('industry_insights', $business_data),
            'target_audience_analysis' => $this->safe_analysis('target_audience', $business_data),
            'opportunities' => $this->safe_analysis('opportunities', $business_data),
            'threats' => $this->safe_analysis('threats', $business_data),
            'pricing_analysis' => $this->safe_analysis('pricing_strategy', $business_data),
            'potential_leads' => $this->safe_analysis('potential_leads', $business_data),
            'research_timestamp' => current_time('mysql'),
            'research_framework' => '10-category-v1',
            'research_summary' => '',
            'contrast_analysis' => []
        ];
        
        $research_results['contrast_analysis'] = $this->generate_contrast_analysis($business_data, $research_results);
        $research_results['research_summary'] = $this->generate_research_summary($research_results, $business_data);
        $this->store_research_results(get_current_user_id(), $research_results);
        
        if (!empty($research_results['potential_leads'])) {
            $this->save_leads_to_crm(get_current_user_id(), $research_results['potential_leads'], $business_data);
        }
        
        error_log('✅ VMO Deep Research: FULL research completed');
        
        return $research_results;
    }
    
    /**
     * CATEGORY 1: Analyze Vision & Intent
     * Clarify the REAL goal behind user's stated goal
     */
    private function analyze_vision_and_intent($business_data) {
        $business_goal = $business_data['business_goal'] ?? $business_data['goals'] ?? '';
        $success_definition = $business_data['success_definition'] ?? '';
        $income_intent = $business_data['income_intent'] ?? '';
        
        $prompt = "As a business strategy consultant, analyze this entrepreneur's vision and intent:

STATED INFORMATION:
- Business Goal: {$business_goal}
- Success Definition: {$success_definition}
- Income Intent: {$income_intent}

VISION ANALYSIS:
1. What is the REAL goal behind their stated goal?
2. Are there lifestyle vs. wealth vs. impact priorities we can identify?
3. What unstated assumptions might limit their success?
4. What alternative visions might serve them better?
5. What's the gap between their dream and practical reality?

Provide insights that will help us recommend the right business structure and approach.";

        return [
            'analysis' => $this->call_openai_analysis($prompt, 1500),
            'timestamp' => current_time('mysql')
        ];
    }
    
    /**
     * CATEGORY 2: Analyze Business Structure
     * Validate proposed model, research alternatives
     */
    private function analyze_business_structure($business_data) {
        $business_type = $business_data['business_type'] ?? '';
        $operating_model = $business_data['operating_model'] ?? '';
        $offerings = $business_data['offerings'] ?? $business_data['services'] ?? '';
        
        $prompt = "As a business model strategist, analyze this business structure:

PROPOSED STRUCTURE:
- Business Type: {$business_type} (product/service/both)
- Operating Model: {$operating_model} (online/physical/hybrid)
- Offerings: {$offerings}

STRUCTURE ANALYSIS:
1. Is this business model viable in the current market?
2. What alternative models might work better?
3. Should they consider subscription/recurring revenue?
4. Is their product vs service balance optimal?
5. What hybrid opportunities exist?
6. What's the path to scalability?

Provide recommendations for optimizing their business structure.";

        return [
            'analysis' => $this->call_openai_analysis($prompt, 1500),
            'alternative_models' => [],
            'timestamp' => current_time('mysql')
        ];
    }
    
    /**
     * CATEGORY 3: Target Market Intelligence (CRITICAL!)
     * Find WHO actually buys - may differ from user's guess!
     */
    private function analyze_target_market_intelligence($business_data) {
        $perceived_market = $business_data['target_market'] ?? $business_data['perceived_target_market'] ?? '';
        $business_type = $business_data['business_type'] ?? '';
        $offerings = $business_data['offerings'] ?? $business_data['services'] ?? '';
        $location = $business_data['location'] ?? '';
        
        $prompt = "As a market research specialist, conduct COMPREHENSIVE target market intelligence:

BUSINESS CONTEXT:
- User's Perceived Target: {$perceived_market}
- Business Type: {$business_type}
- Offerings: {$offerings}
- Location: {$location}

🚨 CRITICAL: The user may NOT know their optimal target market!
Your job is to find opportunities they don't know about.

TARGET MARKET INTELLIGENCE:
1. WHO actually buys in this space? (Demographics, psychographics, behaviors)
2. Are there underserved segments with HIGH demand?
3. What are the buying patterns and decision factors?
4. What surprising opportunities exist? (Example: A photographer might target individuals, but schools/organizations need bulk photography with higher order values)
5. B2B vs B2C vs B2B2C opportunities?
6. Which segments are oversaturated vs underserved?
7. What's the customer acquisition cost for each segment?
8. Which segment has the highest lifetime value?

Provide ACTIONABLE intelligence that may reveal opportunities the user didn't consider.";

        return [
            'analysis' => $this->call_openai_analysis($prompt, 2000),
            'alternative_markets' => [],
            'underserved_segments' => [],
            'timestamp' => current_time('mysql')
        ];
    }
    
    /**
     * CATEGORY 5: Operations & Delivery Analysis
     */
    private function analyze_operations_and_delivery($business_data) {
        $business_type = $business_data['business_type'] ?? '';
        $operating_model = $business_data['operating_model'] ?? '';
        $offerings = $business_data['offerings'] ?? $business_data['services'] ?? '';
        
        $prompt = "As an operations consultant, analyze optimal delivery and operations:

BUSINESS CONTEXT:
- Business Type: {$business_type}
- Operating Model: {$operating_model}
- Offerings: {$offerings}

OPERATIONS ANALYSIS:
1. What's the optimal fulfillment method for this business?
2. Shipping, logistics, service delivery best practices?
3. Tools and systems that reduce operational friction?
4. Inventory vs dropship vs on-demand options?
5. Scheduling, booking, appointment systems needed?
6. What operational bottlenecks should they avoid?
7. Automation opportunities to reduce manual work?

Provide recommendations for streamlined operations.";

        return [
            'analysis' => $this->call_openai_analysis($prompt, 1500),
            'recommended_tools' => [],
            'timestamp' => current_time('mysql')
        ];
    }
    
    /**
     * CATEGORY 6: Brand & Positioning Analysis
     */
    private function analyze_brand_positioning($business_data) {
        $business_type = $business_data['business_type'] ?? '';
        $industry = $business_data['industry'] ?? '';
        $target_market = $business_data['target_market'] ?? '';
        $website_style = $business_data['website_style_preference'] ?? '';
        
        $prompt = "As a brand strategist, analyze positioning opportunities:

BUSINESS CONTEXT:
- Business Type: {$business_type}
- Industry: {$industry}
- Target Market: {$target_market}
- Desired Website Style: {$website_style}

BRAND POSITIONING ANALYSIS:
1. What unique positioning angles haven't been claimed by competitors?
2. What brand archetypes resonate in this space?
3. Customer experience expectations?
4. Color psychology and imagery trends for this industry?
5. Tone of voice recommendations?
6. How to differentiate in a crowded market?
7. Does their desired website style match what converts in this market?

Provide positioning strategy that will help them stand out.";

        return [
            'analysis' => $this->call_openai_analysis($prompt, 1500),
            'positioning_angles' => [],
            'timestamp' => current_time('mysql')
        ];
    }
    
    /**
     * CATEGORY 7: Marketing Strategy Analysis
     */
    private function analyze_marketing_strategy($business_data) {
        $business_type = $business_data['business_type'] ?? '';
        $target_market = $business_data['target_market'] ?? '';
        $budget = $business_data['constraints'] ?? '';
        
        $prompt = "As a marketing strategist, analyze optimal marketing approach:

BUSINESS CONTEXT:
- Business Type: {$business_type}
- Target Market: {$target_market}
- Constraints/Budget: {$budget}

MARKETING STRATEGY ANALYSIS:
1. Highest-ROI marketing channels for this business?
2. Social media platforms where target audience lives?
3. Content marketing strategies that work in this space?
4. Email marketing benchmarks and strategies?
5. Referral, partnership, affiliate opportunities?
6. Paid advertising effectiveness and costs?
7. What marketing channels do competitors neglect?

Provide prioritized marketing recommendations.";

        return [
            'analysis' => $this->call_openai_analysis($prompt, 1500),
            'recommended_channels' => [],
            'timestamp' => current_time('mysql')
        ];
    }
    
    /**
     * CATEGORY 8: Legal & Compliance Analysis
     */
    private function analyze_legal_compliance($business_data) {
        $business_type = $business_data['business_type'] ?? '';
        $industry = $business_data['industry'] ?? '';
        $location = $business_data['location'] ?? '';
        
        $prompt = "As a business compliance consultant, analyze legal requirements:

BUSINESS CONTEXT:
- Business Type: {$business_type}
- Industry: {$industry}
- Location: {$location}

LEGAL & COMPLIANCE ANALYSIS:
1. What licenses, permits, certifications are required?
2. Industry-specific regulations?
3. Geographical restrictions or requirements?
4. Tax implications (sales tax, business structure)?
5. Insurance requirements?
6. Payment processing requirements (PCI compliance)?
7. Privacy and data protection requirements (GDPR, CCPA)?

Provide compliance checklist for this business.";

        return [
            'analysis' => $this->call_openai_analysis($prompt, 1500),
            'requirements' => [],
            'timestamp' => current_time('mysql')
        ];
    }
    
    /**
     * CATEGORY 9: Technology Stack Analysis
     */
    private function analyze_technology_stack($business_data) {
        $business_type = $business_data['business_type'] ?? '';
        $features = $business_data['feature_wishlist'] ?? '';
        
        $prompt = "As a technology consultant, analyze optimal tech stack:

BUSINESS CONTEXT:
- Business Type: {$business_type}
- Desired Features: {$features}

TECHNOLOGY ANALYSIS:
1. What technology stack do successful competitors use?
2. Automation opportunities to reduce manual work?
3. Integrations that streamline operations?
4. AI/ML tools applicable to this business?
5. CRM, email, analytics tools used in industry?
6. E-commerce platform recommendations?
7. Hosting and performance considerations?

Provide technology recommendations.";

        return [
            'analysis' => $this->call_openai_analysis($prompt, 1500),
            'recommended_tools' => [],
            'timestamp' => current_time('mysql')
        ];
    }
    
    /**
     * CATEGORY 10: Success Metrics Analysis
     */
    private function analyze_success_metrics($business_data) {
        $business_type = $business_data['business_type'] ?? '';
        $success_definition = $business_data['success_definition'] ?? '';
        $timeline = $business_data['timeline'] ?? '';
        
        $prompt = "As a business performance consultant, analyze success metrics:

BUSINESS CONTEXT:
- Business Type: {$business_type}
- User's Success Definition: {$success_definition}
- Timeline: {$timeline}

SUCCESS METRICS ANALYSIS:
1. Industry benchmarks (conversion rates, CAC, LTV)?
2. KPIs that matter most for this business type?
3. Revenue milestones and growth trajectories?
4. Early warning indicators of problems?
5. What metrics should they track daily/weekly/monthly?
6. Realistic timeline expectations?

Provide KPI framework for this business.";

        return [
            'analysis' => $this->call_openai_analysis($prompt, 1500),
            'kpis' => [],
            'benchmarks' => [],
            'timestamp' => current_time('mysql')
        ];
    }
    
    /**
     * Generate Contrast Analysis
     * Compare user's original ideas against research findings
     */
    private function generate_contrast_analysis($business_data, $research_results) {
        $user_target = $business_data['target_market'] ?? $business_data['perceived_target_market'] ?? '';
        $user_style = $business_data['website_style_preference'] ?? '';
        $user_features = $business_data['feature_wishlist'] ?? '';
        
        $target_research = $research_results['target_market_intelligence']['analysis'] ?? '';
        $brand_research = $research_results['brand_positioning']['analysis'] ?? '';
        
        $prompt = "Based on the user's original preferences and our research, identify CONTRASTS:

USER'S ORIGINAL INPUT:
- Target Market: {$user_target}
- Website Style: {$user_style}
- Desired Features: {$user_features}

RESEARCH FINDINGS:
{$target_research}

{$brand_research}

Generate a contrast analysis in JSON format:
{
  \"contrasts\": [
    {
      \"area\": \"Target Market\",
      \"user_idea\": \"what they said\",
      \"research_finding\": \"what we discovered\",
      \"gap_significance\": \"high/medium/low\",
      \"recommendation\": \"what we suggest\"
    }
  ],
  \"opportunities\": [\"list of opportunities user didn't consider\"],
  \"risks\": [\"potential problems with original plan\"]
}

Return ONLY valid JSON.";

        $result = $this->call_openai_analysis($prompt, 2000);
        
        // Try to parse JSON
        $json = json_decode($result, true);
        if (json_last_error() === JSON_ERROR_NONE) {
            return $json;
        }
        
        // Return raw analysis if JSON parsing fails
        return [
            'raw_analysis' => $result,
            'contrasts' => [],
            'opportunities' => [],
            'risks' => []
        ];
    }
    
    /**
     * Analyze competitors in the user's industry and location
     */
    private function analyze_competitors($business_data) {
        $business_type = $business_data['business_type'] ?? 'business';
        $industry = $business_data['industry'] ?? '';
        $location = $business_data['location'] ?? '';
        $services = $business_data['services'] ?? '';
        
        $prompt = "As a business intelligence analyst, conduct a comprehensive competitor analysis:

BUSINESS CONTEXT:
- Business Type: {$business_type}
- Industry: {$industry}
- Location: {$location}
- Services/Products: {$services}

COMPETITOR ANALYSIS TASKS:

1. **Top 5-10 Competitors**: Identify the main competitors in this space.
   For each competitor provide:
   - Company name
   - Website features and strengths
   - Unique selling propositions
   - Pricing strategy (if known)
   - Market positioning
   - Customer experience approach
   - Technology stack and features
   - Social media presence
   - Content strategy

2. **Competitive Landscape**:
   - Market leaders and why they lead
   - Mid-tier players and their niches
   - Emerging competitors to watch
   - Market share distribution (estimate)

3. **Website Design Patterns**:
   - Common color schemes in this industry
   - Typical page structures
   - Must-have features customers expect
   - Innovative features some competitors offer
   - Mobile experience standards
   - Speed and performance benchmarks

4. **Content Strategy Analysis**:
   - What content do top competitors publish?
   - Blog frequency and topics
   - Video and multimedia usage
   - Lead magnets and free resources
   - SEO content strategies

5. **Competitive Gaps**:
   - Services competitors offer that we should consider
   - Features missing from competitor sites
   - Unmet customer needs
   - Opportunities for differentiation

Provide detailed, actionable intelligence that will inform website design, feature selection, and business strategy.";

        $analysis = $this->call_openai_analysis($prompt, 4000);
        
        return [
            'analysis' => $analysis,
            'competitors_identified' => $this->extract_competitor_count($analysis),
            'key_insights' => $this->extract_key_insights($analysis),
            'timestamp' => current_time('mysql')
        ];
    }
    
    /**
     * Analyze market trends using AI - UNIVERSAL approach for any business type
     */
    private function analyze_market_trends($business_data) {
        $industry = $business_data['industry'] ?? '';
        $business_type = $business_data['business_type'] ?? '';
        $location = $business_data['location'] ?? '';
        $offerings = $business_data['offerings'] ?? '';
        
        // Detect category for context-aware analysis
        $category = $this->detect_business_category($business_data);
        
        $prompt = "As a market research analyst, analyze the SPECIFIC market trends for this business:

═══════════════════════════════════════════════════════════════
BUSINESS BEING ANALYZED:
═══════════════════════════════════════════════════════════════
- Industry/Category: {$industry}
- Business Type: {$business_type}
- Location: {$location}
- What They Offer: {$offerings}

═══════════════════════════════════════════════════════════════
MARKET TRENDS ANALYSIS (Be specific to THIS industry):
═══════════════════════════════════════════════════════════════

1. **Market Size & Growth**:
   - Current market size for THIS SPECIFIC industry (with dollar amounts)
   - Annual growth rate (CAGR)
   - 3-5 year projections
   - Key market segments

2. **Customer Behavior Trends**:
   - How customers in this industry make purchasing decisions
   - Online vs. offline behavior patterns
   - What they value most when choosing a provider
   - Trust and credibility signals they seek

3. **Technology & Innovation**:
   - Emerging technologies affecting THIS industry
   - AI and automation impact
   - Digital transformation patterns
   - Innovation opportunities

4. **Competitive Dynamics**:
   - Market consolidation trends
   - New entrant activity
   - Pricing pressures
   - Differentiation strategies that work

5. **Marketing & Distribution**:
   - Most effective customer acquisition channels
   - Content and social media trends
   - Partnership opportunities
   - Customer retention strategies

6. **Future Outlook**:
   - Key trends shaping the next 3-5 years
   - Potential disruptions
   - Emerging opportunities
   - Risks to monitor

Provide SPECIFIC market data, numbers, and percentages where available.
Focus on actionable insights directly relevant to THIS type of business.";

        $trends = $this->call_openai_analysis($prompt, 3500);
        
        return [
            'analysis' => $trends,
            'key_trends' => $this->extract_key_trends($trends),
            'opportunities' => $this->extract_opportunities_from_trends($trends),
            'category' => $category,
            'timestamp' => current_time('mysql')
        ];
    }
    
    /**
     * ✅ UNIVERSAL: Generate structured market intelligence for ANY business type
     * This returns JSON-parseable data for display in the market intelligence intro
     */
    private function analyze_structured_market_intelligence($business_data, $competitors_list = []) {
        $industry = $business_data['industry'] ?? '';
        $business_type = $business_data['business_type'] ?? '';
        $location = $business_data['location'] ?? '';
        $offerings = $business_data['offerings'] ?? $business_data['services'] ?? '';
        
        // Detect category for context-aware analysis
        $category = $this->detect_business_category($business_data);
        
        // Build competitor names for context
        $competitor_names = [];
        foreach ($competitors_list as $comp) {
            if (is_array($comp) && !empty($comp['name'])) {
                $competitor_names[] = $comp['name'];
            } elseif (is_string($comp)) {
                $competitor_names[] = $comp;
            }
        }
        $competitor_context = !empty($competitor_names) ? implode(', ', array_slice($competitor_names, 0, 8)) : 'competitors to be analyzed';
        
        // Build universal prompt that works for any business
        $prompt = $this->build_universal_market_intelligence_prompt($business_data, $competitor_context, $category);
        
        error_log('📊 Market Intelligence: category=' . $category);

        $response = $this->call_openai_analysis($prompt, 4000, 'pro');
        
        // Try to parse JSON from response
        $structured_data = $this->parse_json_from_response($response);
        
        if ($structured_data) {
            // Guardrail: if the model "confirms" an unrelated industry while our input clearly
            // indicates a website builder / SaaS, keep the original industry to prevent bad research.
            $in = strtolower((string)($industry ?? ''));
            $out = strtolower((string)($structured_data['industry_confirmation'] ?? ''));
            $signals = strtolower(
                (string)($business_data['business_name'] ?? '') . ' ' .
                (string)($business_data['offerings'] ?? '') . ' ' .
                (string)($business_data['services'] ?? '') . ' ' .
                (string)($business_data['blueprint_summary'] ?? '')
            );
            $looks_like_builder = (
                strpos($signals, 'website builder') !== false ||
                strpos($signals, 'web builder') !== false ||
                strpos($signals, 'site builder') !== false ||
                strpos($signals, 'ai website') !== false ||
                strpos($signals, 'wordpress') !== false ||
                strpos($signals, 'wix') !== false ||
                strpos($signals, 'squarespace') !== false ||
                strpos($signals, 'shopify') !== false
            );
            $looks_like_business_formation = (
                strpos($out, 'registered agent') !== false ||
                strpos($out, 'business formation') !== false ||
                strpos($out, 'llc') !== false ||
                strpos($out, 'incorporation') !== false
            );
            if ($looks_like_builder && $looks_like_business_formation && !empty($in)) {
                $structured_data['industry_confirmation'] = $industry;
            }

            $structured_data['raw_response'] = $response;
            $structured_data['timestamp'] = current_time('mysql');
            $structured_data['category'] = $category;
            return $structured_data;
        }
        
        // Fallback if JSON parsing fails
        return [
            'industry_confirmation' => $industry,
            'global_market_size' => 'Data not available',
            'category' => $category,
            'raw_response' => $response,
            'timestamp' => current_time('mysql')
        ];
    }
    
    /**
     * ✅ UNIVERSAL: Build market intelligence prompt that works for ANY business type
     */
    private function build_universal_market_intelligence_prompt($business_data, $competitor_context, $category) {
        $industry = $business_data['industry'] ?? '';
        $business_type = $business_data['business_type'] ?? '';
        $location = $business_data['location'] ?? '';
        $offerings = $business_data['offerings'] ?? '';
        
        return "As a market intelligence analyst, provide STRUCTURED market data for this SPECIFIC business/industry.

═══════════════════════════════════════════════════════════════
BUSINESS BEING ANALYZED:
═══════════════════════════════════════════════════════════════
- Industry/Category: {$industry}
- Business Type: {$business_type}
- Location: {$location}
- Offerings: {$offerings}
- Known Competitors: {$competitor_context}

IMPORTANT: You MUST respond with valid JSON data. Do not include any markdown formatting or code blocks.

Return the following JSON structure with REAL estimated values based on publicly available industry data:

{
    \"industry_confirmation\": \"[Confirm the specific industry/niche, e.g., 'Website Builder Software' or 'Fast Casual Mexican Restaurant']\",
    \"global_market_size\": \"[Global market value for THIS specific industry with currency, e.g., '\$8.5 Billion']\",
    \"global_market_year\": \"[Year of the estimate, e.g., '2024']\",
    \"local_market_size\": \"[Local/regional market value for {$location} if applicable, e.g., '\$78 Million']\",
    \"local_market_year\": \"[Year of the local estimate]\",
    \"market_growth_rate\": \"[Annual growth rate/CAGR, e.g., '8.5%']\",
    \"market_projection\": \"[Projected market size and year, e.g., '\$15 Billion by 2030']\",
    \"market_position\": {
        \"current_status\": \"[Assess the client's current market position honestly — e.g., 'No established market presence yet' or 'Early-stage entrant' or 'Growing regional player']\",
        \"potential_position\": \"[Expected position if the strategic plan is executed ideally — e.g., 'Disruptive challenger in the SMB segment']\",
        \"short_term_6mo\": \"[Realistic position after 6 months of executing recommendations — market share %, revenue target, brand awareness level]\",
        \"mid_term_2_3yr\": \"[Position after 2-3 years — market share %, revenue, competitive standing vs named competitors]\",
        \"long_term_5yr_plus\": \"[Position after 5+ years — market leadership potential, revenue, market share vs top players]\"
    },
    \"top_competitors\": [
        {
            \"name\": \"[Competitor Name - must be a REAL company]\",
            \"competitor_type\": \"direct\",
            \"description\": \"[What they offer]\",
            \"annual_revenue\": \"[Estimated annual revenue, e.g., '\$1.6 Billion']\",
            \"valuation\": \"[Company valuation or net worth if known, e.g., '\$12 Billion']\",
            \"market_position\": \"[Market leader / Major player / Challenger / Regional player]\",
            \"market_share\": \"[Market share percentage, e.g., '15%']\",
            \"key_strength\": \"[Their competitive advantage]\",
            \"weakness\": \"[A specific weakness or gap the client can exploit]\",
            \"how_to_beat\": \"[Specific strategy to outperform this competitor based on the client's offerings]\"
        }
    ],
    \"indirect_competitors\": [
        {
            \"name\": \"[Indirect/secondary competitor name — companies in adjacent markets that compete for the same customers]\",
            \"competitor_type\": \"indirect\",
            \"description\": \"[What they offer and WHY they are indirect competition]\",
            \"annual_revenue\": \"[Estimated annual revenue if known]\",
            \"valuation\": \"[Company valuation if known]\",
            \"threat_level\": \"[high/medium/low — how much do they threaten the client?]\",
            \"overlap\": \"[Where their offerings overlap with the client's]\",
            \"vulnerability\": \"[Why the client's bundled/better offering could pull their customers]\"
        }
    ],
    \"target_customer_segments\": [
        {
            \"segment\": \"[Customer segment, e.g., 'Small Business Owners']\",
            \"size\": \"[Segment size, e.g., '33 million in US']\",
            \"avg_deal_value\": \"[Average purchase/contract value, e.g., '\$500/project']\",
            \"acquisition_channel\": \"[Best way to reach them]\"
        }
    ],
    \"ideal_target_market\": {
        \"primary\": \"[Primary target customer profile - be specific]\",
        \"secondary\": \"[Secondary target customer profile]\",
        \"bulk_buyers\": \"[B2B or volume buyers if applicable]\",
        \"underserved_segments\": \"[Underserved market segments with opportunity]\"
    }
}

REQUIREMENTS:
- Include 4-6 DIRECT competitors (companies offering the SAME products/services) with competitor_type='direct'
- Include 2-4 INDIRECT competitors (adjacent markets that compete for same customers) with competitor_type='indirect'
- For EVERY competitor: include annual_revenue, valuation, market_share, weakness, and how_to_beat
- The market_position object MUST be filled out completely — short_term, mid_term, and long_term projections are MANDATORY
- Include 3-4 target customer segments specific to THIS industry
- Use REAL company names and realistic estimated figures from public sources
- All monetary values should include currency symbols (use USD)
- Be specific to the EXACT industry — don't give generic data

Respond with ONLY the JSON object, no additional text or formatting.";
    }
    
    /**
     * ✅ Helper: Parse JSON from AI response (handles markdown code blocks)
     */
    private function parse_json_from_response($response) {
        // First, try direct JSON parse
        $decoded = json_decode($response, true);
        if ($decoded && is_array($decoded)) {
            return $decoded;
        }
        
        // Try to extract JSON from markdown code block
        if (preg_match('/```(?:json)?\s*([\s\S]*?)```/', $response, $matches)) {
            $decoded = json_decode(trim($matches[1]), true);
            if ($decoded && is_array($decoded)) {
                return $decoded;
            }
        }
        
        // Try to find JSON object in response
        if (preg_match('/\{[\s\S]*\}/', $response, $matches)) {
            $decoded = json_decode($matches[0], true);
            if ($decoded && is_array($decoded)) {
                return $decoded;
            }
        }
        
        return null;
    }
    
    /**
     * ✅ Analyze industry insights - overview of the industry landscape
     */
    private function analyze_industry($business_data) {
        $industry = $business_data['industry'] ?? 'general';
        $business_type = $business_data['business_type'] ?? 'service';
        $location = $business_data['location'] ?? '';
        $offerings = $business_data['offerings'] ?? $business_data['services'] ?? '';
        
        $prompt = "As an industry analyst, provide comprehensive industry insights:

BUSINESS CONTEXT:
- Industry: {$industry}
- Business Type: {$business_type}
- Location: {$location}
- Offerings: {$offerings}

INDUSTRY ANALYSIS:

1. **Industry Overview**:
   - Industry definition and scope
   - Key players and market structure
   - Industry maturity stage (emerging, growing, mature, declining)
   - Total addressable market (TAM)

2. **Industry Dynamics**:
   - Porter's Five Forces summary
   - Entry barriers
   - Supplier and buyer power
   - Threat of substitutes
   - Competitive rivalry intensity

3. **Success Factors**:
   - What differentiates winners from losers in this industry
   - Key capabilities required
   - Common mistakes to avoid
   - Best practices from industry leaders

4. **Industry Challenges**:
   - Current pain points affecting businesses
   - Talent and labor challenges
   - Supply chain issues
   - Technology disruption threats

5. **Opportunities & White Space**:
   - Underserved market segments
   - Emerging niches
   - Innovation opportunities
   - Partnership possibilities

6. **Industry Outlook**:
   - Short-term outlook (6-12 months)
   - Medium-term trends (1-3 years)
   - Long-term transformation (3-5 years)
   - Key uncertainties and wildcards

Provide actionable insights that can inform business strategy and positioning.";

        $analysis = $this->call_openai_analysis($prompt, 3000);
        
        return [
            'analysis' => $analysis,
            'industry_stage' => $this->extract_industry_stage($analysis),
            'success_factors' => $this->extract_success_factors($analysis),
            'timestamp' => current_time('mysql')
        ];
    }
    
    /**
     * Extract industry stage from analysis
     */
    private function extract_industry_stage($analysis) {
        if (stripos($analysis, 'emerging') !== false) return 'emerging';
        if (stripos($analysis, 'growing') !== false || stripos($analysis, 'growth') !== false) return 'growing';
        if (stripos($analysis, 'mature') !== false) return 'mature';
        if (stripos($analysis, 'declining') !== false) return 'declining';
        return 'unknown';
    }
    
    /**
     * Extract success factors from analysis
     */
    private function extract_success_factors($analysis) {
        preg_match_all('/[-•]\s+(.+?)(?:\n|$)/m', $analysis, $matches);
        return array_slice($matches[1] ?? [], 0, 10);
    }
    
    /**
     * Conduct keyword research for SEO
     */
    private function conduct_keyword_research($business_data) {
        $business_type = $business_data['business_type'] ?? '';
        $services = $business_data['services'] ?? '';
        $location = $business_data['location'] ?? '';

        $live_evidence = $this->build_live_search_evidence(array(
            trim($services . ' ' . $location),
            trim($business_type . ' ' . $location),
            trim($services . ' near me'),
        ), array(
            'location' => $location,
            'limit' => 4,
            'page_scan_limit' => 1,
            'max_results_per_query' => 3,
        ));
        
        $prompt = "As an SEO specialist, conduct comprehensive keyword research:

BUSINESS CONTEXT:
- Business Type: {$business_type}
- Services: {$services}
- Location: {$location}

KEYWORD RESEARCH TASKS:

1. **Primary Keywords** (High-volume, high-intent):
   Identify 10-15 primary keywords that:
   - Have significant search volume
   - Indicate strong purchase/hire intent
   - Are achievable for ranking (moderate competition)
   - Include the business type and location
   
   For each keyword provide:
   - Estimated monthly searches
   - Competition level (High/Medium/Low)
   - Commercial intent (High/Medium/Low)
   - Ranking difficulty

2. **Secondary Keywords** (Supporting content):
   Identify 20-30 secondary keywords for blog content:
   - Question-based keywords (How to...)
   - Problem-solution keywords
   - Comparison keywords (vs, best, top)
   - Educational keywords
   
3. **Long-tail Keywords** (Low competition, specific):
   Identify 15-20 long-tail keywords:
   - Very specific service + location combinations
   - Problem-specific phrases
   - Low competition opportunities
   - Voice search phrases

4. **Local SEO Keywords**:
   Location-specific keyword variations:
   - [Service] in [City]
   - [Service] near me
   - [Service] [Neighborhood]
   - Best [Service] [Location]

5. **Semantic & Related Terms**:
   Terms to include naturally in content:
   - Industry terminology
   - Related services
   - Complementary topics
   - LSI keywords

6. **Content Cluster Recommendations**:
   Suggest 5-7 content pillar topics with supporting subtopics that:
   - Establish topical authority
   - Cover the full customer journey
   - Address common questions and concerns
   - Support conversion goals

Provide specific, actionable keyword recommendations with estimated metrics.

LIVE SERP EVIDENCE (USE THIS CURRENT SEARCH DATA TO VALIDATE YOUR RECOMMENDATIONS):
{$live_evidence['text']}";

        $keywords = $this->call_openai_analysis($prompt, 3500);
        
        return [
            'analysis' => $keywords,
            'primary_keywords' => $this->extract_primary_keywords($keywords),
            'content_topics' => $this->extract_content_topics($keywords),
            'timestamp' => current_time('mysql')
        ];
    }
    
    /**
     * Gather industry-specific insights
     */
    private function gather_industry_insights($business_data) {
        $industry = $business_data['industry'] ?? '';
        $business_type = $business_data['business_type'] ?? '';
        
        $prompt = "As an industry expert, provide deep insights:

BUSINESS CONTEXT:
- Industry: {$industry}
- Business Type: {$business_type}

INDUSTRY INSIGHTS:

1. **Industry Best Practices**:
   - Standards and benchmarks
   - Common mistakes to avoid
   - Success factors
   - Quality indicators customers look for

2. **Customer Journey in This Industry**:
   - How customers discover providers
   - Research and evaluation process
   - Decision-making factors
   - Post-purchase expectations
   - Loyalty drivers

3. **Trust & Credibility Signals**:
   - Certifications and credentials valued
   - Social proof that matters
   - Testimonial strategies
   - Portfolio/work sample expectations
   - Guarantees and warranties

4. **Pricing Psychology**:
   - How customers evaluate value
   - Pricing presentation strategies
   - Package vs. hourly preferences
   - Discount expectations
   - Payment term norms

5. **Customer Service Expectations**:
   - Response time standards
   - Communication channel preferences
   - Support availability expectations
   - Problem resolution approaches

6. **Industry Challenges & Pain Points**:
   - Common customer complaints
   - Service delivery challenges
   - Market saturation issues
   - Differentiation difficulties

Provide specific, actionable insights for this industry.";

        $insights = $this->call_openai_analysis($prompt, 3000);
        
        return [
            'analysis' => $insights,
            'best_practices' => $this->extract_best_practices($insights),
            'trust_signals' => $this->extract_trust_signals($insights),
            'timestamp' => current_time('mysql')
        ];
    }
    
    /**
     * Analyze target audience in detail
     */
    private function analyze_target_audience($business_data) {
        $target_market = $business_data['target_market'] ?? '';
        $customer_problems = $business_data['customer_problems'] ?? '';
        
        $prompt = "As a customer research specialist, analyze the target audience:

BUSINESS CONTEXT:
- Target Market: {$target_market}
- Problems Solved: {$customer_problems}

TARGET AUDIENCE ANALYSIS:

1. **Demographic Profile**:
   - Age ranges and predominant groups
   - Income levels and spending capacity
   - Education and professional background
   - Geographic distribution
   - Family status and household composition

2. **Psychographic Profile**:
   - Values and priorities
   - Lifestyle characteristics
   - Attitudes and opinions
   - Interests and hobbies
   - Media consumption habits

3. **Online Behavior**:
   - Preferred devices (mobile, desktop, tablet)
   - Social media platforms used
   - Search behavior patterns
   - Content consumption preferences
   - Online shopping habits

4. **Pain Points & Motivations**:
   - Primary problems seeking to solve
   - Emotional drivers
   - Rational decision factors
   - Barriers to purchase
   - Objections to overcome

5. **Decision-Making Process**:
   - Research phase behaviors
   - Information sources consulted
   - Comparison criteria
   - Influencers and stakeholders
   - Time to decision

6. **Communication Preferences**:
   - Language style (formal/casual)
   - Tone preferences
   - Technical vs. simple explanations
   - Visual vs. text preferences
   - Humor and personality expectations

Provide detailed customer personas and targeting recommendations.";

        $audience_analysis = $this->call_openai_analysis($prompt, 3000);
        
        return [
            'analysis' => $audience_analysis,
            'personas' => $this->extract_personas($audience_analysis),
            'messaging_strategy' => $this->extract_messaging_strategy($audience_analysis),
            'timestamp' => current_time('mysql')
        ];
    }
    
    /**
     * Identify business opportunities
     */
    private function identify_opportunities($business_data) {
        $business_type = $business_data['business_type'] ?? '';
        $industry = $business_data['industry'] ?? '';
        
        $prompt = "Identify strategic business opportunities:

CONTEXT:
- Business: {$business_type}
- Industry: {$industry}

OPPORTUNITY ANALYSIS:

1. **Market Gaps**: Unmet needs in the market
2. **Emerging Niches**: New specialized service areas
3. **Technology Opportunities**: Digital tools to leverage
4. **Partnership Possibilities**: Strategic collaborations
5. **Expansion Opportunities**: Geographic or service expansion
6. **Revenue Streams**: Additional monetization options
7. **Competitive Advantages**: Unique positioning possibilities

Provide 5-10 specific, actionable opportunities.";

        $opportunities = $this->call_openai_analysis($prompt, 2000);
        
        return [
            'analysis' => $opportunities,
            'top_opportunities' => $this->extract_opportunities($opportunities),
            'timestamp' => current_time('mysql')
        ];
    }
    
    /**
     * Identify threats and challenges
     */
    private function identify_threats($business_data) {
        $business_type = $business_data['business_type'] ?? '';
        $industry = $business_data['industry'] ?? '';
        
        $prompt = "Identify potential threats and challenges:

CONTEXT:
- Business: {$business_type}
- Industry: {$industry}

THREAT ANALYSIS:

1. **Competitive Threats**: Established and emerging competitors
2. **Market Challenges**: Saturation, commoditization, price pressure
3. **Technology Disruption**: Automation, AI, new platforms
4. **Economic Factors**: Recession risks, inflation, spending changes
5. **Regulatory Risks**: Compliance, licensing, legal changes
6. **Reputational Risks**: Review management, social media crises
7. **Operational Challenges**: Scaling, quality control, staffing

Provide specific threats with mitigation strategies.";

        $threats = $this->call_openai_analysis($prompt, 2000);
        
        return [
            'analysis' => $threats,
            'critical_threats' => $this->extract_threats($threats),
            'mitigation_strategies' => $this->extract_mitigation_strategies($threats),
            'timestamp' => current_time('mysql')
        ];
    }
    
    /**
     * Analyze pricing strategy
     */
    private function analyze_pricing_strategy($business_data) {
        $services = $business_data['services'] ?? '';
        $business_type = $business_data['business_type'] ?? '';
        
        $prompt = "Analyze optimal pricing strategy:

CONTEXT:
- Business Type: {$business_type}
- Services: {$services}

PRICING ANALYSIS:

1. **Market Rate Benchmarks**: Industry standard pricing
2. **Value-Based Pricing**: Premium vs. budget positioning
3. **Pricing Models**: Hourly, project, subscription, package
4. **Psychological Pricing**: Price points and presentation
5. **Competitive Positioning**: Premium, mid-tier, or value leader
6. **Discount Strategies**: When and how to discount
7. **Upsell Opportunities**: Additional services and packages

Provide specific pricing recommendations with rationale.";

        $pricing = $this->call_openai_analysis($prompt, 2000);
        
        return [
            'analysis' => $pricing,
            'recommended_pricing' => $this->extract_pricing_recommendations($pricing),
            'timestamp' => current_time('mysql')
        ];
    }
    
    /**
     * ✅ Identify potential leads for user's CRM
     * This is a wrapper for generate_potential_leads used in safe_analysis()
     */
    private function identify_potential_leads($business_data) {
        return $this->generate_potential_leads($business_data);
    }
    
    /**
     * Generate comprehensive research summary
     */
    private function generate_research_summary($research_results, $business_data) {
        $business_name = $business_data['business_name'] ?? 'Your Business';
        
        $prompt = "Create an executive summary of this business research:

BUSINESS: {$business_name}

RESEARCH FINDINGS:
{$research_results['competitor_analysis']['analysis']}

{$research_results['market_trends']['analysis']}

{$research_results['opportunities']['analysis']}

TASK: Create a compelling 3-paragraph executive summary that:
1. Summarizes the market opportunity and landscape
2. Highlights the most critical insights and opportunities
3. Provides clear strategic recommendations

Keep it concise, actionable, and inspiring.";

        return $this->call_openai_analysis($prompt, 1000, 'pro');
    }
    
    /**
     * Safe Analysis Wrapper - Wraps individual analysis calls in try-catch
     * to prevent one failure from stopping all research
     */
    private function safe_analysis($type, $business_data) {
        try {
            error_log("🔍 VMO Research: Starting analysis for '{$type}'...");
            
            switch ($type) {
                case 'vision_and_intent':
                    return $this->analyze_vision_and_intent($business_data);
                    
                case 'business_structure':
                    return $this->analyze_business_structure($business_data);
                    
                case 'target_market_intelligence':
                    return $this->analyze_target_market_intelligence($business_data);
                    
                case 'competitors':
                    return $this->analyze_competitors($business_data);
                    
                case 'market_trends':
                    return $this->analyze_market_trends($business_data);
                    
                case 'operations_and_delivery':
                    return $this->analyze_operations_and_delivery($business_data);
                    
                case 'brand_positioning':
                    return $this->analyze_brand_positioning($business_data);
                    
                case 'marketing_strategy':
                    return $this->analyze_marketing_strategy($business_data);
                    
                case 'keyword_research':
                    return $this->conduct_keyword_research($business_data);
                    
                case 'legal_compliance':
                    return $this->analyze_legal_compliance($business_data);
                    
                case 'technology_stack':
                    return $this->analyze_technology_stack($business_data);
                    
                case 'success_metrics':
                    return $this->analyze_success_metrics($business_data);
                    
                case 'industry_insights':
                    return $this->analyze_industry($business_data);
                    
                case 'target_audience':
                    return $this->analyze_target_audience($business_data);
                    
                case 'opportunities':
                    return $this->identify_opportunities($business_data);
                    
                case 'threats':
                    return $this->identify_threats($business_data);
                    
                case 'pricing_strategy':
                    return $this->analyze_pricing_strategy($business_data);
                    
                case 'potential_leads':
                    return $this->identify_potential_leads($business_data);
                    
                default:
                    error_log("⚠️ VMO Research: Unknown analysis type '{$type}'");
                    return ['analysis' => 'Analysis type not recognized', 'error' => true];
            }
        } catch (Exception $e) {
            error_log("❌ VMO Research Error ({$type}): " . $e->getMessage());
            return [
                'analysis' => 'Analysis temporarily unavailable',
                'error' => true,
                'error_message' => $e->getMessage(),
                'timestamp' => current_time('mysql')
            ];
        }
    }
    
    /**
     * Call OpenAI for analysis
     * 
     * ✅ OPTIMIZED FOR GPT-5.5 WITH FALLBACK:
     * - Higher token allocation (4000) for comprehensive analysis
     * - Lower temperature (0.4) for faster, more focused responses
     * - Extended timeout (180s) for complex reasoning
     * - Auto-fallback to nano tier if primary model fails or returns empty
     */
    private function call_openai_analysis($prompt, $max_tokens = 4000, $model_tier = 'strategic') {
        if (empty($this->api_key)) {
            error_log('❌ VMO Deep Research: OpenAI API key not configured');
            return 'Research analysis unavailable - API key not configured';
        }
        
        // ✅ TOKEN DEDUCTION FIX: In pre-paid chunked flows (wizard Phase 3),
        // skip internal per-call charges to avoid duplicate charging.
        if (!$this->skip_internal_token_deduction) {
            $user_id = get_current_user_id();
            $token_cost = min(2000, max(500, intval($max_tokens * 0.4))); // 40% of max_tokens, min 500, max 2000
            if (function_exists('vmo_deduct_tokens')) {
                if (!vmo_deduct_tokens($user_id, $token_cost, 'deep_research', 'Deep research AI analysis')) {
                    error_log('VMO Deep Research: Insufficient tokens for user ' . $user_id);
                    return 'Research analysis unavailable - Insufficient tokens. Please top up your balance.';
                }
            }
        }
        
        // ✅ Use centralized model registry. Deep research runs most raw analysis on strategic,
        // then upgrades synthesis and market-positioning judgment calls to pro.
        $model_tier = is_string($model_tier) ? sanitize_key($model_tier) : 'strategic';
        if ($model_tier === 'supreme') {
            $model_tier = 'pro';
        }
        if (!in_array($model_tier, array('strategic', 'pro'), true)) {
            $model_tier = 'strategic';
        }
        if ($model_tier === 'pro') {
            $primary_model = function_exists('vmo_get_model') ? vmo_get_model('pro') : (defined('VMO_MODEL_PRO') ? VMO_MODEL_PRO : 'gpt-5.5-pro');
        } else {
            $primary_model = function_exists('vmo_get_model') ? vmo_get_model('strategic') : (defined('VMO_OPENAI_MODEL') ? VMO_OPENAI_MODEL : 'gpt-5.5');
        }
        $fallback_model = function_exists('vmo_get_model') ? vmo_get_model('fallback') : 'gpt-5.5-nano';
        
        // Try primary model first, fallback if it fails
        $models_to_try = [$primary_model];
        if ($primary_model !== $fallback_model) {
            $models_to_try[] = $fallback_model;
        }
        
        foreach ($models_to_try as $attempt => $model) {
            $is_fallback = ($attempt > 0);
            if ($is_fallback) {
                error_log('🔄 VMO Deep Research: Falling back to ' . $model . '...');
            } else {
                error_log('🔍 VMO Deep Research: Using model: ' . $model . ' with max_tokens: ' . $max_tokens);
            }
            
            // ✅ FIX: Use correct token parameter based on model
            $token_param = function_exists('vmo_get_token_param') 
                ? vmo_get_token_param($model, $max_tokens) 
                : array('max_tokens' => $max_tokens);
            
            // ✅ Lower temperature for GPT-5.x - produces faster, more consistent output
            $temperature = (strpos($model, 'gpt-5') !== false) ? 0.4 : 0.6;
            
            $data = array_merge([
                'model' => $model,
                'messages' => [
                    [
                        'role' => 'system',
                        'content' => 'You are a world-class business intelligence analyst. Provide concise, actionable insights. Be direct and structured. Use bullet points for clarity.'
                    ],
                    [
                        'role' => 'user',
                        'content' => $prompt
                    ]
                ]
            ], $token_param);

            if (function_exists('vmo_apply_safe_temperature_to_payload')) {
                $data = vmo_apply_safe_temperature_to_payload($data, $model, $temperature);
            } else {
                $data['temperature'] = $temperature;
            }
            
            // ✅ Extended timeout for deep reasoning - NEVER timeout on API calls
            // GPT-5.x/o1 models may take 5+ minutes for complex analysis
            $timeout = 600; // 10 minutes - deep research must complete
            
            $logged_temperature = array_key_exists('temperature', $data) ? $data['temperature'] : 'default';
            error_log('🔍 VMO Deep Research: Sending API request (model=' . $model . ', temp=' . $logged_temperature . ', timeout=' . $timeout . 's)...');
            
            $response = wp_remote_post('https://api.openai.com/v1/chat/completions', [
                'headers' => [
                    'Authorization' => 'Bearer ' . $this->api_key,
                    'Content-Type' => 'application/json',
                ],
                'body' => json_encode($data),
                'timeout' => $timeout
            ]);
            
            // Check for WP error
            if (is_wp_error($response)) {
                error_log('❌ VMO Deep Research API Error (' . $model . '): ' . $response->get_error_message());
                if (!$is_fallback && $model !== $fallback_model) {
                    continue; // Try fallback
                }
                return 'Analysis unavailable - API error: ' . $response->get_error_message();
            }
            
            $http_code = wp_remote_retrieve_response_code($response);
            error_log('🔍 VMO Deep Research: API response code: ' . $http_code);
            
            $response_body = wp_remote_retrieve_body($response);
            $body = json_decode($response_body, true);
            if (!is_array($body)) {
                $body = [];
            }
            
            // Check for HTTP error
            if ($http_code !== 200) {
                $error_msg = 'Unknown API error';
                if (!empty($body['error']) && is_array($body['error']) && !empty($body['error']['message'])) {
                    $error_msg = (string)$body['error']['message'];
                } elseif (is_string($response_body) && trim($response_body) !== '') {
                    $error_msg = trim(substr($response_body, 0, 300));
                }
                error_log('❌ VMO Deep Research API Error (' . $model . ', HTTP ' . $http_code . '): ' . $error_msg);
                if (!$is_fallback && $model !== $fallback_model) {
                    continue; // Try fallback
                }
                return 'Analysis unavailable - API returned HTTP ' . $http_code . ': ' . $error_msg;
            }
            
            // Check for valid content
            $content = '';
            if (
                isset($body['choices'])
                && is_array($body['choices'])
                && isset($body['choices'][0])
                && is_array($body['choices'][0])
                && isset($body['choices'][0]['message'])
                && is_array($body['choices'][0]['message'])
                && isset($body['choices'][0]['message']['content'])
            ) {
                $content = trim((string)$body['choices'][0]['message']['content']);
            }

            if ($content !== '') {
                
                // ✅ Check if content is actually meaningful (not empty or too short)
                if (strlen($content) > 50) {
                    $tokens_used = (
                        isset($body['usage'])
                        && is_array($body['usage'])
                        && isset($body['usage']['total_tokens'])
                    ) ? $body['usage']['total_tokens'] : 'N/A';
                    $model_label = $is_fallback ? $model . ' (fallback)' : $model;
                    error_log('✅ VMO Deep Research: API call successful with ' . $model_label . ' (tokens: ' . $tokens_used . ')');
                    return $content;
                } else {
                    error_log('⚠️ VMO Deep Research: ' . $model . ' returned very short content (' . strlen($content) . ' chars)');
                    if (!$is_fallback && $model !== $fallback_model) {
                        continue; // Try fallback
                    }
                }
            }
            
            // ✅ Check for empty content specifically
            if (
                isset($body['choices'])
                && is_array($body['choices'])
                && isset($body['choices'][0])
                && is_array($body['choices'][0])
                && isset($body['choices'][0]['message'])
                && is_array($body['choices'][0]['message'])
                && empty($body['choices'][0]['message']['content'])
            ) {
                error_log('⚠️ VMO Deep Research: ' . $model . ' returned empty content');
                if (!$is_fallback && $model !== $fallback_model) {
                    continue; // Try fallback
                }
                return 'Analysis incomplete - model returned empty response.';
            }
        }
        
        error_log('❌ VMO Deep Research: All models failed to produce valid output');
        return 'Analysis unavailable - all models failed';
    }
    
    /**
     * Helper functions to extract structured data from AI analysis
     */
    
    private function extract_competitor_count($analysis) {
        // Simple pattern matching to count competitors mentioned
        preg_match_all('/\d+\.\s+\*\*([^:]+)/', $analysis, $matches);
        return count($matches[1] ?? []);
    }
    
    private function extract_key_insights($analysis) {
        // Extract bullet points or numbered insights
        preg_match_all('/[-•]\s+(.+?)(?:\n|$)/m', $analysis, $matches);
        return array_slice($matches[1] ?? [], 0, 5);
    }
    
    private function extract_key_trends($analysis) {
        preg_match_all('/\d+\.\s+\*\*([^:]+):?\*\*/m', $analysis, $matches);
        return $matches[1] ?? [];
    }
    
    private function extract_opportunities_from_trends($analysis) {
        // Extract opportunity-related sentences
        $sentences = preg_split('/[.!?]+/', $analysis);
        $opportunities = [];
        foreach ($sentences as $sentence) {
            if (stripos($sentence, 'opportunity') !== false || 
                stripos($sentence, 'potential') !== false ||
                stripos($sentence, 'growth') !== false) {
                $opportunities[] = trim($sentence);
                if (count($opportunities) >= 3) break;
            }
        }
        return $opportunities;
    }
    
    private function extract_primary_keywords($analysis) {
        // Extract keywords from markdown lists
        preg_match_all('/[-•]\s+(.+?)\s+\(/', $analysis, $matches);
        return array_slice($matches[1] ?? [], 0, 10);
    }
    
    private function extract_content_topics($analysis) {
        preg_match_all('/\d+\.\s+([^:\n]+)/', $analysis, $matches);
        return array_slice($matches[1] ?? [], 0, 7);
    }
    
    private function extract_best_practices($analysis) {
        preg_match_all('/[-•]\s+(.+?)(?:\n|$)/m', $analysis, $matches);
        return array_slice($matches[1] ?? [], 0, 5);
    }
    
    private function extract_trust_signals($analysis) {
        $signals = [];
        if (preg_match('/certifications?/i', $analysis)) $signals[] = 'Professional Certifications';
        if (preg_match('/testimonial/i', $analysis)) $signals[] = 'Customer Testimonials';
        if (preg_match('/review/i', $analysis)) $signals[] = 'Reviews & Ratings';
        if (preg_match('/portfolio/i', $analysis)) $signals[] = 'Portfolio/Case Studies';
        if (preg_match('/guarantee/i', $analysis)) $signals[] = 'Service Guarantee';
        return $signals;
    }
    
    private function extract_personas($analysis) {
        // Basic extraction - could be enhanced
        return ['Primary Persona' => 'Extracted from analysis'];
    }
    
    private function extract_messaging_strategy($analysis) {
        return 'Professional, solution-focused communication';
    }
    
    private function extract_opportunities($analysis) {
        preg_match_all('/\d+\.\s+\*\*([^:]+)/', $analysis, $matches);
        return array_slice($matches[1] ?? [], 0, 7);
    }
    
    private function extract_threats($analysis) {
        preg_match_all('/\d+\.\s+\*\*([^:]+)/', $analysis, $matches);
        return array_slice($matches[1] ?? [], 0, 5);
    }
    
    private function extract_mitigation_strategies($analysis) {
        return ['Regular monitoring', 'Competitive differentiation', 'Customer retention focus'];
    }
    
    private function extract_pricing_recommendations($analysis) {
        return ['Competitive pricing with premium positioning', 'Value-based packages'];
    }
    
    /**
     * Store research results in database
     */
    private function store_research_results($user_id, $research_results) {
        global $wpdb;
        $table = $wpdb->prefix . 'vmo_deep_research';
        
        // Create table if it doesn't exist
        $charset_collate = $wpdb->get_charset_collate();
        $sql = "CREATE TABLE IF NOT EXISTS {$table} (
            id bigint(20) NOT NULL AUTO_INCREMENT,
            user_id bigint(20) NOT NULL,
            research_data longtext NOT NULL,
            created_at datetime DEFAULT CURRENT_TIMESTAMP,
            updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            PRIMARY KEY  (id),
            KEY user_id (user_id)
        ) {$charset_collate};";
        
        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
        dbDelta($sql);
        
        // Store or update research
        $existing = $wpdb->get_var($wpdb->prepare(
            "SELECT id FROM {$table} WHERE user_id = %d ORDER BY id DESC LIMIT 1",
            $user_id
        ));
        
        if ($existing) {
            // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
            $wpdb->update(
                $table,
                ['research_data' => json_encode($research_results)],
                ['id' => $existing],
                ['%s'],
                ['%d']
            );
        } else {
            // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
            $wpdb->insert(
                $table,
                [
                    'user_id' => $user_id,
                    'research_data' => json_encode($research_results)
                ],
                ['%d', '%s']
            );
        }
        
        error_log('✅ VMO Deep Research: Results stored in database');
    }
    
    /**
     * Get stored research results
     */
    public function get_research_results($user_id) {
        global $wpdb;
        $table = $wpdb->prefix . 'vmo_deep_research';
        
        $row = $wpdb->get_row($wpdb->prepare(
            "SELECT research_data, created_at FROM {$table} WHERE user_id = %d ORDER BY id DESC LIMIT 1",
            $user_id
        ));
        
        if ($row) {
            return [
                'research_data' => json_decode($row->research_data, true),
                'created_at' => $row->created_at
            ];
        }
        
        return null;
    }
    
    /**
     * ✅ BULLETPROOF: Generate list of potential leads - simplified for reliability
     * Uses AI to identify companies/contacts that match target market
     * Falls back to template leads if AI fails
     */
    private function generate_potential_leads($business_data) {
        error_log('🎯 VMO Deep Research: Generating potential leads...');
        
        $business_name = $business_data['business_name'] ?? 'this business';
        $business_type = $business_data['business_type'] ?? 'business';
        $industry = $business_data['industry'] ?? '';
        $location = $business_data['location'] ?? 'United States';
        $target_market = $business_data['target_market'] ?? '';
        $offerings = $business_data['offerings'] ?? '';
        
        // Detect category for lead targeting
        $categories = $this->detect_business_categories($business_data);
        $primary_category = $categories['primary'];
        
        error_log('🎯 Lead generation: primary=' . $primary_category . ', location=' . $location);

        $live_evidence = $this->build_live_search_evidence(array(
            trim($industry . ' companies ' . $location),
            trim($target_market . ' companies ' . $location),
            trim($industry . ' association ' . $location),
        ), array(
            'location' => $location,
            'limit' => 6,
            'page_scan_limit' => 1,
            'max_results_per_query' => 3,
        ));
        
        // ✅ SIMPLIFIED: Single API call with structured JSON output request
        $prompt = $this->build_simple_leads_prompt($business_data, $primary_category, $live_evidence['text']);
        
        try {
            // Use moderate token limit for reliability
            $leads_response = $this->call_openai_analysis($prompt, 3000);
            
            error_log('🎯 Lead generation response length: ' . strlen($leads_response));
            
            // Parse the response directly - it should already be JSON
            $structured_leads = $this->parse_simple_leads_response($leads_response, $business_data);
            
            if (count($structured_leads) >= 10) {
                error_log('✅ VMO Deep Research: Generated ' . count($structured_leads) . ' leads');
                return [
                    'raw_analysis' => $leads_response,
                    'structured_leads' => $structured_leads,
                    'total_count' => count($structured_leads),
                    'generated_at' => current_time('mysql')
                ];
            }
        } catch (\Exception $e) {
            error_log('⚠️ Lead generation API error: ' . $e->getMessage());
        }
        
        error_log('⚠️ VMO Deep Research: Real potential lead generation returned no verified leads; template leads were not produced for CRM.');
        return [
            'raw_analysis' => 'Real potential lead generation needs attention. Template leads were intentionally not generated for CRM.',
            'structured_leads' => array(),
            'total_count' => 0,
            'generated_at' => current_time('mysql'),
            'is_fallback' => true,
            'save_to_crm' => false
        ];
    }
    
    /**
     * ✅ SIMPLIFIED: Build a simple, reliable lead generation prompt
     * Requests JSON directly to avoid second API call
     */
        private function build_simple_leads_prompt($business_data, $primary_category, $live_evidence = '') {
        $business_name = $business_data['business_name'] ?? 'this business';
        $business_type = $business_data['business_type'] ?? 'business';
        $industry = $business_data['industry'] ?? '';
        $location = $business_data['location'] ?? 'United States';
        $target_market = $business_data['target_market'] ?? '';
        
        $city = $this->extract_city($location);
        $state = $this->extract_state($location);
        
                return "Generate up to 25 real B2B leads for this business using the live evidence provided below. Return ONLY a valid JSON array.

BUSINESS: {$business_name}
TYPE: {$business_type}
INDUSTRY: {$industry}
PRIMARY CATEGORY: {$primary_category}
LOCATION: {$city}, {$state}
TARGET MARKET: {$target_market}

LIVE SEARCH EVIDENCE:
{$live_evidence}

Return exactly this JSON structure (no markdown, no explanation):
[
  {
    \"company_name\": \"\",
    \"contact_person\": \"\",
    \"job_title\": \"\",
        \"email\": \"\",
        \"phone\": \"\",
        \"website\": \"\",
    \"city\": \"{$city}\",
    \"state\": \"{$state}\",
    \"industry\": \"\",
    \"why_ideal\": \"\",
    \"lead_score\": 75,
        \"priority_level\": \"medium\",
        \"evidence_source\": \"\"
  }
]

Rules:
- Prefer 25 leads when the evidence supports it.
- Use ONLY real organizations or institutions supported by the live evidence.
- Leave email and phone blank unless they are explicitly present in the evidence.
- Include website URLs when available.
- Vary the lead_score (60-95) and priority_level (high/medium/low).
- Do not invent fake companies, fake people, or fake contact details.";
    }
    
    /**
     * ✅ SIMPLIFIED: Parse leads response with robust fallback
     */
    private function parse_simple_leads_response($response, $business_data) {
        $leads = [];
        
        if (empty($response)) {
            return $leads;
        }
        
        // Clean the response
        $json_clean = $response;
        $json_clean = preg_replace('/```json\s*|\s*```/', '', $json_clean);
        $json_clean = preg_replace('/```\s*|\s*```/', '', $json_clean);
        $json_clean = trim($json_clean);
        
        // Find JSON array
        if (preg_match('/\[[\s\S]*\]/', $json_clean, $matches)) {
            $json_clean = $matches[0];
        }
        
        // Parse JSON
        $parsed = json_decode($json_clean, true);
        
        if (!is_array($parsed) || json_last_error() !== JSON_ERROR_NONE) {
            error_log('⚠️ Lead JSON parse error: ' . json_last_error_msg());
            return $leads;
        }
        
        foreach ($parsed as $lead) {
            $company_name = sanitize_text_field($lead['company_name'] ?? $lead['company'] ?? '');
            $contact_person = sanitize_text_field($lead['contact_person'] ?? '');
            
            if (empty($company_name)) {
                continue;
            }
            
            $leads[] = [
                'full_name' => $contact_person ?: $company_name,
                'first_name' => sanitize_text_field(explode(' ', $contact_person)[0] ?? ''),
                'last_name' => sanitize_text_field(implode(' ', array_slice(explode(' ', $contact_person), 1)) ?: ''),
                'company' => $company_name,
                'job_title' => sanitize_text_field($lead['job_title'] ?? ''),
                'email' => sanitize_email($lead['email'] ?? ''),
                'phone' => sanitize_text_field($lead['phone'] ?? ''),
                'website' => esc_url_raw($lead['website'] ?? ''),
                'source_url' => esc_url_raw($lead['evidence_source'] ?? ''),
                'city' => sanitize_text_field($lead['city'] ?? $business_data['location'] ?? ''),
                'state' => sanitize_text_field($lead['state'] ?? ''),
                'country' => 'USA',
                'contact_type' => 'B2B',
                'primary_category' => sanitize_text_field($lead['industry'] ?? 'General'),
                'why_ideal' => sanitize_textarea_field($lead['why_ideal'] ?? ''),
                'lead_score' => (int) ($lead['lead_score'] ?? 70),
                'priority_level' => sanitize_text_field($lead['priority_level'] ?? 'medium'),
                'value_potential' => $this->convert_value_to_score($lead['priority_level'] ?? 'medium'),
                'lifecycle_stage' => 'new',
                'engagement_level' => 'medium',
                'source' => 'lead_generation'
            ];
        }
        
        error_log('✅ Parsed ' . count($leads) . ' leads from AI response');
        return $leads;
    }
    
    /**
     * Generate fallback lead suggestions when AI generation fails.
     * Returns template descriptions of ideal prospect TYPES (not fake people).
     * These are clearly marked as template suggestions, not real contacts.
     */
    private function generate_fallback_leads($business_data) {
        error_log('🔄 Generating fallback lead suggestions (templates, not real contacts)...');
        
        $location = $business_data['location'] ?? 'United States';
        $industry = $business_data['industry'] ?? 'general';
        $target_market = $business_data['target_market'] ?? '';
        $city = $this->extract_city($location);
        $state = $this->extract_state($location);
        
        // Return prospect TYPE templates — no fake names, emails, or phone numbers
        $fallback_leads = [
            [
                'full_name' => '[Local Business Owner]',
                'company' => 'Local ' . ucfirst($industry) . ' Business',
                'job_title' => 'Owner / Founder',
                'email' => '',
                'phone' => '',
                'city' => $city,
                'state' => $state,
                'contact_type' => 'B2B',
                'primary_category' => $industry,
                'why_ideal' => 'Small local business actively growing and looking for solutions. Search local directories and Google Maps for "' . $industry . ' near ' . $city . '" to find real prospects.',
                'lead_score' => 65,
                'priority_level' => 'medium',
                'lead_temperature' => 'Warm',
                'source' => 'template_suggestion'
            ],
            [
                'full_name' => '[Regional Company Executive]',
                'company' => 'Regional ' . ucfirst($industry) . ' Company',
                'job_title' => 'VP of Operations',
                'email' => '',
                'phone' => '',
                'city' => $city,
                'state' => $state,
                'contact_type' => 'B2B',
                'primary_category' => $industry,
                'why_ideal' => 'Mid-sized company with budget for solutions. Search LinkedIn for "' . $industry . ' ' . ($state ?: $city) . '" to find real decision-makers.',
                'lead_score' => 70,
                'priority_level' => 'medium',
                'lead_temperature' => 'Warm',
                'source' => 'template_suggestion'
            ],
            [
                'full_name' => '[Startup Founder]',
                'company' => ucfirst($industry) . ' Startup',
                'job_title' => 'CEO & Founder',
                'email' => '',
                'phone' => '',
                'city' => $city,
                'state' => $state,
                'contact_type' => 'B2B',
                'primary_category' => $industry,
                'why_ideal' => 'Fast-growing startup actively adopting new tools. Check AngelList, Crunchbase, or local incubators for real prospects.',
                'lead_score' => 80,
                'priority_level' => 'high',
                'lead_temperature' => 'Hot',
                'source' => 'template_suggestion'
            ],
            [
                'full_name' => '[Industry Association Contact]',
                'company' => $city . ' ' . ucfirst($industry) . ' Association',
                'job_title' => 'Membership Director',
                'email' => '',
                'phone' => '',
                'city' => $city,
                'state' => $state,
                'contact_type' => 'B2B',
                'primary_category' => $industry,
                'why_ideal' => 'Professional associations provide access to many member businesses. Search for "' . $industry . ' association ' . ($state ?: 'near me') . '" to find real organizations.',
                'lead_score' => 75,
                'priority_level' => 'high',
                'lead_temperature' => 'Hot',
                'source' => 'template_suggestion'
            ],
            [
                'full_name' => '[Chamber of Commerce Contact]',
                'company' => $city . ' Chamber of Commerce',
                'job_title' => 'Business Development',
                'email' => '',
                'phone' => '',
                'city' => $city,
                'state' => $state,
                'contact_type' => 'B2B',
                'primary_category' => $industry,
                'why_ideal' => 'Chambers of Commerce provide direct access to local businesses. Visit your local chamber website for real contact information.',
                'lead_score' => 72,
                'priority_level' => 'medium',
                'lead_temperature' => 'Warm',
                'source' => 'template_suggestion'
            ],
        ];
        
        error_log('✅ Generated ' . count($fallback_leads) . ' template lead suggestions');
        
        return $fallback_leads;
    }
    
    /**
     * Get area code for location
     */
    private function get_area_code_for_location($location) {
        $area_codes = [
            'atlanta' => '404', 'georgia' => '404',
            'new york' => '212', 'nyc' => '212',
            'los angeles' => '310', 'la' => '310', 'california' => '415',
            'chicago' => '312', 'illinois' => '312',
            'houston' => '713', 'texas' => '214',
            'phoenix' => '602', 'arizona' => '602',
            'philadelphia' => '215', 'pennsylvania' => '215',
            'san antonio' => '210', 'dallas' => '214',
            'san diego' => '619', 'san jose' => '408',
            'austin' => '512', 'jacksonville' => '904',
            'san francisco' => '415', 'columbus' => '614',
            'indianapolis' => '317', 'seattle' => '206',
            'denver' => '303', 'boston' => '617',
            'nashville' => '615', 'miami' => '305',
            'florida' => '305', 'washington' => '202',
            'detroit' => '313', 'memphis' => '901',
            'baltimore' => '410', 'louisville' => '502'
        ];
        
        $location_lower = strtolower($location);
        foreach ($area_codes as $city => $code) {
            if (strpos($location_lower, $city) !== false) {
                return $code;
            }
        }
        
        return ''; // No fake area code — leave blank for unknown locations
    }
    
    /**
     * Extract city from location string
     */
    private function extract_city($location) {
        $parts = explode(',', $location);
        return trim($parts[0] ?? $location);
    }
    
    /**
     * Extract state from location string
     */
    private function extract_state($location) {
        $parts = explode(',', $location);
        return trim($parts[1] ?? '');
    }
    
    /**
     * Get industry-specific fallback leads
     */
    private function get_industry_specific_fallback_leads($industry, $location, $area_code) {
        $city = $this->extract_city($location);
        $state = $this->extract_state($location);
        
        $industry_lower = strtolower($industry);
        
        if (strpos($industry_lower, 'tech') !== false || strpos($industry_lower, 'software') !== false) {
            return [
                ['full_name' => 'Alex Kumar', 'company' => 'TechStart Solutions', 'job_title' => 'CTO', 'email' => 'alex@techstartsolutions.com', 'phone' => "({$area_code}) 555-0201", 'city' => $city, 'state' => $state, 'lead_score' => 75, 'priority_level' => 'high', 'lead_temperature' => 'Hot', 'why_ideal' => 'Tech company actively adopting new tools.', 'source' => 'fallback_lead_gen'],
                ['full_name' => 'Jessica Park', 'company' => 'Digital Agency Pro', 'job_title' => 'Managing Director', 'email' => 'jessica@digitalagencypro.com', 'phone' => "({$area_code}) 555-0202", 'city' => $city, 'state' => $state, 'lead_score' => 70, 'priority_level' => 'medium', 'lead_temperature' => 'Warm', 'why_ideal' => 'Agency that could use for clients.', 'source' => 'fallback_lead_gen'],
                ['full_name' => 'Robert Lee', 'company' => 'Innovative Apps Inc', 'job_title' => 'Product Manager', 'email' => 'robert@innovativeapps.co', 'phone' => "({$area_code}) 555-0203", 'city' => $city, 'state' => $state, 'lead_score' => 65, 'priority_level' => 'medium', 'lead_temperature' => 'Warm', 'why_ideal' => 'Growing app company with expanding needs.', 'source' => 'fallback_lead_gen'],
                ['full_name' => 'Amanda Foster', 'company' => 'CloudScale Systems', 'job_title' => 'Director of Engineering', 'email' => 'amanda@cloudscalesystems.com', 'phone' => "({$area_code}) 555-0204", 'city' => $city, 'state' => $state, 'lead_score' => 72, 'priority_level' => 'medium', 'lead_temperature' => 'Warm', 'why_ideal' => 'Enterprise client with budget.', 'source' => 'fallback_lead_gen'],
                ['full_name' => 'Chris Martinez', 'company' => 'Freelance Developer Network', 'job_title' => 'Founder', 'email' => 'chris@freelancedevnetwork.com', 'phone' => "({$area_code}) 555-0205", 'city' => $city, 'state' => $state, 'lead_score' => 68, 'priority_level' => 'medium', 'lead_temperature' => 'Warm', 'why_ideal' => 'Network of developers who need tools.', 'source' => 'fallback_lead_gen']
            ];
        }
        
        // Default industry leads
        return [
            ['full_name' => 'Patricia White', 'company' => 'Local Business Association', 'job_title' => 'Executive Director', 'email' => 'patricia@localbizassoc.org', 'phone' => "({$area_code}) 555-0301", 'city' => $city, 'state' => $state, 'lead_score' => 70, 'priority_level' => 'medium', 'lead_temperature' => 'Warm', 'why_ideal' => 'Access to many local businesses.', 'source' => 'fallback_lead_gen'],
            ['full_name' => 'Thomas Brown', 'company' => 'Regional Chamber of Commerce', 'job_title' => 'Membership Director', 'email' => 'tbrown@regionalchamber.org', 'phone' => "({$area_code}) 555-0302", 'city' => $city, 'state' => $state, 'lead_score' => 75, 'priority_level' => 'high', 'lead_temperature' => 'Hot', 'why_ideal' => 'Can provide referrals to members.', 'source' => 'fallback_lead_gen'],
            ['full_name' => 'Nancy Garcia', 'company' => 'Small Business Development Center', 'job_title' => 'Business Advisor', 'email' => 'ngarcia@sbdc-region.gov', 'phone' => "({$area_code}) 555-0303", 'city' => $city, 'state' => $state, 'lead_score' => 65, 'priority_level' => 'medium', 'lead_temperature' => 'Warm', 'why_ideal' => 'Advises startups who need solutions.', 'source' => 'fallback_lead_gen'],
            ['full_name' => 'Kevin Wright', 'company' => 'Professional Services LLC', 'job_title' => 'Managing Partner', 'email' => 'kevin@professionalservices.com', 'phone' => "({$area_code}) 555-0304", 'city' => $city, 'state' => $state, 'lead_score' => 68, 'priority_level' => 'medium', 'lead_temperature' => 'Warm', 'why_ideal' => 'Established firm with recurring needs.', 'source' => 'fallback_lead_gen'],
            ['full_name' => 'Lisa Anderson', 'company' => 'Growth Consulting Group', 'job_title' => 'Senior Consultant', 'email' => 'lisa@growthconsulting.com', 'phone' => "({$area_code}) 555-0305", 'city' => $city, 'state' => $state, 'lead_score' => 72, 'priority_level' => 'high', 'lead_temperature' => 'Hot', 'why_ideal' => 'Helps clients grow - can recommend solutions.', 'source' => 'fallback_lead_gen']
        ];
    }
    
    /**
     * ✅ NEW: Convert value estimate to numeric score
     */
    private function convert_value_to_score($value_text) {
        $value_lower = strtolower(trim($value_text));
        
        if (strpos($value_lower, 'high') !== false) return 80;
        if (strpos($value_lower, 'medium') !== false || strpos($value_lower, 'mid') !== false) return 50;
        if (strpos($value_lower, 'low') !== false) return 30;
        
        return 50; // Default medium
    }
    
    /**
     * ✅ BULLETPROOF: Save generated leads to CRM database with full contact details
     * Wrapped in try-catch to never break the JSON response
     */
    private function save_leads_to_crm($user_id, $leads_data, $business_data) {
        global $wpdb;
        
        try {
            if (!function_exists('vmo_crm_upsert_contact') || !function_exists('vmo_crm_log_interaction')) {
                error_log('⚠️ VMO CRM: Central CRM helpers are unavailable');
                return 0;
            }

            $contacts_table = $wpdb->prefix . 'vmo_contacts';
            
            // Check if table exists
            $table_exists = $wpdb->get_var("SHOW TABLES LIKE '$contacts_table'");
            if (!$table_exists) {
                error_log('⚠️ VMO CRM: Contacts table does not exist yet');
                return 0;
            }
            $this->ensure_lead_tracking_columns($contacts_table);
            
            $structured_leads = $leads_data['structured_leads'] ?? [];
            $saved_count = 0;

            if (!empty($leads_data['is_template']) || !empty($leads_data['is_fallback']) || (isset($leads_data['save_to_crm']) && empty($leads_data['save_to_crm']))) {
                error_log('⚠️ VMO CRM: Template/fallback leads were not saved to CRM');
                return 0;
            }
            
            if (empty($structured_leads)) {
                error_log('⚠️ VMO CRM: No leads to save');
                return 0;
            }
            
            foreach ($structured_leads as $lead) {
                try {
                    // Ensure engagement_level is valid ENUM value
                    $engagement = strtolower($lead['engagement_level'] ?? 'medium');
                    if (!in_array($engagement, ['high', 'medium', 'low'])) {
                        $engagement = 'medium';
                    }
                    
                    // Ensure priority_level is valid ENUM value
                    $priority = strtolower($lead['priority_level'] ?? 'medium');
                    if (!in_array($priority, ['high', 'medium', 'low'])) {
                        $priority = 'medium';
                    }

                    $lead_source = sanitize_key($lead['lead_source'] ?? $lead['tier'] ?? 'tertiary');
                    if (!in_array($lead_source, array('primary', 'secondary', 'tertiary'), true)) {
                        $lead_source = 'tertiary';
                    }
                    $lead_verified = !empty($lead['lead_verified']) && (($lead['source'] ?? '') !== 'fallback_lead_gen');
                    
                    // Prepare acquisition details with full context
                    $acquisition_details = [
                        'generated_from' => 'deep_research',
                        'lead_source' => $lead_source,
                        'lead_verified' => $lead_verified,
                        'business_context' => [
                            'business_name' => $business_data['business_name'] ?? '',
                            'target_market' => $business_data['target_market'] ?? '',
                            'industry' => $business_data['industry'] ?? ''
                        ],
                        'why_ideal' => $lead['why_ideal'] ?? '',
                        'website' => $lead['website'] ?? '',
                        'source_url' => $lead['source_url'] ?? '',
                        'contact_type' => $lead['contact_type'] ?? 'B2B',
                        'generated_at' => current_time('mysql')
                    ];

                    $contact = vmo_crm_upsert_contact((int) $user_id, [
                        'first_name' => sanitize_text_field($lead['first_name'] ?? ''),
                        'last_name' => sanitize_text_field($lead['last_name'] ?? ''),
                        'full_name' => sanitize_text_field($lead['full_name'] ?? ''),
                        'email' => sanitize_email($lead['email'] ?? ''),
                        'phone' => sanitize_text_field($lead['phone'] ?? ''),
                        'company' => sanitize_text_field($lead['company'] ?? ''),
                    ], [
                        'source' => 'lead_generation',
                        'primary_category' => 'lead',
                        'secondary_category' => $lead_source,
                        'confidence_score' => min(100, max(0, (int) ($lead['lead_score'] ?? 70))),
                        'priority_level' => $priority,
                        'lifecycle_stage' => 'new',
                        'job_title' => sanitize_text_field($lead['job_title'] ?? ''),
                        'address' => sanitize_text_field($lead['address'] ?? ''),
                        'city' => sanitize_text_field($lead['city'] ?? ''),
                        'state' => sanitize_text_field($lead['state'] ?? ''),
                        'zip' => sanitize_text_field($lead['zip'] ?? ''),
                        'country' => sanitize_text_field($lead['country'] ?? 'USA'),
                        'lead_score' => min(100, max(0, (int) ($lead['lead_score'] ?? 70))),
                        'lead_source' => $lead_source,
                        'lead_verified' => $lead_verified ? 1 : 0,
                        'engagement_level' => $engagement,
                        'value_potential' => min(100, max(0, (int) ($lead['value_potential'] ?? 50))),
                        'tags' => array('deep_research', $lead_source, $lead_verified ? 'verified_lead' : 'unverified_lead'),
                        'notes' => sanitize_textarea_field(trim(($lead['why_ideal'] ?? '') . (!empty($lead['website']) ? "\nWebsite: " . $lead['website'] : ''))),
                        'acquisition_details' => $acquisition_details,
                    ]);

                    if (!empty($contact['contact_id'])) {
                        vmo_crm_log_interaction((int) $user_id, (string) $contact['contact_id'], [
                            'interaction_type' => 'automated_engagement',
                            'subtype' => 'deep_research_lead_generation',
                            'subject' => 'Deep Research lead identified',
                            'summary' => sanitize_textarea_field($lead['why_ideal'] ?? 'Deep Research identified this lead.'),
                            'outcome' => 'needs_follow_up',
                            'metadata' => $acquisition_details,
                        ]);
                        $saved_count++;
                    } else {
                        error_log('⚠️ Failed to save lead through central CRM helper');
                    }
                } catch (\Exception $e) {
                    error_log('⚠️ Error inserting individual lead: ' . $e->getMessage());
                    continue; // Skip this lead, try next
                }
            }
            
            error_log("✅ VMO Deep Research: Saved {$saved_count} of " . count($structured_leads) . " leads to CRM for user {$user_id}");
            
            return $saved_count;
            
        } catch (\Exception $e) {
            error_log('⚠️ VMO CRM save_leads_to_crm error: ' . $e->getMessage());
            return 0;
        } catch (\Throwable $e) {
            error_log('⚠️ VMO CRM save_leads_to_crm throwable: ' . $e->getMessage());
            return 0;
        }
    }

    private function ensure_lead_tracking_columns($contacts_table) {
        global $wpdb;
        $columns = $wpdb->get_col("SHOW COLUMNS FROM {$contacts_table}", 0);
        if (!is_array($columns)) {
            return;
        }
        if (!in_array('lead_source', $columns, true)) {
            $wpdb->query("ALTER TABLE {$contacts_table} ADD COLUMN lead_source ENUM('primary','secondary','tertiary') DEFAULT NULL AFTER secondary_category");
        }
        if (!in_array('lead_verified', $columns, true)) {
            $wpdb->query("ALTER TABLE {$contacts_table} ADD COLUMN lead_verified TINYINT(1) DEFAULT 0 AFTER lead_source");
        }
    }
}
