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

from typing import Any

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

from .assign_user import AssignUser
from .base_client import BaseClient
from .base_model import Upload
from .create_label import CreateLabel
from .create_tracker import CreateTracker
from .delete_label import DeleteLabel
from .delete_ticket import DeleteTicket
from .delete_tracker import DeleteTracker
from .enums import Visibility
from .import_tracker_dump import ImportTrackerDump
from .input_types import (
    ACLInput,
    SubmitCommentEmailInput,
    SubmitCommentInput,
    SubmitTicketEmailInput,
    SubmitTicketInput,
    TrackerInput,
    UpdateLabelInput,
    UpdateTicketInput,
)
from .label_ticket import LabelTicket
from .rename_tracker import RenameTracker
from .submit_comment import SubmitComment
from .submit_comment_via_email import SubmitCommentViaEmail
from .submit_ticket import SubmitTicket
from .submit_ticket_via_email import SubmitTicketViaEmail
from .unassign_user import UnassignUser
from .unlabel_ticket import UnlabelTicket
from .update_label import UpdateLabel
from .update_ticket import UpdateTicket
from .update_tracker import UpdateTracker
from .update_tracker_access import UpdateTrackerAccess


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


class Client(BaseClient):
    def __init__(self, auth=None, headers=None, client=None, **kwargs):
        origin = get_api("todo.sr.ht")
        if auth is None:
            auth = InternalAuth()
        headers = {**auth.headers, "User-Agent": "todo.sr.ht frontend"}
        if has_request_context():
            headers["X-Forwarded-For"] = ", ".join(request.access_route)
        if not client:
            backend_timeout = cfg("todo.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 submit_ticket_via_email(
        self, tracker_id: int, input: SubmitTicketEmailInput, **kwargs: Any
    ) -> SubmitTicketViaEmail:
        query = gql("""
            mutation SubmitTicketViaEmail($trackerId: Int!, $input: SubmitTicketEmailInput!) {
              ticket: submitTicketEmail(trackerId: $trackerId, input: $input) {
                id
              }
            }
            """)
        variables: dict[str, object] = {"trackerId": tracker_id, "input": input}
        response = self.execute(
            query=query,
            operation_name="SubmitTicketViaEmail",
            variables=variables,
            **kwargs
        )
        data = self.get_data(response)
        return SubmitTicketViaEmail.model_validate(data)

    def submit_comment_via_email(
        self,
        tracker_id: int,
        ticket_id: int,
        input: SubmitCommentEmailInput,
        **kwargs: Any
    ) -> SubmitCommentViaEmail:
        query = gql("""
            mutation SubmitCommentViaEmail($trackerId: Int!, $ticketId: Int!, $input: SubmitCommentEmailInput!) {
              event: submitCommentEmail(
                trackerId: $trackerId
                ticketId: $ticketId
                input: $input
              ) {
                id
              }
            }
            """)
        variables: dict[str, object] = {
            "trackerId": tracker_id,
            "ticketId": ticket_id,
            "input": input,
        }
        response = self.execute(
            query=query,
            operation_name="SubmitCommentViaEmail",
            variables=variables,
            **kwargs
        )
        data = self.get_data(response)
        return SubmitCommentViaEmail.model_validate(data)

    def update_tracker(
        self, id: int, input: TrackerInput, **kwargs: Any
    ) -> UpdateTracker:
        query = gql("""
            mutation UpdateTracker($id: Int!, $input: TrackerInput!) {
              tracker: updateTracker(id: $id, input: $input) {
                name
                owner {
                  __typename
                  canonicalName
                }
              }
            }
            """)
        variables: dict[str, object] = {"id": id, "input": input}
        response = self.execute(
            query=query, operation_name="UpdateTracker", variables=variables, **kwargs
        )
        data = self.get_data(response)
        return UpdateTracker.model_validate(data)

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

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

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

    def import_tracker_dump(
        self, tracker_id: int, dump: Upload, **kwargs: Any
    ) -> ImportTrackerDump:
        query = gql("""
            mutation ImportTrackerDump($trackerId: Int!, $dump: Upload!) {
              importTrackerDump(trackerId: $trackerId, dump: $dump)
            }
            """)
        variables: dict[str, object] = {"trackerId": tracker_id, "dump": dump}
        response = self.execute(
            query=query,
            operation_name="ImportTrackerDump",
            variables=variables,
            **kwargs
        )
        data = self.get_data(response)
        return ImportTrackerDump.model_validate(data)

    def submit_comment(
        self, tracker_id: int, ticket_id: int, input: SubmitCommentInput, **kwargs: Any
    ) -> SubmitComment:
        query = gql("""
            mutation SubmitComment($trackerId: Int!, $ticketId: Int!, $input: SubmitCommentInput!) {
              event: submitComment(trackerId: $trackerId, ticketId: $ticketId, input: $input) {
                id
              }
            }
            """)
        variables: dict[str, object] = {
            "trackerId": tracker_id,
            "ticketId": ticket_id,
            "input": input,
        }
        response = self.execute(
            query=query, operation_name="SubmitComment", variables=variables, **kwargs
        )
        data = self.get_data(response)
        return SubmitComment.model_validate(data)

    def update_ticket(
        self, tracker_id: int, ticket_id: int, input: UpdateTicketInput, **kwargs: Any
    ) -> UpdateTicket:
        query = gql("""
            mutation UpdateTicket($trackerId: Int!, $ticketId: Int!, $input: UpdateTicketInput!) {
              ticket: updateTicket(trackerId: $trackerId, ticketId: $ticketId, input: $input) {
                id
              }
            }
            """)
        variables: dict[str, object] = {
            "trackerId": tracker_id,
            "ticketId": ticket_id,
            "input": input,
        }
        response = self.execute(
            query=query, operation_name="UpdateTicket", variables=variables, **kwargs
        )
        data = self.get_data(response)
        return UpdateTicket.model_validate(data)

    def delete_ticket(
        self, tracker_id: int, ticket_id: int, **kwargs: Any
    ) -> DeleteTicket:
        query = gql("""
            mutation DeleteTicket($trackerId: Int!, $ticketId: Int!) {
              ticket: deleteTicket(trackerId: $trackerId, ticketId: $ticketId) {
                id
              }
            }
            """)
        variables: dict[str, object] = {"trackerId": tracker_id, "ticketId": ticket_id}
        response = self.execute(
            query=query, operation_name="DeleteTicket", variables=variables, **kwargs
        )
        data = self.get_data(response)
        return DeleteTicket.model_validate(data)

    def label_ticket(
        self, tracker_id: int, ticket_id: int, label_id: int, **kwargs: Any
    ) -> LabelTicket:
        query = gql("""
            mutation LabelTicket($trackerId: Int!, $ticketId: Int!, $labelId: Int!) {
              labelTicket(trackerId: $trackerId, ticketId: $ticketId, labelId: $labelId) {
                id
              }
            }
            """)
        variables: dict[str, object] = {
            "trackerId": tracker_id,
            "ticketId": ticket_id,
            "labelId": label_id,
        }
        response = self.execute(
            query=query, operation_name="LabelTicket", variables=variables, **kwargs
        )
        data = self.get_data(response)
        return LabelTicket.model_validate(data)

    def unlabel_ticket(
        self, tracker_id: int, ticket_id: int, label_id: int, **kwargs: Any
    ) -> UnlabelTicket:
        query = gql("""
            mutation UnlabelTicket($trackerId: Int!, $ticketId: Int!, $labelId: Int!) {
              unlabelTicket(trackerId: $trackerId, ticketId: $ticketId, labelId: $labelId) {
                id
              }
            }
            """)
        variables: dict[str, object] = {
            "trackerId": tracker_id,
            "ticketId": ticket_id,
            "labelId": label_id,
        }
        response = self.execute(
            query=query, operation_name="UnlabelTicket", variables=variables, **kwargs
        )
        data = self.get_data(response)
        return UnlabelTicket.model_validate(data)

    def assign_user(
        self, tracker_id: int, ticket_id: int, user_id: int, **kwargs: Any
    ) -> AssignUser:
        query = gql("""
            mutation AssignUser($trackerId: Int!, $ticketId: Int!, $userId: Int!) {
              assignUser(trackerId: $trackerId, ticketId: $ticketId, userId: $userId) {
                id
              }
            }
            """)
        variables: dict[str, object] = {
            "trackerId": tracker_id,
            "ticketId": ticket_id,
            "userId": user_id,
        }
        response = self.execute(
            query=query, operation_name="AssignUser", variables=variables, **kwargs
        )
        data = self.get_data(response)
        return AssignUser.model_validate(data)

    def unassign_user(
        self, tracker_id: int, ticket_id: int, user_id: int, **kwargs: Any
    ) -> UnassignUser:
        query = gql("""
            mutation UnassignUser($trackerId: Int!, $ticketId: Int!, $userId: Int!) {
              unassignUser(trackerId: $trackerId, ticketId: $ticketId, userId: $userId) {
                id
              }
            }
            """)
        variables: dict[str, object] = {
            "trackerId": tracker_id,
            "ticketId": ticket_id,
            "userId": user_id,
        }
        response = self.execute(
            query=query, operation_name="UnassignUser", variables=variables, **kwargs
        )
        data = self.get_data(response)
        return UnassignUser.model_validate(data)

    def create_tracker(
        self, name: str, description: str, visibility: Visibility, **kwargs: Any
    ) -> CreateTracker:
        query = gql("""
            mutation CreateTracker($name: String!, $description: String!, $visibility: Visibility!) {
              tracker: createTracker(
                name: $name
                description: $description
                visibility: $visibility
              ) {
                name
                owner {
                  __typename
                  canonicalName
                }
              }
            }
            """)
        variables: dict[str, object] = {
            "name": name,
            "description": description,
            "visibility": visibility,
        }
        response = self.execute(
            query=query, operation_name="CreateTracker", variables=variables, **kwargs
        )
        data = self.get_data(response)
        return CreateTracker.model_validate(data)

    def submit_ticket(
        self, tracker_id: int, input: SubmitTicketInput, **kwargs: Any
    ) -> SubmitTicket:
        query = gql("""
            mutation SubmitTicket($trackerId: Int!, $input: SubmitTicketInput!) {
              ticket: submitTicket(trackerId: $trackerId, input: $input) {
                id
                tracker {
                  owner {
                    __typename
                    canonicalName
                  }
                  name
                }
              }
            }
            """)
        variables: dict[str, object] = {"trackerId": tracker_id, "input": input}
        response = self.execute(
            query=query, operation_name="SubmitTicket", variables=variables, **kwargs
        )
        data = self.get_data(response)
        return SubmitTicket.model_validate(data)

    def create_label(
        self,
        tracker_id: int,
        name: str,
        foreground_color: str,
        background_color: str,
        **kwargs: Any
    ) -> CreateLabel:
        query = gql("""
            mutation CreateLabel($trackerId: Int!, $name: String!, $foregroundColor: String!, $backgroundColor: String!) {
              createLabel(
                trackerId: $trackerId
                name: $name
                foregroundColor: $foregroundColor
                backgroundColor: $backgroundColor
              ) {
                id
              }
            }
            """)
        variables: dict[str, object] = {
            "trackerId": tracker_id,
            "name": name,
            "foregroundColor": foreground_color,
            "backgroundColor": background_color,
        }
        response = self.execute(
            query=query, operation_name="CreateLabel", variables=variables, **kwargs
        )
        data = self.get_data(response)
        return CreateLabel.model_validate(data)

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

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

    submit_ticket_via_email_query = "mutation SubmitTicketViaEmail($trackerId: Int!, $input: SubmitTicketEmailInput!) {\n  ticket: submitTicketEmail(trackerId: $trackerId, input: $input) {\n    id\n  }\n}"
    submit_comment_via_email_query = "mutation SubmitCommentViaEmail($trackerId: Int!, $ticketId: Int!, $input: SubmitCommentEmailInput!) {\n  event: submitCommentEmail(\n    trackerId: $trackerId\n    ticketId: $ticketId\n    input: $input\n  ) {\n    id\n  }\n}"
    update_tracker_query = "mutation UpdateTracker($id: Int!, $input: TrackerInput!) {\n  tracker: updateTracker(id: $id, input: $input) {\n    name\n    owner {\n      __typename\n      canonicalName\n    }\n  }\n}"
    rename_tracker_query = "mutation RenameTracker($id: Int!, $name: String!) {\n  updateTracker(id: $id, input: {name: $name}) {\n    name\n  }\n}"
    update_tracker_access_query = "mutation UpdateTrackerAccess($id: Int!, $input: ACLInput!) {\n  updateTrackerACL(trackerId: $id, input: $input) {\n    browse\n  }\n}"
    delete_tracker_query = "mutation DeleteTracker($id: Int!) {\n  tracker: deleteTracker(id: $id) {\n    id\n    name\n  }\n}"
    import_tracker_dump_query = "mutation ImportTrackerDump($trackerId: Int!, $dump: Upload!) {\n  importTrackerDump(trackerId: $trackerId, dump: $dump)\n}"
    submit_comment_query = "mutation SubmitComment($trackerId: Int!, $ticketId: Int!, $input: SubmitCommentInput!) {\n  event: submitComment(trackerId: $trackerId, ticketId: $ticketId, input: $input) {\n    id\n  }\n}"
    update_ticket_query = "mutation UpdateTicket($trackerId: Int!, $ticketId: Int!, $input: UpdateTicketInput!) {\n  ticket: updateTicket(trackerId: $trackerId, ticketId: $ticketId, input: $input) {\n    id\n  }\n}"
    delete_ticket_query = "mutation DeleteTicket($trackerId: Int!, $ticketId: Int!) {\n  ticket: deleteTicket(trackerId: $trackerId, ticketId: $ticketId) {\n    id\n  }\n}"
    label_ticket_query = "mutation LabelTicket($trackerId: Int!, $ticketId: Int!, $labelId: Int!) {\n  labelTicket(trackerId: $trackerId, ticketId: $ticketId, labelId: $labelId) {\n    id\n  }\n}"
    unlabel_ticket_query = "mutation UnlabelTicket($trackerId: Int!, $ticketId: Int!, $labelId: Int!) {\n  unlabelTicket(trackerId: $trackerId, ticketId: $ticketId, labelId: $labelId) {\n    id\n  }\n}"
    assign_user_query = "mutation AssignUser($trackerId: Int!, $ticketId: Int!, $userId: Int!) {\n  assignUser(trackerId: $trackerId, ticketId: $ticketId, userId: $userId) {\n    id\n  }\n}"
    unassign_user_query = "mutation UnassignUser($trackerId: Int!, $ticketId: Int!, $userId: Int!) {\n  unassignUser(trackerId: $trackerId, ticketId: $ticketId, userId: $userId) {\n    id\n  }\n}"
    create_tracker_query = "mutation CreateTracker($name: String!, $description: String!, $visibility: Visibility!) {\n  tracker: createTracker(\n    name: $name\n    description: $description\n    visibility: $visibility\n  ) {\n    name\n    owner {\n      __typename\n      canonicalName\n    }\n  }\n}"
    submit_ticket_query = "mutation SubmitTicket($trackerId: Int!, $input: SubmitTicketInput!) {\n  ticket: submitTicket(trackerId: $trackerId, input: $input) {\n    id\n    tracker {\n      owner {\n        __typename\n        canonicalName\n      }\n      name\n    }\n  }\n}"
    create_label_query = "mutation CreateLabel($trackerId: Int!, $name: String!, $foregroundColor: String!, $backgroundColor: String!) {\n  createLabel(\n    trackerId: $trackerId\n    name: $name\n    foregroundColor: $foregroundColor\n    backgroundColor: $backgroundColor\n  ) {\n    id\n  }\n}"
    update_label_query = "mutation UpdateLabel($id: Int!, $input: UpdateLabelInput!) {\n  updateLabel(id: $id, input: $input) {\n    id\n  }\n}"
    delete_label_query = (
        "mutation DeleteLabel($id: Int!) {\n  deleteLabel(id: $id) {\n    id\n  }\n}"
    )
