from typing import List, Dict, Any, Optional

class TranslationsResource:
    def __init__(self, client):
        self.client = client

    def get_config(self, project_id: str) -> Dict[str, Any]:
        """Get translation configuration for a project."""
        return self.client._request("GET", "/translations/config", params={"project_id": project_id})

    def update_config(self, project_id: str, default_language: Optional[str] = None, 
                      supported_languages: Optional[List[str]] = None, 
                      auto_translate: Optional[bool] = None, 
                      provider: Optional[str] = None) -> Dict[str, Any]:
        """Update translation configuration."""
        data = {}
        if default_language: data["default_language"] = default_language
        if supported_languages is not None: data["supported_languages"] = supported_languages
        if auto_translate is not None: data["auto_translate"] = auto_translate
        if provider: data["provider"] = provider
        
        return self.client._request("PUT", "/translations/config", params={"project_id": project_id}, json_data=data)

    def add_rule(self, project_id: str, rule_type: str, original_text: str, 
                 translated_text: Optional[str] = None, language_code: Optional[str] = None) -> Dict[str, Any]:
        """Add a translation rule (glossary or do-not-translate)."""
        data = {
            "rule_type": rule_type,
            "original_text": original_text
        }
        if translated_text: data["translated_text"] = translated_text
        if language_code: data["language_code"] = language_code
        
        return self.client._request("POST", "/translations/rules", params={"project_id": project_id}, json_data=data)

    def delete_rule(self, project_id: str, rule_id: int) -> bool:
        """Delete a translation rule."""
        self.client._request("DELETE", f"/translations/rules/{rule_id}", params={"project_id": project_id})
        return True

    def translate(self, project_id: str, texts: List[str], target_lang: str, instructions: Optional[str] = None) -> Dict[str, str]:
        """Translate a batch of text."""
        data = {
            "texts": texts,
            "target_lang": target_lang
        }
        if instructions: data["instructions"] = instructions
        
        response = self.client._request("POST", f"/widget/{project_id}/translate", json_data=data)
        return response.get("translations", {})
