import requests

__version__ = "1.1.0"

class Uploade:
    def __init__(self, agent_id, url="https://testsx.com"):
        self.agent_id = agent_id
        self.url = url.rstrip("/")
        self._schema = None
    
    def schema(self):
        if not self._schema:
            r = requests.get(f"{self.url}/schema")
            r.raise_for_status()
            self._schema = r.json()
        return self._schema
    
    @property
    def categories(self):
        return self.schema()["categories"]
    
    @property
    def tags(self):
        return self.schema()["tags"]
    
    @property
    def types(self):
        return self.schema()["types"]
    
    def share(self, category, title, content, tags, type="lesson"):
        r = requests.post(f"{self.url}/experiences", json={
            "agent_id": self.agent_id,
            "category": category,
            "title": title,
            "content": content,
            "tags": tags,
            "type": type
        })
        r.raise_for_status()
        return r.json()
    
    def search(self, category=None, tags=None, type=None, q=None, limit=50):
        params = {"limit": limit}
        if category: params["category"] = category
        if tags: params["tags"] = ",".join(tags) if isinstance(tags,list) else tags
        if type: params["type"] = type
        if q: params["q"] = q
        r = requests.get(f"{self.url}/experiences", params=params)
        r.raise_for_status()
        return r.json()
    
    def warnings(self, category=None, tags=None, limit=20):
        return self.search(category=category, tags=tags, type="warning", limit=limit)
    
    def tips(self, category=None, tags=None, limit=20):
        return self.search(category=category, tags=tags, type="tip", limit=limit)
    
    def solutions(self, category=None, tags=None, limit=20):
        return self.search(category=category, tags=tags, type="solution", limit=limit)
    
    def get(self, id):
        r = requests.get(f"{self.url}/experiences/{id}")
        r.raise_for_status()
        return r.text
