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

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, Upload
from .create_mailing_list import CreateMailingList
from .delete_mailing_list import DeleteMailingList
from .enums import Visibility
from .export_archive import ExportArchive
from .get_patchset_thread_blocks import GetPatchsetThreadBlocks
from .get_preferences import GetPreferences
from .import_spool import ImportSpool
from .input_types import ACLInput, MailingListInput, PreferencesInput
from .update_mailing_list import UpdateMailingList
from .update_mailing_list_access import UpdateMailingListAccess
from .update_preferences import UpdatePreferences


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


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

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

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

    def import_spool(self, list_id: int, spool: Upload, **kwargs: Any) -> ImportSpool:
        query = gql("""
            mutation ImportSpool($listID: Int!, $spool: Upload!) {
              importMailingListSpool(listID: $listID, spool: $spool)
            }
            """)
        variables: dict[str, object] = {"listID": list_id, "spool": spool}
        response = self.execute(
            query=query, operation_name="ImportSpool", variables=variables, **kwargs
        )
        data = self.get_data(response)
        return ImportSpool.model_validate(data)

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

    def export_archive(
        self, owner: str, list_name: str, **kwargs: Any
    ) -> ExportArchive:
        query = gql("""
            query ExportArchive($owner: String!, $listName: String!) {
              user(username: $owner) {
                list(name: $listName) {
                  archive
                  last30days
                }
              }
            }
            """)
        variables: dict[str, object] = {"owner": owner, "listName": list_name}
        response = self.execute(
            query=query, operation_name="ExportArchive", variables=variables, **kwargs
        )
        data = self.get_data(response)
        return ExportArchive.model_validate(data)

    def get_patchset_thread_blocks(
        self, patchset: int, **kwargs: Any
    ) -> GetPatchsetThreadBlocks:
        query = gql("""
            query GetPatchsetThreadBlocks($patchset: Int!) {
              patchset(id: $patchset) {
                thread {
                  blocks {
                    key
                    source {
                      id
                    }
                    sourceRange {
                      start
                      end
                    }
                    parentRange {
                      start
                      end
                    }
                  }
                }
              }
            }
            """)
        variables: dict[str, object] = {"patchset": patchset}
        response = self.execute(
            query=query,
            operation_name="GetPatchsetThreadBlocks",
            variables=variables,
            **kwargs
        )
        data = self.get_data(response)
        return GetPatchsetThreadBlocks.model_validate(data)

    def get_preferences(self, **kwargs: Any) -> GetPreferences:
        query = gql("""
            query GetPreferences {
              preferences {
                copySelf
              }
            }
            """)
        variables: dict[str, object] = {}
        response = self.execute(
            query=query, operation_name="GetPreferences", variables=variables, **kwargs
        )
        data = self.get_data(response)
        return GetPreferences.model_validate(data)

    def update_preferences(
        self, prefs: PreferencesInput, **kwargs: Any
    ) -> UpdatePreferences:
        query = gql("""
            mutation UpdatePreferences($prefs: PreferencesInput!) {
              updatePreferences(preferences: $prefs) {
                copySelf
              }
            }
            """)
        variables: dict[str, object] = {"prefs": prefs}
        response = self.execute(
            query=query,
            operation_name="UpdatePreferences",
            variables=variables,
            **kwargs
        )
        data = self.get_data(response)
        return UpdatePreferences.model_validate(data)

    create_mailing_list_query = "mutation CreateMailingList($name: String!, $visibility: Visibility!, $description: String) {\n  mailingList: createMailingList(\n    name: $name\n    visibility: $visibility\n    description: $description\n  ) {\n    name\n    owner {\n      __typename\n      canonicalName\n    }\n  }\n}"
    update_mailing_list_query = "mutation UpdateMailingList($id: Int!, $input: MailingListInput!) {\n  updateMailingList(id: $id, input: $input) {\n    id\n  }\n}"
    update_mailing_list_access_query = "mutation UpdateMailingListAccess($id: Int!, $input: ACLInput!) {\n  updateMailingListACL(listID: $id, input: $input) {\n    id\n  }\n}"
    import_spool_query = "mutation ImportSpool($listID: Int!, $spool: Upload!) {\n  importMailingListSpool(listID: $listID, spool: $spool)\n}"
    delete_mailing_list_query = "mutation DeleteMailingList($id: Int!) {\n  deleteMailingList(id: $id) {\n    id\n  }\n}"
    export_archive_query = "query ExportArchive($owner: String!, $listName: String!) {\n  user(username: $owner) {\n    list(name: $listName) {\n      archive\n      last30days\n    }\n  }\n}"
    get_patchset_thread_blocks_query = "query GetPatchsetThreadBlocks($patchset: Int!) {\n  patchset(id: $patchset) {\n    thread {\n      blocks {\n        key\n        source {\n          id\n        }\n        sourceRange {\n          start\n          end\n        }\n        parentRange {\n          start\n          end\n        }\n      }\n    }\n  }\n}"
    get_preferences_query = (
        "query GetPreferences {\n  preferences {\n    copySelf\n  }\n}"
    )
    update_preferences_query = "mutation UpdatePreferences($prefs: PreferencesInput!) {\n  updatePreferences(preferences: $prefs) {\n    copySelf\n  }\n}"
