# Generated by ariadne-codegen
# Source: hubsrht/services/hub/

from typing import Any, Optional, Union

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

from .base_client import BaseClient
from .base_model import UNSET, UnsetType
from .create_project import CreateProject
from .delete_project import DeleteProject
from .enums import Visibility
from .get_resource_projects import GetResourceProjects
from .input_types import ProjectInput
from .link_mailing_list import LinkMailingList
from .link_source import LinkSource
from .link_tracker import LinkTracker
from .unlink_mailing_list import UnlinkMailingList
from .unlink_source import UnlinkSource
from .unlink_tracker import UnlinkTracker
from .update_project import UpdateProject


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


class HubClient(BaseClient):
    def __init__(self, auth=None, headers=None, client=None, **kwargs):
        origin = get_api("hub.sr.ht")
        if auth is None:
            auth = InternalAuth()
        headers = {**auth.headers, "User-Agent": "hub.sr.ht frontend"}
        if has_request_context():
            headers["X-Forwarded-For"] = ", ".join(request.access_route)
        if not client:
            backend_timeout = cfg("hub.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_project(
        self,
        name: str,
        visibility: Visibility,
        description: Union[Optional[str], UnsetType] = UNSET,
        tags: Union[Optional[list[str]], UnsetType] = UNSET,
        **kwargs: Any
    ) -> CreateProject:
        query = gql("""
            mutation CreateProject($name: String!, $visibility: Visibility!, $description: String, $tags: [String!]) {
              project: createProject(
                name: $name
                visibility: $visibility
                description: $description
                tags: $tags
              ) {
                rid
                name
              }
            }
            """)
        variables: dict[str, object] = {
            "name": name,
            "visibility": visibility,
            "description": description,
            "tags": tags,
        }
        response = self.execute(
            query=query, operation_name="CreateProject", variables=variables, **kwargs
        )
        data = self.get_data(response)
        return CreateProject.model_validate(data)

    def update_project(
        self, rid: str, input: ProjectInput, **kwargs: Any
    ) -> UpdateProject:
        query = gql("""
            mutation UpdateProject($rid: ID!, $input: ProjectInput!) {
              project: updateProject(rid: $rid, input: $input) {
                rid
                name
              }
            }
            """)
        variables: dict[str, object] = {"rid": rid, "input": input}
        response = self.execute(
            query=query, operation_name="UpdateProject", variables=variables, **kwargs
        )
        data = self.get_data(response)
        return UpdateProject.model_validate(data)

    def delete_project(self, rid: str, **kwargs: Any) -> DeleteProject:
        query = gql("""
            mutation DeleteProject($rid: ID!) {
              project: deleteProject(rid: $rid) {
                rid
                name
              }
            }
            """)
        variables: dict[str, object] = {"rid": rid}
        response = self.execute(
            query=query, operation_name="DeleteProject", variables=variables, **kwargs
        )
        data = self.get_data(response)
        return DeleteProject.model_validate(data)

    def link_mailing_list(
        self, project_id: str, list_id: str, **kwargs: Any
    ) -> LinkMailingList:
        query = gql("""
            mutation LinkMailingList($projectID: ID!, $listID: ID!) {
              mailing_list: linkMailingList(projectID: $projectID, listID: $listID) {
                rid
              }
            }
            """)
        variables: dict[str, object] = {"projectID": project_id, "listID": list_id}
        response = self.execute(
            query=query, operation_name="LinkMailingList", variables=variables, **kwargs
        )
        data = self.get_data(response)
        return LinkMailingList.model_validate(data)

    def unlink_mailing_list(
        self, project_id: str, list_id: str, **kwargs: Any
    ) -> UnlinkMailingList:
        query = gql("""
            mutation UnlinkMailingList($projectID: ID!, $listID: ID!) {
              mailing_list: unlinkMailingList(projectID: $projectID, listID: $listID) {
                rid
              }
            }
            """)
        variables: dict[str, object] = {"projectID": project_id, "listID": list_id}
        response = self.execute(
            query=query,
            operation_name="UnlinkMailingList",
            variables=variables,
            **kwargs
        )
        data = self.get_data(response)
        return UnlinkMailingList.model_validate(data)

    def link_source(
        self, project_id: str, source_repo_id: str, **kwargs: Any
    ) -> LinkSource:
        query = gql("""
            mutation LinkSource($projectID: ID!, $sourceRepoID: ID!) {
              source: linkSource(projectID: $projectID, sourceRepoID: $sourceRepoID) {
                rid
              }
            }
            """)
        variables: dict[str, object] = {
            "projectID": project_id,
            "sourceRepoID": source_repo_id,
        }
        response = self.execute(
            query=query, operation_name="LinkSource", variables=variables, **kwargs
        )
        data = self.get_data(response)
        return LinkSource.model_validate(data)

    def unlink_source(
        self, project_id: str, source_repo_id: str, **kwargs: Any
    ) -> UnlinkSource:
        query = gql("""
            mutation UnlinkSource($projectID: ID!, $sourceRepoID: ID!) {
              source: unlinkSource(projectID: $projectID, sourceRepoID: $sourceRepoID) {
                rid
              }
            }
            """)
        variables: dict[str, object] = {
            "projectID": project_id,
            "sourceRepoID": source_repo_id,
        }
        response = self.execute(
            query=query, operation_name="UnlinkSource", variables=variables, **kwargs
        )
        data = self.get_data(response)
        return UnlinkSource.model_validate(data)

    def link_tracker(
        self, project_id: str, tracker_id: str, **kwargs: Any
    ) -> LinkTracker:
        query = gql("""
            mutation LinkTracker($projectID: ID!, $trackerID: ID!) {
              tracker: linkTracker(projectID: $projectID, trackerID: $trackerID) {
                rid
              }
            }
            """)
        variables: dict[str, object] = {
            "projectID": project_id,
            "trackerID": tracker_id,
        }
        response = self.execute(
            query=query, operation_name="LinkTracker", variables=variables, **kwargs
        )
        data = self.get_data(response)
        return LinkTracker.model_validate(data)

    def unlink_tracker(
        self, project_id: str, tracker_id: str, **kwargs: Any
    ) -> UnlinkTracker:
        query = gql("""
            mutation UnlinkTracker($projectID: ID!, $trackerID: ID!) {
              tracker: unlinkTracker(projectID: $projectID, trackerID: $trackerID) {
                rid
              }
            }
            """)
        variables: dict[str, object] = {
            "projectID": project_id,
            "trackerID": tracker_id,
        }
        response = self.execute(
            query=query, operation_name="UnlinkTracker", variables=variables, **kwargs
        )
        data = self.get_data(response)
        return UnlinkTracker.model_validate(data)

    def get_resource_projects(
        self, rid: str, cursor: Union[Optional[Any], UnsetType] = UNSET, **kwargs: Any
    ) -> GetResourceProjects:
        query = gql("""
            query GetResourceProjects($rid: ID!, $cursor: Cursor) {
              resource(rid: $rid) {
                __typename
                name
                description
                owner {
                  __typename
                  canonicalName
                }
                ... on SourceRepo {
                  repoType
                }
                projects(cursor: $cursor) {
                  results {
                    name
                    description
                    visibility
                    owner {
                      __typename
                      canonicalName
                    }
                  }
                  cursor
                }
              }
            }
            """)
        variables: dict[str, object] = {"rid": rid, "cursor": cursor}
        response = self.execute(
            query=query,
            operation_name="GetResourceProjects",
            variables=variables,
            **kwargs
        )
        data = self.get_data(response)
        return GetResourceProjects.model_validate(data)

    create_project_query = "mutation CreateProject($name: String!, $visibility: Visibility!, $description: String, $tags: [String!]) {\n  project: createProject(\n    name: $name\n    visibility: $visibility\n    description: $description\n    tags: $tags\n  ) {\n    rid\n    name\n  }\n}"
    update_project_query = "mutation UpdateProject($rid: ID!, $input: ProjectInput!) {\n  project: updateProject(rid: $rid, input: $input) {\n    rid\n    name\n  }\n}"
    delete_project_query = "mutation DeleteProject($rid: ID!) {\n  project: deleteProject(rid: $rid) {\n    rid\n    name\n  }\n}"
    link_mailing_list_query = "mutation LinkMailingList($projectID: ID!, $listID: ID!) {\n  mailing_list: linkMailingList(projectID: $projectID, listID: $listID) {\n    rid\n  }\n}"
    unlink_mailing_list_query = "mutation UnlinkMailingList($projectID: ID!, $listID: ID!) {\n  mailing_list: unlinkMailingList(projectID: $projectID, listID: $listID) {\n    rid\n  }\n}"
    link_source_query = "mutation LinkSource($projectID: ID!, $sourceRepoID: ID!) {\n  source: linkSource(projectID: $projectID, sourceRepoID: $sourceRepoID) {\n    rid\n  }\n}"
    unlink_source_query = "mutation UnlinkSource($projectID: ID!, $sourceRepoID: ID!) {\n  source: unlinkSource(projectID: $projectID, sourceRepoID: $sourceRepoID) {\n    rid\n  }\n}"
    link_tracker_query = "mutation LinkTracker($projectID: ID!, $trackerID: ID!) {\n  tracker: linkTracker(projectID: $projectID, trackerID: $trackerID) {\n    rid\n  }\n}"
    unlink_tracker_query = "mutation UnlinkTracker($projectID: ID!, $trackerID: ID!) {\n  tracker: unlinkTracker(projectID: $projectID, trackerID: $trackerID) {\n    rid\n  }\n}"
    get_resource_projects_query = "query GetResourceProjects($rid: ID!, $cursor: Cursor) {\n  resource(rid: $rid) {\n    __typename\n    name\n    description\n    owner {\n      __typename\n      canonicalName\n    }\n    ... on SourceRepo {\n      repoType\n    }\n    projects(cursor: $cursor) {\n      results {\n        name\n        description\n        visibility\n        owner {\n          __typename\n          canonicalName\n        }\n      }\n      cursor\n    }\n  }\n}"
