#!/usr/bin/env python3
"""
Genesis Extendly Knowledge Absorber
==================================
Systematically absorbs and synthesizes Extendly expertise.
Focuses on snapshots, white-label setup, and GHL integration.
"""

import os
import sys
import json
from datetime import datetime
from typing import List, Dict, Any

# Add genesis-system to path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

from google import genai
from google.genai import types

# Load config
CONFIG_PATH = os.path.join(os.path.dirname(__file__), "genesis_config.json")
with open(CONFIG_PATH) as f:
    CONFIG = json.load(f)

class ExtendlyKnowledgeAbsorber:
    """
    Agent responsible for systematic Extendly knowledge absorption.
    """
    
    def __init__(self):
        api_key = CONFIG["gemini"]["api_key"]
        self.client = genai.Client(api_key=api_key)
        self.kb_path = r"E:\genesis-system\knowledge-bases\extendly_mastery.json"
        os.makedirs(os.path.dirname(self.kb_path), exist_ok=True)
        
        print("[OK] Extendly Knowledge Absorber Initialized.")

    def absorb_sequence(self):
        """
        Executes the systematic absorption sequence.
        1. Extendly Snapshots (Small Business focus)
        2. Extendly OS & Setup
        3. GHL Integration Layer
        """
        print("[EXTENDLY] Beginning Systematic Absorption Sequence...")
        
        sequences = [
            {"topic": "Extendly Small Business Snapshot", "context": "Focus on content, 100+ workflows, funnels, and setup process."},
            {"topic": "Extendly OS Activation", "description": "Analyzing the activation, licensing, and white-labeling mechanics of Extendly for GHL agencies."},
            {"topic": "Extendly Snapshot Installation", "context": "Detailed steps for importing snapshots, loading into sub-accounts, and resolving asset conflicts."}
        ]
        
        consolidated_intel = {}
        
        for seq in sequences:
            topic = seq.get("topic")
            print(f"[EXTENDLY] Absorbing: {topic}...")
            
            # Use Gemini for synthesis of each topic
            prompt = f"Consolidate all known expertise for: {topic}. {seq.get('context', '')} {seq.get('description', '')}. Focus on technical implementation details for a GoHighLevel agency."
            
            response = self.client.models.generate_content(
                model='gemini-2.0-flash',
                contents=prompt
            )
            
            consolidated_intel[topic] = response.text
            
        # Store as Unified Knowledge Base
        with open(self.kb_path, "w") as f:
            json.dump(consolidated_intel, f, indent=4)
            
        print(f"[OK] Extendly Mastery KB stored at: {self.kb_path}")
        return self.kb_path

if __name__ == "__main__":
    absorber = ExtendlyKnowledgeAbsorber()
    absorber.absorb_sequence()
