from textual.app import App, ComposeResult
from textual.widgets import Header, Footer, Static, DataTable, Label
from textual.containers import Container, Vertical, Horizontal
from rich.text import Text
from db import db, KNOWN_BOTS
import re

# Import Shared Bot List (We need to centralize this properly later, but for now we copy-paste or import)
# Ideally, we should move KNOWN_BOTS to a shared config.py, but to keep it simple we'll use a small helper here.

class RadarApp(App):
    CSS = """
    Screen {
        layout: vertical;
        background: #001000; /* Dark Radar Green Background */
    }

    #radar_header {
        height: auto;
        text-align: center;
        color: #00ff00;
        text-style: bold;
        background: #002200;
        border-bottom: solid green;
        padding: 0 1;
    }

    .panel {
        height: 1fr; 
        border: solid green;
        margin: 0 1;
    }
    
    .panel_title {
        text-align: center;
        background: #004400;
        color: #00ff00;
        text-style: bold;
        padding: 0 1;
        dock: top;
    }
    
    DataTable {
        height: 1fr;
        width: 100%;
        background: #001000;
        color: #00ff00;
    }
    """

    def compose(self) -> ComposeResult:
        yield Header()
        
        yield Static(
            "📡 CAPABILITY RADAR | Detecting Advanced Agent Behaviors", 
            id="radar_header"
        )

        # 1. JS Capability Panel
        with Vertical(classes="panel"):
            yield Label("⚡ JAVASCRIPT EXECUTORS (Caught by CAPTCHA)", classes="panel_title")
            yield DataTable(id="table_js")

        # 2. Intent Panel
        with Vertical(classes="panel"):
            yield Label("🧠 ACCEPTS MARKDOWN (Negotiated / Traced)", classes="panel_title")
            yield DataTable(id="table_md")

        yield Footer()

    def extract_and_stylize(self, agent_str):
        """Returns a tuple: (Styled Identity, Dimmed Full String)"""
        agent_str = agent_str.strip().replace("Mozilla/5.0 ", "")
        
        identity = "Unknown"
        identity_style = "dim white"
        
        # Find the specific Bot Identity
        for bot_name in KNOWN_BOTS:
            if bot_name in agent_str:
                identity = bot_name
                identity_style = "bold orange1"
                break
                
        # Format the outputs
        id_text = Text(identity, style=identity_style)
        full_text = Text(agent_str, style="dim green")
        
        return id_text, full_text

    def populate_table(self, table_id, data_source):
        try:
            table = self.query_one(f"#{table_id}", DataTable)
            table.add_columns("Hits", "Identity", "Raw Signature")
            
            data = data_source(limit=15) 
            
            if not data:
                table.add_row("-", "None", "No signals detected")
                return

            for ua, count in data:
                identity, signature = self.extract_and_stylize(ua)
                table.add_row(str(count), identity, signature)
                
        except Exception as e:
            table.clear()
            table.add_row("ERROR", "None", str(e)[:40])

    def on_mount(self) -> None:
        self.populate_table("table_js", db.get_js_executors)
        self.populate_table("table_md", db.get_markdown_readers)

if __name__ == "__main__":
    app = RadarApp()
    app.run()
