<script setup lang="ts">
import { useRouter } from 'vue-router'
import { computed } from 'vue'
import { useStore } from '~/store'
import { useI18n } from 'vue-i18n'
import { momentFormat } from '~/utils/filters'
import defaultCover from '~/assets/audio/default-cover.png'

import PlayButton from '~/components/audio/PlayButton.vue'
import Card from '~/components/ui/Card.vue'
import Link from '~/components/ui/Link.vue'
import Spacer from '~/components/ui/Spacer.vue'

import { type components } from '~/generated/types.ts'

type Album = components['schemas']['Album']

const play = defineEmit<[album: Album]>()

interface Props {
  album: Album;
}

const { t } = useI18n()

const props = defineProps<Props>()

const { album } = props

const artist_credit = album.artist_credit || []
const firstArtist = artist_credit.length > 0 ? artist_credit[0].artist : null

const store = useStore()
const imageUrl = computed(() => props.album.cover?.urls.original
  ? store.getters['instance/absoluteUrl'](props.album.cover?.urls.large_square_crop)
  : defaultCover
)
</script>

<template>
  <Card
    :title="album.title"
    :image="imageUrl"
    :tags="album.tags"
    :to="{name: 'library.albums.detail', params: {id: album.id}}"
    small
  >
    <template #topright>
      <PlayButton
        icon-only
        :is-playable="album.is_playable"
        :album="album"
      />
    </template>

    <template #default
      v-for="ac in album.artist_credit"
      :key="ac.artist.id"
    >
      <Link
        align-text="left"
        :to="{ name: 'library.artists.detail', params: { id: ac.artist.id }}"
      >
        {{ ac.credit ?? t('components.Queue.meta.unknownArtist') }}
      </Link>
      <span>{{ ac.joinphrase }}</span>
    </template>

    <template #footer>
      <span v-if="album.release_date">
        {{ momentFormat(new Date(album.release_date), 'Y') }}
      </span>
      <i class="bi bi-dot"/>
      <span>
        {{ t('components.audio.album.Card.meta.tracks', album.tracks_count) }}
      </span>
      <Spacer h grow />
      <PlayButton
        :dropdown-only="true"
        discrete
        :is-playable="album.is_playable"
        :album="album"
      />
    </template>
  </Card>
</template>

<style lang="scss" scoped>
.play-button {
  top: 16px;
  right: 16px;
}
</style>
