<template>
  <article class="board-post">
    <header class="post-header">
      <span class="type-indicator" :class="typeClass">{{ typeLabel }}</span>
      <div v-if="editable" class="post-actions">
        <button type="button" class="action-btn" @click="$emit('edit', post)">Edit</button>
        <button type="button" class="action-btn danger" @click="$emit('delete', post)">Delete</button>
      </div>
    </header>

    <h3 class="post-title">{{ post.title }}</h3>

    <div v-if="post.seeking" class="post-block">
      <div class="block-label">Seeking</div>
      <p class="block-text">{{ post.seeking }}</p>
    </div>

    <div v-if="post.offering" class="post-block">
      <div class="block-label">Offering</div>
      <p class="block-text">{{ post.offering }}</p>
    </div>

    <p v-if="post.note" class="post-note">{{ post.note }}</p>

    <div v-if="post.tags && post.tags.length" class="post-tags">
      <span v-for="tag in post.tags" :key="tag" class="tag-pill">{{ tag }}</span>
    </div>

    <footer class="post-footer">
      <div class="author">
        <img
          v-if="post.author && post.author.avatar"
          :src="post.author.avatar"
          :alt="post.author.name"
          class="author-avatar"
        >
        <span v-else class="author-avatar avatar-placeholder" />
        <span class="author-name">{{ post.author ? post.author.name : 'Unknown' }}</span>
        <CircleBadge
          v-if="post.author && post.author.circle"
          :circle="post.author.circle"
        />
      </div>
      <a
        v-if="slackLink"
        :href="slackLink"
        target="_blank"
        rel="noopener"
        class="slack-link"
      >Discuss on Slack &rarr;</a>
    </footer>
  </article>
</template>

<script setup>
const props = defineProps({
  post: { type: Object, required: true },
  channels: { type: Array, default: () => [] },
  editable: { type: Boolean, default: false },
})

defineEmits(['edit', 'delete'])

const hasSeeking = computed(() => !!(props.post.seeking && props.post.seeking.trim()))
const hasOffering = computed(() => !!(props.post.offering && props.post.offering.trim()))

const typeLabel = computed(() => {
  if (hasSeeking.value && hasOffering.value) return 'SEEKING + OFFERING'
  if (hasSeeking.value) return 'SEEKING'
  if (hasOffering.value) return 'OFFERING'
  return ''
})

const typeClass = computed(() => {
  if (hasSeeking.value && hasOffering.value) return 'type-both'
  if (hasSeeking.value) return 'type-seeking'
  if (hasOffering.value) return 'type-offering'
  return ''
})

const slackLink = computed(() => {
  const postTags = props.post.tags || []
  if (!postTags.length) return null
  const channel = props.channels.find(c => {
    const slugs = c.tagSlugs || []
    return slugs.some(s => postTags.includes(s))
  })
  if (!channel || !channel.slackChannelId) return null
  return `https://gammaspace.slack.com/archives/${channel.slackChannelId}`
})
</script>

<style scoped>
.board-post {
  border: 1px dashed var(--border);
  padding: 18px 22px;
  transition: background 0.15s, border-color 0.15s;
}
.board-post:hover {
  background: var(--surface);
  border-color: var(--border-d);
}

.post-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 10px;
}

.type-indicator {
  display: inline-block;
  font-size: 10px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  padding: 2px 8px;
  border: 1px dashed;
  font-family: "Commit Mono", monospace;
}
.type-indicator.type-seeking {
  color: var(--candle);
  border-color: var(--candle-faint);
}
.type-indicator.type-offering {
  color: var(--green);
  border-color: var(--green);
}
.type-indicator.type-both {
  color: var(--ember);
  border-color: var(--ember);
}

.post-actions {
  display: flex;
  gap: 6px;
}
.action-btn {
  font-family: "Commit Mono", monospace;
  font-size: 10px;
  letter-spacing: 0.04em;
  padding: 3px 9px;
  border: 1px dashed var(--border);
  background: transparent;
  color: var(--text-dim);
  cursor: pointer;
  transition: all 0.12s;
}
.action-btn:hover {
  color: var(--text-bright);
  border-color: var(--border-d);
}
.action-btn.danger:hover {
  color: var(--ember);
  border-color: var(--ember);
}

.post-title {
  font-family: "Brygada 1918", serif;
  font-size: 20px;
  font-weight: 500;
  color: var(--text-bright);
  margin-bottom: 10px;
  line-height: 1.25;
}

.post-block {
  margin-bottom: 10px;
}
.block-label {
  font-size: 10px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text-faint);
  margin-bottom: 2px;
}
.block-text {
  font-size: 13px;
  color: var(--text);
  white-space: pre-wrap;
}

.post-note {
  font-size: 11px;
  color: var(--text-faint);
  font-style: italic;
  margin: 8px 0;
  white-space: pre-wrap;
}

.post-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin: 10px 0;
}
.tag-pill {
  display: inline-block;
  font-size: 10px;
  font-family: "Commit Mono", monospace;
  color: var(--text-dim);
  padding: 2px 8px;
  border: 1px dashed var(--border);
}

.post-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-top: 14px;
  padding-top: 10px;
  border-top: 1px dashed var(--border);
}

.author {
  display: flex;
  align-items: center;
  gap: 8px;
}
.author-avatar {
  width: 20px;
  height: 20px;
  object-fit: cover;
  border: 1px dashed var(--border);
}
.avatar-placeholder {
  background: var(--surface);
}
.author-name {
  font-size: 11px;
  color: var(--text-dim);
  font-family: "Commit Mono", monospace;
}

.slack-link {
  font-size: 11px;
  font-family: "Commit Mono", monospace;
  color: var(--candle);
  text-decoration: none;
  border-bottom: 1px dashed var(--candle-faint);
}
.slack-link:hover {
  color: var(--candle-dim);
  text-decoration: none;
  border-bottom-style: solid;
}
</style>
