import gql from 'graphql-tag'
import client from './fetch-client'
import { setRoute } from './actions-route'
import { fetchBoardList } from './actions-board'

export const updateGroup = group => async dispatch => {
  try {
    var { id, name, description } = group

    const response = await client.mutate({
      mutation: gql`
        mutation UpdateGroup($id: String!, $patch: GroupPatch!) {
          updateGroup(id: $id, patch: $patch) {
            id
            name
            description
            createdAt
            updatedAt
          }
        }
      `,
      variables: {
        id,
        patch: { name, description }
      }
    })

    dispatch(fetchGroupList())
  } catch (error) {
    console.error(error)
    /* TODO error */
  }
}

export const deleteGroup = group => async dispatch => {
  try {
    var { id } = group

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

    dispatch(fetchGroupList())
  } catch (error) {
    console.error(error)
    /* TODO error */
  }
}
export const fetchGroupList = () => async dispatch => {
  try {
    const response = await client.query({
      query: gql`
        {
          groups {
            id
            name
            description
            createdAt
            updatedAt
          }
        }
      `
    })

    dispatch({
      type: 'GROUP-LIST',
      list: response.data.groups
    })
  } catch (error) {
    dispatch({
      type: 'CLEAR-GROUP-LIST'
    })
  }
}

export const createGroup = group => async dispatch => {
  try {
    const response = await client.mutate({
      mutation: gql`
        mutation CreateGroup($group: NewGroup!) {
          createGroup(group: $group) {
            id
            name
            description
            createdAt
            updatedAt
          }
        }
      `,
      variables: { group }
    })

    dispatch(fetchGroupList())
    dispatch(fetchBoardList('group', response.data.createGroup.id))
  } catch (error) {
    console.error(error)
  }
}

export const joinGroup = (boardId, group) => async dispatch => {
  try {
    var { id } = group

    const response = await client.mutate({
      mutation: gql`
        mutation JoinGroup($id: String!, $boardIds: [String]!) {
          joinGroup(id: $id, boardIds: $boardIds) {
            id
            name
            description
            boards {
              id
              name
              description
              createdAt
              updatedAt
            }
            createdAt
            updatedAt
          }
        }
      `,
      variables: {
        id,
        boardIds: [boardId]
      }
    })

    dispatch(setRoute('list-by-group', id))

    dispatch({
      type: 'BOARD-LIST',
      group: id,
      list: response.data.joinGroup.boards
    })
  } catch (error) {
    console.error(error)
    /* TODO error */
  }
}
