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

from typing import Any, Optional, Union

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 UNSET, UnsetType, Upload
from .clone_repository import CloneRepository
from .create_repository import CreateRepository
from .delete_artifact import DeleteArtifact
from .delete_repository import DeleteRepository
from .enums import Visibility
from .get_artifact import GetArtifact
from .get_artifact_url import GetArtifactURL
from .input_types import RepoInput
from .rename_repository import RenameRepository
from .update_repository import UpdateRepository
from .upload_artifact import UploadArtifact


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


class Client(BaseClient):
    def __init__(self, auth=None, headers=None, client=None, **kwargs):
        origin = cfg("git.sr.ht", "api-origin", default=get_origin("git.sr.ht"))
        if auth is None:
            auth = InternalAuth()
        headers = {**auth.headers, "User-Agent": "git.sr.ht frontend"}
        if has_request_context():
            headers["X-Forwarded-For"] = ", ".join(request.access_route)
        if not client:
            backend_timeout = cfg("git.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 upload_artifact(
        self, repo_id: int, revspec: str, file: Upload, **kwargs: Any
    ) -> UploadArtifact:
        query = gql("""
            mutation UploadArtifact($repoID: Int!, $revspec: String!, $file: Upload!) {
              artifact: uploadArtifact(repoId: $repoID, revspec: $revspec, file: $file) {
                id
              }
            }
            """)
        variables: dict[str, object] = {
            "repoID": repo_id,
            "revspec": revspec,
            "file": file,
        }
        response = self.execute(
            query=query, operation_name="UploadArtifact", variables=variables, **kwargs
        )
        data = self.get_data(response)
        return UploadArtifact.model_validate(data)

    def get_artifact_url(
        self, owner: str, repo: str, ref: str, filename: str, **kwargs: Any
    ) -> GetArtifactURL:
        query = gql("""
            query GetArtifactURL($owner: String!, $repo: String!, $ref: String!, $filename: String!) {
              user(username: $owner) {
                repository(name: $repo) {
                  reference(name: $ref) {
                    artifact(filename: $filename) {
                      filename
                      url
                    }
                  }
                }
              }
            }
            """)
        variables: dict[str, object] = {
            "owner": owner,
            "repo": repo,
            "ref": ref,
            "filename": filename,
        }
        response = self.execute(
            query=query, operation_name="GetArtifactURL", variables=variables, **kwargs
        )
        data = self.get_data(response)
        return GetArtifactURL.model_validate(data)

    def get_artifact(
        self, owner: str, repo: str, ref: str, filename: str, **kwargs: Any
    ) -> GetArtifact:
        query = gql("""
            query GetArtifact($owner: String!, $repo: String!, $ref: String!, $filename: String!) {
              user(username: $owner) {
                repository(name: $repo) {
                  reference(name: $ref) {
                    artifact(filename: $filename) {
                      id
                    }
                  }
                }
              }
            }
            """)
        variables: dict[str, object] = {
            "owner": owner,
            "repo": repo,
            "ref": ref,
            "filename": filename,
        }
        response = self.execute(
            query=query, operation_name="GetArtifact", variables=variables, **kwargs
        )
        data = self.get_data(response)
        return GetArtifact.model_validate(data)

    def delete_artifact(self, id: int, **kwargs: Any) -> DeleteArtifact:
        query = gql("""
            mutation DeleteArtifact($id: Int!) {
              deleteArtifact(id: $id) {
                id
              }
            }
            """)
        variables: dict[str, object] = {"id": id}
        response = self.execute(
            query=query, operation_name="DeleteArtifact", variables=variables, **kwargs
        )
        data = self.get_data(response)
        return DeleteArtifact.model_validate(data)

    def create_repository(
        self,
        name: str,
        visibility: Visibility,
        description: Union[Optional[str], UnsetType] = UNSET,
        **kwargs: Any
    ) -> CreateRepository:
        query = gql("""
            mutation CreateRepository($name: String!, $visibility: Visibility!, $description: String) {
              repository: createRepository(
                name: $name
                visibility: $visibility
                description: $description
              ) {
                name
              }
            }
            """)
        variables: dict[str, object] = {
            "name": name,
            "visibility": visibility,
            "description": description,
        }
        response = self.execute(
            query=query,
            operation_name="CreateRepository",
            variables=variables,
            **kwargs
        )
        data = self.get_data(response)
        return CreateRepository.model_validate(data)

    def clone_repository(
        self,
        clone_url: str,
        name: str,
        visibility: Visibility,
        description: Union[Optional[str], UnsetType] = UNSET,
        **kwargs: Any
    ) -> CloneRepository:
        query = gql("""
            mutation CloneRepository($cloneUrl: String!, $name: String!, $visibility: Visibility!, $description: String) {
              repository: createRepository(
                name: $name
                visibility: $visibility
                description: $description
                cloneUrl: $cloneUrl
              ) {
                name
              }
            }
            """)
        variables: dict[str, object] = {
            "cloneUrl": clone_url,
            "name": name,
            "visibility": visibility,
            "description": description,
        }
        response = self.execute(
            query=query, operation_name="CloneRepository", variables=variables, **kwargs
        )
        data = self.get_data(response)
        return CloneRepository.model_validate(data)

    def rename_repository(self, id: int, name: str, **kwargs: Any) -> RenameRepository:
        query = gql("""
            mutation RenameRepository($id: Int!, $name: String!) {
              repository: updateRepository(id: $id, input: {name: $name}) {
                name
              }
            }
            """)
        variables: dict[str, object] = {"id": id, "name": name}
        response = self.execute(
            query=query,
            operation_name="RenameRepository",
            variables=variables,
            **kwargs
        )
        data = self.get_data(response)
        return RenameRepository.model_validate(data)

    def update_repository(
        self, id: int, input: RepoInput, **kwargs: Any
    ) -> UpdateRepository:
        query = gql("""
            mutation UpdateRepository($id: Int!, $input: RepoInput!) {
              updateRepository(id: $id, input: $input) {
                id
              }
            }
            """)
        variables: dict[str, object] = {"id": id, "input": input}
        response = self.execute(
            query=query,
            operation_name="UpdateRepository",
            variables=variables,
            **kwargs
        )
        data = self.get_data(response)
        return UpdateRepository.model_validate(data)

    def delete_repository(self, id: int, **kwargs: Any) -> DeleteRepository:
        query = gql("""
            mutation DeleteRepository($id: Int!) {
              repository: deleteRepository(id: $id) {
                id
                name
                owner {
                  __typename
                  canonicalName
                  ... on User {
                    username
                  }
                }
              }
            }
            """)
        variables: dict[str, object] = {"id": id}
        response = self.execute(
            query=query,
            operation_name="DeleteRepository",
            variables=variables,
            **kwargs
        )
        data = self.get_data(response)
        return DeleteRepository.model_validate(data)

    upload_artifact_query = "mutation UploadArtifact($repoID: Int!, $revspec: String!, $file: Upload!) {\n  artifact: uploadArtifact(repoId: $repoID, revspec: $revspec, file: $file) {\n    id\n  }\n}"
    get_artifact_url_query = "query GetArtifactURL($owner: String!, $repo: String!, $ref: String!, $filename: String!) {\n  user(username: $owner) {\n    repository(name: $repo) {\n      reference(name: $ref) {\n        artifact(filename: $filename) {\n          filename\n          url\n        }\n      }\n    }\n  }\n}"
    get_artifact_query = "query GetArtifact($owner: String!, $repo: String!, $ref: String!, $filename: String!) {\n  user(username: $owner) {\n    repository(name: $repo) {\n      reference(name: $ref) {\n        artifact(filename: $filename) {\n          id\n        }\n      }\n    }\n  }\n}"
    delete_artifact_query = "mutation DeleteArtifact($id: Int!) {\n  deleteArtifact(id: $id) {\n    id\n  }\n}"
    create_repository_query = "mutation CreateRepository($name: String!, $visibility: Visibility!, $description: String) {\n  repository: createRepository(\n    name: $name\n    visibility: $visibility\n    description: $description\n  ) {\n    name\n  }\n}"
    clone_repository_query = "mutation CloneRepository($cloneUrl: String!, $name: String!, $visibility: Visibility!, $description: String) {\n  repository: createRepository(\n    name: $name\n    visibility: $visibility\n    description: $description\n    cloneUrl: $cloneUrl\n  ) {\n    name\n  }\n}"
    rename_repository_query = "mutation RenameRepository($id: Int!, $name: String!) {\n  repository: updateRepository(id: $id, input: {name: $name}) {\n    name\n  }\n}"
    update_repository_query = "mutation UpdateRepository($id: Int!, $input: RepoInput!) {\n  updateRepository(id: $id, input: $input) {\n    id\n  }\n}"
    delete_repository_query = "mutation DeleteRepository($id: Int!) {\n  repository: deleteRepository(id: $id) {\n    id\n    name\n    owner {\n      __typename\n      canonicalName\n      ... on User {\n        username\n      }\n    }\n  }\n}"
