<script setup lang="ts">
import { useBookRecommendationStore } from '~/stores/book-recommendations'
import DeleteBookRecommendation from '~/components/modal/DeleteBookRecommendation.vue'
import EditBookRecommendation from '~/components/modal/EditBookRecommendation.vue'
import NewBookRecommendation from '~/components/modal/NewBookRecommendation.vue'
import CastVote from '~/components/modal/CastVote.vue'

const dayjs = useDayjs()
const { $echo } = useNuxtApp()
const authStore = useAuthStore()

const bookRecommendationStore = useBookRecommendationStore()
bookRecommendationStore.fetchRecommendations()

const columns = [
  {
    key: 'book_name',
    label: 'Name',
    sortable: true,
  },
  {
    key: 'author',
    label: 'Autor',
    sortable: true,
  },
  {
    key: 'published_at',
    label: 'Erstveröffentlichung',
    sortable: true,
  },
  {
    key: 'description',
    label: 'Beschreibung',
  },
  {
    key: 'pages',
    label: 'Seiten',
    sortable: true,
  },
  {
    key: 'recommender.name',
    label: 'Empfohlen von',
    sortable: true,
  },
  {
    key: 'created_at',
    label: 'Erstellt am',
    sortable: true,
  },
  {
    key: 'status',
    label: 'Status',
    sortable: true,
  },
  {
    key: 'votes',
    label: 'Votes',
    sortable: true,
  },
  {
    key: 'actions',
    label: '',
  },
]
const sort = ref({
  column: 'votes',
  direction: 'desc',
})

function resolveStatus(status: string) {
  return bookRecommendationStore.statusOptions.find(option => option.value === status)
}

const rows = computed(() => {
  // return all bookRecommendationStore.recommendations but add actions
  return bookRecommendationStore.recommendations.map((recommendation) => {
    return {
      ...recommendation,
      actions: {
        class: 'max-w-96',
      },
    }
  })
})

onMounted(() => {
  $echo.private(`BookRecommendation`)
    .listen('.BookRecommendationUpdated', (e) => {
      bookRecommendationStore.updateRecommendationWS(e.bookRecommendation)
    })
    .listen('.BookRecommendationDeleted', (e) => {
      bookRecommendationStore.deleteRecommendationWS(e.bookRecommendation)
    })
    .listen('.BookRecommendationCreated', (e) => {
      bookRecommendationStore.createRecommendationWS(e.bookRecommendation)
    })
  authStore.socketId = $echo.socketId()

  $echo.private(`Vote`)
    .listen('.VoteCreated', (e) => {
      bookRecommendationStore.createVoteWS(e.vote)
    })
})
</script>

<template>
  <div>
    <NewBookRecommendation />
    <UTable :ui="{ td: { base: 'max-w-[0] truncate' } }" :sort="sort" :loading="bookRecommendationStore.fetchRecommendationsStatus === 'pending'" :columns="columns" :rows="rows">
      <template #created_at-data="{ row }">
        <div>{{ dayjs(row.created_at).format('DD.MM.YYYY') }}</div>
      </template>
      <template #published_at-data="{ row }">
        <div>{{ dayjs(row.published_at).format('DD.MM.YYYY') }}</div>
      </template>
      <template #votes-data="{ row }">
        {{ row.votes.length }}
      </template>
      <template #status-data="{ row }">
        <UBadge :color="resolveStatus(row.status)?.color">
          {{ resolveStatus(row.status)?.name }}
        </UBadge>
      </template>
      <template #actions-data="{ row }">
        <div class="flex max-w-full space-x-2">
          <CastVote :row="row" />
          <EditBookRecommendation :row="row" />
          <DeleteBookRecommendation :row="row" />
        </div>
      </template>
    </UTable>
  </div>
</template>
