"""
Pixel Office – CrewAI Custom Tools Wrapper
Install prerequisites: pip install crewai requests
"""

from crewai.tools import BaseTool
from typing import Optional
import requests

class GDPRScannerTool(BaseTool):
    name: str = "GDPR Cookie Consent Scanner"
    description: str = (
        "Scans a website URL using headless browsers to audit cookie consent banner compliance. "
        "Requires X-Payment-Reference header obtained via PixelPay."
    )

    def _run(self, url: str, payment_ref: Optional[str] = None) -> str:
        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)}"

class GlobalPolicyForgeTool(BaseTool):
    name: str = "Global Policy Forge"
    description: str = (
        "Generates tailored legal terms, privacy statements, refund rules, and cookies guidelines. "
        "Requires companyName and websiteUrl. Requires X-Payment-Reference header obtained via PixelPay."
    )

    def _run(self, 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:
        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)}"

class UniversalIconForgeTool(BaseTool):
    name: str = "Universal Icon Forge"
    description: str = (
        "Uploads a base64 source logo image and compiles PWA, iOS, and favicon packages. "
        "Requires X-Payment-Reference header obtained via PixelPay."
    )

    def _run(self, image_base64: str, payment_ref: Optional[str] = None) -> str:
        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)}"

class WhistleblowingSafeHavenTool(BaseTool):
    name: str = "Whistleblowing Safe Haven Portal Setup"
    description: str = (
        "Configures and creates a secure whistleblower reporting portal subdomain mapping. "
        "Requires companyName, customSubdomain, and contactEmail. Requires X-Payment-Reference header obtained via PixelPay."
    )

    def _run(self, company_name: str, custom_subdomain: str, contact_email: str, payment_ref: Optional[str] = None) -> str:
        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)}"
