# Generated by ariadne-codegen
# Source: buildsrht/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
from .enums import Visibility
from .share_secret import ShareSecret
from .submit_build import SubmitBuild
from .update_job import UpdateJob


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


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

    def update_job(
        self,
        job_id: int,
        visibility: Visibility,
        tags: Union[Optional[list[str]], UnsetType] = UNSET,
        **kwargs: Any
    ) -> UpdateJob:
        query = gql("""
            mutation UpdateJob($jobId: Int!, $visibility: Visibility!, $tags: [String!]) {
              update(jobId: $jobId, visibility: $visibility, tags: $tags) {
                id
                owner {
                  __typename
                  ... on User {
                    username
                  }
                }
              }
            }
            """)
        variables: dict[str, object] = {
            "jobId": job_id,
            "visibility": visibility,
            "tags": tags,
        }
        response = self.execute(
            query=query, operation_name="UpdateJob", variables=variables, **kwargs
        )
        data = self.get_data(response)
        return UpdateJob.model_validate(data)

    def share_secret(self, uuid: str, target: str, **kwargs: Any) -> ShareSecret:
        query = gql("""
            mutation ShareSecret($uuid: String!, $target: String!) {
              secret: shareSecret(uuid: $uuid, user: $target) {
                __typename
                id
                name
                uuid
              }
            }
            """)
        variables: dict[str, object] = {"uuid": uuid, "target": target}
        response = self.execute(
            query=query, operation_name="ShareSecret", variables=variables, **kwargs
        )
        data = self.get_data(response)
        return ShareSecret.model_validate(data)

    submit_build_query = "mutation SubmitBuild($manifest: String!, $note: String, $visibility: Visibility, $tags: [String!]) {\n  job: submit(\n    manifest: $manifest\n    note: $note\n    visibility: $visibility\n    tags: $tags\n  ) {\n    id\n  }\n}"
    update_job_query = "mutation UpdateJob($jobId: Int!, $visibility: Visibility!, $tags: [String!]) {\n  update(jobId: $jobId, visibility: $visibility, tags: $tags) {\n    id\n    owner {\n      __typename\n      ... on User {\n        username\n      }\n    }\n  }\n}"
    share_secret_query = "mutation ShareSecret($uuid: String!, $target: String!) {\n  secret: shareSecret(uuid: $uuid, user: $target) {\n    __typename\n    id\n    name\n    uuid\n  }\n}"
