"""
Pixel Office – LangChain Custom Tools Wrapper
Install prerequisites: pip install langchain-core requests
"""

from langchain_core.tools import tool
from typing import Optional
import requests

@tool
def scan_gdpr_compliance(url: str, payment_ref: Optional[str] = None) -> str:
    """
    Scans a website URL using headless browsers to audit cookie consent banner compliance.
    Requires X-Payment-Reference header obtained via PixelPay.
    """
    headers = {"Content-Type": "application/json"}
    if payment_ref:
        headers["X-Payment-Reference"] = payment_ref
    try:
        res = requests.post("https://api.pixeloffice.eu/api/gdpr/scan", json={"url": url}, headers=headers)
        return res.text
    except Exception as e:
        return f"Error executing GDPR scan: {str(e)}"

@tool
def generate_legal_policy(company_name: str, website_url: str, policy_type: str = "terms", country: str = "Česká republika", email: Optional[str] = None, collect_personal_data: bool = False, use_cookies: bool = False, payment_ref: Optional[str] = None) -> str:
    """
    Generates tailored legal terms, privacy statements, refund rules, and cookies guidelines.
    Requires companyName and websiteUrl. Requires X-Payment-Reference header obtained via PixelPay.
    """
    headers = {"Content-Type": "application/json"}
    if payment_ref:
        headers["X-Payment-Reference"] = payment_ref
    
    payload = {
        "companyName": company_name,
        "websiteUrl": website_url,
        "policyType": policy_type,
        "country": country,
        "email": email,
        "collectPersonalData": collect_personal_data,
        "useCookies": use_cookies
    }
    try:
        res = requests.post("https://api.pixeloffice.eu/api/policy/generate", json=payload, headers=headers)
        return res.text
    except Exception as e:
        return f"Error generating policy: {str(e)}"

@tool
def forge_universal_icons(image_base64: str, payment_ref: Optional[str] = None) -> str:
    """
    Uploads a base64 source logo image and compiles PWA, iOS, and favicon packages.
    Requires X-Payment-Reference header obtained via PixelPay.
    """
    headers = {"Content-Type": "application/json"}
    if payment_ref:
        headers["X-Payment-Reference"] = payment_ref
    try:
        res = requests.post("https://api.pixeloffice.eu/api/favicon/generate", json={"image": image_base64}, headers=headers)
        return res.text
    except Exception as e:
        return f"Error executing Icon Forge: {str(e)}"

@tool
def create_whistleblower_portal(company_name: str, custom_subdomain: str, contact_email: str, payment_ref: Optional[str] = None) -> str:
    """
    Configures and creates a secure whistleblower reporting portal subdomain mapping.
    Requires companyName, customSubdomain, and contactEmail. Requires X-Payment-Reference header obtained via PixelPay.
    """
    headers = {"Content-Type": "application/json"}
    if payment_ref:
        headers["X-Payment-Reference"] = payment_ref
    
    payload = {
        "companyName": company_name,
        "customSubdomain": custom_subdomain,
        "contactEmail": contact_email
    }
    try:
        res = requests.post("https://api.pixeloffice.eu/api/whistleblowing/create-portal", json=payload, headers=headers)
        return res.text
    except Exception as e:
        return f"Error establishing whistleblower portal: {str(e)}"
