"""
Tano Shield Firewall Client
"""

from tano_shield.core import TanoShieldClient
from tano_shield.core.errors import ValidationError
from urllib.parse import urlencode
import time

class FirewallClient(TanoShieldClient):
    """
    Firewall client for Tano Shield
    """
    
    def __init__(self, **options):
        """
        Create a new Firewall client
        
        Args:
            **options: Client options
        """
        super().__init__(**options)
    
    def check_input(self, user_input, system_prompt=None, response=None, event_id=None, security_checks=None, prompt=None):
        """
        Perform a firewall check on user input
        
        Args:
            user_input (str): The user input to check
            system_prompt (str, optional): The system prompt
            response (str, optional): The AI response to check
            event_id (str, optional): A unique identifier for the event
            security_checks (list, optional): Specific security checks to perform
            prompt (str, optional): The original prompt
        
        Returns:
            dict: Firewall check results
        
        Raises:
            ValidationError: If user input is not provided
        """
        if not user_input:
            raise ValidationError('User input is required')
        
        payload = {
            'prompt': prompt or user_input,
            'system_prompt': system_prompt or '',
            'user_input': user_input,
            'response': response or '',
            'event_id': event_id or f'event-{int(time.time() * 1000)}',
            'securityChecks': security_checks or [
                'sycophancy',
                'faithfulness',
                'harmfulness',
                'implausible_output',
                'information_disclosure',
                'jailbreak'
            ],
        }
        
        return self.post(self.endpoints['FIREWALL_CHECK'], payload)
    
    def get_settings(self):
        """
        Get firewall settings
        
        Returns:
            dict: Firewall settings
        """
        return self.get(f"{self.endpoints['FIREWALL_CHECK']}/settings")
    
    def update_settings(self, settings):
        """
        Update firewall settings
        
        Args:
            settings (dict): New settings
                - default_checks (list, optional): Default checks to perform
                - save_history (bool, optional): Whether to save history
        
        Returns:
            dict: Updated settings
        """
        return self.post(f"{self.endpoints['FIREWALL_CHECK']}/settings", settings)
    
    def get_history(self, limit=None, safe=None, source=None, include_all_sources=None):
        """
        Get firewall call history
        
        Args:
            limit (int, optional): Maximum number of records to return
            safe (bool, optional): Filter by safety status
            source (str, optional): Filter by source
            include_all_sources (bool, optional): Whether to include all sources
        
        Returns:
            list: Firewall call history
        """
        params = {}
        
        if limit is not None:
            params['limit'] = limit
        if safe is not None:
            params['safe'] = safe
        if source:
            params['source'] = source
        if include_all_sources:
            params['includeAllSources'] = 'true'
        
        query_string = urlencode(params)
        url = f"{self.endpoints['FIREWALL_CHECK']}/history"
        if query_string:
            url = f"{url}?{query_string}"
        
        return self.get(url)
    
    def get_analytics(self, days=None):
        """
        Get firewall analytics
        
        Args:
            days (int, optional): Number of days to analyze
        
        Returns:
            dict: Firewall analytics
        """
        params = {}
        
        if days is not None:
            params['days'] = days
        
        query_string = urlencode(params)
        url = f"{self.endpoints['FIREWALL_CHECK']}/analytics"
        if query_string:
            url = f"{url}?{query_string}"
        
        return self.get(url)