# Generated by ariadne-codegen
# Source: buildsrht/meta/

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 .base_client import BaseClient
from .can_submit_builds import CanSubmitBuilds


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


class MetaClient(BaseClient):
    def __init__(self, auth=None, headers=None, client=None, **kwargs):
        origin = get_api("meta.sr.ht")
        if auth is None:
            auth = InternalAuth()
        headers = {**auth.headers, "User-Agent": "meta.sr.ht frontend"}
        if has_request_context():
            headers["X-Forwarded-For"] = ", ".join(request.access_route)
        if not client:
            backend_timeout = cfg("meta.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 can_submit_builds(self, **kwargs: Any) -> CanSubmitBuilds:
        query = gql("""
            query CanSubmitBuilds {
              me {
                receivesPaidServices
              }
            }
            """)
        variables: dict[str, object] = {}
        response = self.execute(
            query=query, operation_name="CanSubmitBuilds", variables=variables, **kwargs
        )
        data = self.get_data(response)
        return CanSubmitBuilds.model_validate(data)

    can_submit_builds_query = (
        "query CanSubmitBuilds {\n  me {\n    receivesPaidServices\n  }\n}"
    )
