import type {
  DeleteNachhilfeangebotMutation,
  DeleteNachhilfeangebotMutationVariables,
  FindNachhilfeangebots,
} from 'types/graphql'

import { Link, routes } from '@redwoodjs/router'
import { useMutation } from '@redwoodjs/web'
import type { TypedDocumentNode } from '@redwoodjs/web'
import { toast } from '@redwoodjs/web/toast'

import { QUERY } from 'src/components/Nachhilfeangebot/NachhilfeangebotsCell'
import { truncate } from 'src/lib/formatters'

const DELETE_NACHHILFEANGEBOT_MUTATION: TypedDocumentNode<
  DeleteNachhilfeangebotMutation,
  DeleteNachhilfeangebotMutationVariables
> = gql`
  mutation DeleteNachhilfeangebotMutation($id: Int!) {
    deleteNachhilfeangebot(id: $id) {
      id
    }
  }
`

const NachhilfeangebotsList = ({
  nachhilfeangebots,
}: FindNachhilfeangebots) => {
  const [deleteNachhilfeangebot] = useMutation(
    DELETE_NACHHILFEANGEBOT_MUTATION,
    {
      onCompleted: () => {
        toast.success('Nachhilfeangebot deleted')
      },
      onError: (error) => {
        toast.error(error.message)
      },
      // This refetches the query on the list page. Read more about other ways to
      // update the cache over here:
      // https://www.apollographql.com/docs/react/data/mutations/#making-all-other-cache-updates
      refetchQueries: [{ query: QUERY }],
      awaitRefetchQueries: true,
    }
  )

  const onDeleteClick = (id: DeleteNachhilfeangebotMutationVariables['id']) => {
    if (
      confirm('Are you sure you want to delete nachhilfeangebot ' + id + '?')
    ) {
      deleteNachhilfeangebot({ variables: { id } })
    }
  }

  return (
    <div className="rw-segment rw-table-wrapper-responsive">
      <table className="rw-table">
        <thead>
          <tr>
            <th>Id</th>
            <th>Subject</th>
            <th>Current class</th>
            <th>Cost</th>
            <th>&nbsp;</th>
          </tr>
        </thead>
        <tbody>
          {nachhilfeangebots.map((nachhilfeangebot) => (
            <tr key={nachhilfeangebot.id}>
              <td>{truncate(nachhilfeangebot.id)}</td>
              <td>{truncate(nachhilfeangebot.subject)}</td>
              <td>{truncate(nachhilfeangebot.currentClass)}</td>
              <td>{truncate(nachhilfeangebot.cost)}</td>
              <td>
                <nav className="rw-table-actions">
                  <Link
                    to={routes.nachhilfeangebot({ id: nachhilfeangebot.id })}
                    title={
                      'Show nachhilfeangebot ' + nachhilfeangebot.id + ' detail'
                    }
                    className="rw-button rw-button-small"
                  >
                    Show
                  </Link>
                  <Link
                    to={routes.editNachhilfeangebot({
                      id: nachhilfeangebot.id,
                    })}
                    title={'Edit nachhilfeangebot ' + nachhilfeangebot.id}
                    className="rw-button rw-button-small rw-button-blue"
                  >
                    Edit
                  </Link>
                  <button
                    type="button"
                    title={'Delete nachhilfeangebot ' + nachhilfeangebot.id}
                    className="rw-button rw-button-small rw-button-red"
                    onClick={() => onDeleteClick(nachhilfeangebot.id)}
                  >
                    Delete
                  </button>
                </nav>
              </td>
            </tr>
          ))}
        </tbody>
      </table>
    </div>
  )
}

export default NachhilfeangebotsList
