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

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_group import CreateGroup
from .enums import Visibility
from .input_types import TriggerInput
from .submit_build import SubmitBuild


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


class BuildsClient(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: str,
        tags: Union[Optional[list[str]], UnsetType] = UNSET,
        execute: Union[Optional[bool], UnsetType] = UNSET,
        visibility: Union[Optional[Visibility], UnsetType] = UNSET,
        **kwargs: Any
    ) -> SubmitBuild:
        query = gql("""
            mutation SubmitBuild($manifest: String!, $note: String!, $tags: [String!], $execute: Boolean, $visibility: Visibility) {
              submit(
                manifest: $manifest
                note: $note
                tags: $tags
                execute: $execute
                visibility: $visibility
                secrets: false
              ) {
                id
              }
            }
            """)
        variables: dict[str, object] = {
            "manifest": manifest,
            "note": note,
            "tags": tags,
            "execute": execute,
            "visibility": visibility,
        }
        response = self.execute(
            query=query, operation_name="SubmitBuild", variables=variables, **kwargs
        )
        data = self.get_data(response)
        return SubmitBuild.model_validate(data)

    def create_group(
        self, jobs: list[int], triggers: list[TriggerInput], note: str, **kwargs: Any
    ) -> CreateGroup:
        query = gql("""
            mutation CreateGroup($jobs: [Int!]!, $triggers: [TriggerInput!]!, $note: String!) {
              createGroup(jobIds: $jobs, triggers: $triggers, note: $note) {
                id
              }
            }
            """)
        variables: dict[str, object] = {
            "jobs": jobs,
            "triggers": triggers,
            "note": note,
        }
        response = self.execute(
            query=query, operation_name="CreateGroup", variables=variables, **kwargs
        )
        data = self.get_data(response)
        return CreateGroup.model_validate(data)

    submit_build_query = "mutation SubmitBuild($manifest: String!, $note: String!, $tags: [String!], $execute: Boolean, $visibility: Visibility) {\n  submit(\n    manifest: $manifest\n    note: $note\n    tags: $tags\n    execute: $execute\n    visibility: $visibility\n    secrets: false\n  ) {\n    id\n  }\n}"
    create_group_query = "mutation CreateGroup($jobs: [Int!]!, $triggers: [TriggerInput!]!, $note: String!) {\n  createGroup(jobIds: $jobs, triggers: $triggers, note: $note) {\n    id\n  }\n}"
