<script setup lang="ts">
import { formatMoney } from '@/api/data'
import { getSelectedProfile } from '@/api/profile'
import type { TransactionsListItem } from '@/api/transaction'
import { useRoute, useRouter } from 'vue-router'
import CategoryLabel from './CategoryLabel.vue'
import { computed, type Ref } from 'vue'
import AppBadge from './common/AppBadge.vue'
import TagLabel from './TagLabel.vue'

const router = useRouter()
const route = useRoute()

type MoneyStyle = 'positive' | 'negative' | 'neutral'

const props = defineProps<{ tx: TransactionsListItem }>()
// Defines the style to use for money based on which accounts are involved.
const moneyStyle: Ref<MoneyStyle> = computed(() => {
  if (props.tx.debitedAccount !== null && props.tx.creditedAccount === null) {
    return 'positive'
  } else if (props.tx.creditedAccount !== null && props.tx.debitedAccount === null) {
    return 'negative'
  }
  return 'neutral'
})

function goToTransaction() {
  const profile = getSelectedProfile(route)
  router.push(`/profiles/${profile}/transactions/${props.tx.id}`)
}
</script>
<template>
  <div
    class="transaction-card"
    @click="goToTransaction()"
  >
    <div>
      <!-- Top row contains timestamp and amount. -->
      <div style="display: flex; justify-content: space-between">
        <div>
          <div class="font-mono font-size-xsmall text-normal">Transaction #{{ tx.id }}</div>
          <div class="text-muted font-mono font-size-xsmall">
            {{ new Date(tx.timestamp).toLocaleString() }}
          </div>
        </div>
        <div>
          <div
            class="font-mono align-right font-size-small"
            :class="{
              'text-positive': moneyStyle === 'positive',
              'text-negative': moneyStyle === 'negative',
            }"
          >
            {{ formatMoney(tx.amount, tx.currency) }}
          </div>
          <div
            v-if="tx.creditedAccount !== null"
            class="font-size-small text-muted"
          >
            Credited to <span class="text-normal font-bold">{{ tx.creditedAccount.name }}</span>
          </div>
          <div
            v-if="tx.debitedAccount !== null"
            class="font-size-small text-muted"
          >
            Debited to <span class="text-normal font-bold">{{ tx.debitedAccount.name }}</span>
          </div>
        </div>
      </div>

      <!-- Middle row contains the description. -->
      <div>
        <p class="transaction-card-description">{{ tx.description }}</p>
      </div>
    </div>

    <!-- Bottom row contains other links. -->
    <div style="display: flex; justify-content: space-between">
      <div>
        <CategoryLabel
          :category="tx.category"
          v-if="tx.category"
          style="margin-left: 0"
        />
        <AppBadge v-if="tx.vendor">{{ tx.vendor.name }}</AppBadge>
      </div>
      <div>
        <!-- Only show the first 3 tags, and add a "+N" badge for any more. -->
        <TagLabel
          v-for="tag in tx.tags.slice(0, 3)"
          :key="tag"
          :tag="tag"
        />
        <AppBadge
          v-if="tx.tags.length > 3"
          class="text-muted"
          >+{{ tx.tags.length - 3 }}</AppBadge
        >
      </div>
    </div>
  </div>
</template>
<style lang="css" scoped>
.transaction-card {
  background-color: var(--bg);
  padding: 0.5rem;
  border-radius: 0.5rem;
  margin: 0.5rem 0;
  cursor: pointer;
  height: 120px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.transaction-card:hover {
  background-color: var(--bg-darker);
}

.transaction-card-description {
  margin: 0.25rem 0;
  font-size: 0.9rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>
