# Generated by ariadne-codegen
# Source: pastesrht/graphql/

from typing import Any

import httpx
from flask import has_request_context, request
from srht.config import cfg, get_origin
from srht.graphql import InternalAuth

from .base_client import BaseClient
from .base_model import Upload
from .create_paste import CreatePaste
from .delete_paste import DeletePaste
from .enums import Visibility
from .update_paste import UpdatePaste


def gql(q: str) -> str:
    return q


class Client(BaseClient):
    def __init__(self, auth=None, headers=None, client=None, **kwargs):
        origin = cfg("paste.sr.ht", "api-origin", default=get_origin("paste.sr.ht"))
        if auth is None:
            auth = InternalAuth()
        headers = {**auth.headers, "User-Agent": "paste.sr.ht frontend"}
        if has_request_context():
            headers["X-Forwarded-For"] = ", ".join(request.access_route)
        if not client:
            backend_timeout = cfg("paste.sr.ht", "backend-timeout", default=3)
            client = httpx.Client(
                headers=headers, timeout=httpx.Timeout(int(backend_timeout))
            )
        super().__init__(origin + "/query", headers, client, **kwargs)

    def create_paste(
        self, files: list[Upload], visibility: Visibility, **kwargs: Any
    ) -> CreatePaste:
        query = gql("""
            mutation CreatePaste($files: [Upload!]!, $visibility: Visibility!) {
              create(files: $files, visibility: $visibility) {
                id
              }
            }
            """)
        variables: dict[str, object] = {"files": files, "visibility": visibility}
        response = self.execute(
            query=query, operation_name="CreatePaste", variables=variables, **kwargs
        )
        data = self.get_data(response)
        return CreatePaste.model_validate(data)

    def update_paste(
        self, id: str, visibility: Visibility, **kwargs: Any
    ) -> UpdatePaste:
        query = gql("""
            mutation UpdatePaste($id: String!, $visibility: Visibility!) {
              update(id: $id, visibility: $visibility) {
                id
              }
            }
            """)
        variables: dict[str, object] = {"id": id, "visibility": visibility}
        response = self.execute(
            query=query, operation_name="UpdatePaste", variables=variables, **kwargs
        )
        data = self.get_data(response)
        return UpdatePaste.model_validate(data)

    def delete_paste(self, id: str, **kwargs: Any) -> DeletePaste:
        query = gql("""
            mutation DeletePaste($id: String!) {
              delete(id: $id) {
                id
              }
            }
            """)
        variables: dict[str, object] = {"id": id}
        response = self.execute(
            query=query, operation_name="DeletePaste", variables=variables, **kwargs
        )
        data = self.get_data(response)
        return DeletePaste.model_validate(data)

    create_paste_query = "mutation CreatePaste($files: [Upload!]!, $visibility: Visibility!) {\n  create(files: $files, visibility: $visibility) {\n    id\n  }\n}"
    update_paste_query = "mutation UpdatePaste($id: String!, $visibility: Visibility!) {\n  update(id: $id, visibility: $visibility) {\n    id\n  }\n}"
    delete_paste_query = (
        "mutation DeletePaste($id: String!) {\n  delete(id: $id) {\n    id\n  }\n}"
    )
