import gql from 'graphql-tag'

import { client } from '@operato/graphql'
import { gqlBuilder } from '@things-factory/utils'

export async function fetchBoardList(listParam = {}) {
  const response = await client.query({
    query: gql`
      {
        boards(${gqlBuilder.buildArgs(listParam)}) {
          items {
            id
            name
            description
            thumbnail
            state
            version
            createdAt
            updatedAt
          }
          total
        }
      }
    `
  })

  return response.data
}

export async function fetchBoard(id) {
  const response = await client.query({
    query: gql`
      query FetchBoardById($id: String!) {
        board(id: $id) {
          id
          name
          description
          group {
            id
            name
          }
          thumbnail
          model
          createdAt
          creator {
            id
            name
          }
          state
          version
          updatedAt
          updater {
            id
            name
          }
        }
      }
    `,
    variables: { id }
  })

  return response.data
}

export async function createBoard(board) {
  /*
    input NewBoard {
      name        : String!
      description : String
      model       : String!
      groupId     : String!
    }
  */

  board.model = JSON.stringify(board.model)

  const response = await client.mutate({
    mutation: gql`
      mutation CreateBoard($board: NewBoard!) {
        createBoard(board: $board) {
          id
          name
          description
          model
          state
          version
          createdAt
          updatedAt
        }
      }
    `,
    variables: {
      board
    }
  })

  return response.data
}

export async function cloneBoard({
  id,
  name,
  description,
  targetSubdomain,
  targetGroupId
}: {
  id: string
  name: string
  description: string
  targetSubdomain: string
  targetGroupId: string
}) {
  const response = await client.mutate({
    mutation: gql`
      mutation CloneBoard($id: String!, $patch: BoardPatch!, $targetSubdomain: String!, $targetGroupId: String) {
        cloneBoard(id: $id, patch: $patch, targetSubdomain: $targetSubdomain, targetGroupId: $targetGroupId) {
          id
          name
          description
          model
          group {
            id
            name
          }
          state
          version
          createdAt
          updatedAt
        }
      }
    `,
    variables: {
      id,
      patch: { name, description },
      targetSubdomain,
      targetGroupId
    }
  })

  return response.data
}

export async function releaseBoard(board) {
  var { id } = board

  const response = await client.mutate({
    mutation: gql`
      mutation ReleaseBoard($id: String!) {
        releaseBoard(id: $id) {
          id
          name
          description
          model
          group {
            id
            name
          }
          state
          version
          createdAt
          updatedAt
        }
      }
    `,
    variables: {
      id
    }
  })

  return response.data
}

export async function revertBoardVersion(id, version) {
  const response = await client.mutate({
    mutation: gql`
      mutation RevertBoardVersion($id: String!, $version: Float!) {
        revertBoardVersion(id: $id, version: $version) {
          name
        }
      }
    `,
    variables: {
      id,
      version
    }
  })

  return response.data.revertBoardVersion
}

export async function updateBoard(board) {
  /*
    input BoardPatch {
      name        : String
      description : String
      model       : String
    }
    */
  var { id, name, description, model, groupId } = board
  model = JSON.stringify(model)

  const response = await client.mutate({
    mutation: gql`
      mutation UpdateBoard($id: String!, $patch: BoardPatch!) {
        updateBoard(id: $id, patch: $patch) {
          id
          name
          description
          model
          group {
            id
            name
          }
          state
          version
          createdAt
          updatedAt
        }
      }
    `,
    variables: {
      id,
      patch: { name, description, model, groupId }
    }
  })

  return response.data
}

export async function deleteBoard(id) {
  const response = await client.mutate({
    mutation: gql`
      mutation ($id: String!) {
        deleteBoard(id: $id)
      }
    `,
    variables: {
      id
    }
  })

  return response.data
}

export async function importBoards({
  groupId,
  files,
  overwrite
}: {
  groupId: string
  files: File[]
  overwrite: boolean
}) {
  const response = await client.mutate({
    mutation: gql`
      mutation importBoards($groupId: String!, $files: [Upload!]!, $overwrite: Boolean!) {
        importBoards(groupId: $groupId, files: $files, overwrite: $overwrite) {
          id
          name
          description
        }
      }
    `,
    variables: {
      groupId,
      files,
      overwrite
    },
    context: { hasUpload: true }
  })

  return response.data
}
