"""GraphQL query and mutation templates.

Keeping these in one place makes schema changes easier to manage.
"""

from __future__ import annotations

from dataclasses import dataclass


@dataclass(frozen=True, slots=True)
class Query:
    """A GraphQL query/mutation and the variable names it expects."""

    text: str


ISSUE_BY_INFO = Query(
    text="""
    query getIssueInfo($repositoryGhId: Int!, $issueNumber: Int!) {
      issueByInfo(repositoryGhId: $repositoryGhId, issueNumber: $issueNumber) {
        id
        title
        number
        state
        htmlUrl
      }
    }
    """
)

UPDATE_ISSUE = Query(
    text="""
    mutation updateIssue($issueId: ID!, $title: String, $body: String) {
      updateIssue(input: { issueId: $issueId, title: $title, body: $body }) {
        issue { id title body }
      }
    }
    """
)

GET_PIPELINES = Query(
    text="""
    query getPipelinesForWorkspace($workspaceId: ID!, $first: Int!) {
      workspace(id: $workspaceId) {
        id
        pipelinesConnection(first: $first) {
          nodes { id name }
        }
      }
    }
    """
)

MOVE_ISSUE = Query(
    text="""
    mutation moveIssue($moveIssueInput: MoveIssueInput!, $workspaceId: ID!) {
      moveIssue(input: $moveIssueInput) {
        issue {
          id
          pipelineIssue(workspaceId: $workspaceId) { pipeline { id } }
        }
      }
    }
    """
)

SPRINT_ISSUES_PAGED = Query(
    text="""
    query SprintIssues($id: ID!, $after: String) {
      node(id: $id) {
        ... on Sprint {
          id
          name
          state
          startAt
          endAt
          sprintIssues(first: 100, after: $after) {
            pageInfo { hasNextPage endCursor }
            nodes {
              issue {
                id
                number
                title
                state
                estimate { value }
              }
            }
          }
        }
      }
    }
    """
)

SPRINT_ISSUES_UNPAGED = Query(
    text="""
    query SprintIssues($id: ID!) {
      node(id: $id) {
        ... on Sprint {
          id
          name
          state
          startAt
          endAt
          sprintIssues {
            nodes {
              issue {
                id
                number
                title
                state
                estimate { value }
              }
            }
          }
        }
      }
    }
    """
)

SPRINT_ISSUES_DETAILS_PAGED = Query(
    text="""
    query SprintIssues($id: ID!, $workspaceId: ID!, $after: String) {
      node(id: $id) {
        ... on Sprint {
          id
          name
          state
          startAt
          endAt
          sprintIssues(first: 100, after: $after) {
            pageInfo { hasNextPage endCursor }
            nodes {
              issue {
                id
                number
                title
                state
                htmlUrl
                repositoryGhId
                estimate { value }
                pipelineIssue(workspaceId: $workspaceId) { pipeline { id name } }
                labels(first: 50) { nodes { name } }
                assignees(first: 10) { nodes { login } }
              }
            }
          }
        }
      }
    }
    """
)

SPRINT_ISSUES_DETAILS_UNPAGED = Query(
    text="""
    query SprintIssues($id: ID!, $workspaceId: ID!) {
      node(id: $id) {
        ... on Sprint {
          id
          name
          state
          startAt
          endAt
          sprintIssues {
            nodes {
              issue {
                id
                number
                title
                state
                htmlUrl
                repositoryGhId
                estimate { value }
                pipelineIssue(workspaceId: $workspaceId) { pipeline { id name } }
                labels(first: 50) { nodes { name } }
                assignees(first: 10) { nodes { login } }
              }
            }
          }
        }
      }
    }
    """
)
