<script setup lang="ts">
export interface IdeaItem {
  id: number
  author: string | null
  content: string
  platform: string | null
  color: string | null
  createdAt: string
}

const props = defineProps<{
  idea: IdeaItem
  index: number
  focused: boolean
}>()

const emit = defineEmits<{
  select: [id: number]
}>()

const cardColor = computed(() => props.idea.color || '#cc785c')

// Convert hex to lighter variant for background
function hexToRgba(hex: string, alpha: number): string {
  const r = parseInt(hex.slice(1, 3), 16)
  const g = parseInt(hex.slice(3, 5), 16)
  const b = parseInt(hex.slice(5, 7), 16)
  return `rgba(${r}, ${g}, ${b}, ${alpha})`
}

const bgColor = computed(() => hexToRgba(cardColor.value, 0.12))
const borderColor = computed(() => hexToRgba(cardColor.value, 0.3))
const accentDot = computed(() => cardColor.value)

// Random subtle rotation for sticky-note feel
const tilt = computed(() => {
  // Use id as seed for consistent rotation
  const seed = props.idea.id * 7 + 3
  return (seed % 5) - 2 // -2 to +2 degrees
})

const formattedDate = computed(() => {
  const d = new Date(props.idea.createdAt)
  return d.toLocaleDateString('zh-CN', { month: 'short', day: 'numeric' })
})
</script>

<template>
  <div
    :class="['idea-card', { 'idea-card--focused': focused }]"
    :style="{
      '--card-bg': bgColor,
      '--card-border': borderColor,
      '--card-accent': accentDot,
      '--card-tilt': tilt + 'deg',
      '--card-delay': index * 60 + 'ms',
    }"
    @click="emit('select', idea.id)"
  >
    <div class="card-inner">
      <div class="card-accent-dot" />
      <span v-if="idea.platform" class="card-platform">{{ idea.platform }}</span>
      <p class="card-content">{{ idea.content }}</p>
      <div class="card-footer">
        <span class="card-author">{{ idea.author || '匿名' }}</span>
        <span class="card-date">{{ formattedDate }}</span>
      </div>
    </div>
  </div>
</template>

<style scoped>
.idea-card {
  --card-bg: rgba(204, 120, 92, 0.12);
  --card-border: rgba(204, 120, 92, 0.3);
  --card-accent: #cc785c;
  --card-tilt: 0deg;
  --card-delay: 0ms;

  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-radius: 12px;
  cursor: pointer;
  transform: rotate(var(--card-tilt));
  transition: all 0.35s var(--ease-out-quart, cubic-bezier(0.25, 1, 0.5, 1));
  animation: card-enter 0.5s var(--ease-out-expo, cubic-bezier(0.16, 1, 0.3, 1)) both;
  animation-delay: var(--card-delay);
  position: relative;
  overflow: hidden;
}

.idea-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, var(--card-accent) 0%, transparent 60%);
  opacity: 0;
  transition: opacity 0.35s ease;
  border-radius: 12px;
  z-index: 0;
}

.idea-card:hover {
  transform: rotate(0deg) translateY(-4px) scale(1.03);
  border-color: var(--card-accent);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.08);
}

.idea-card:hover::before {
  opacity: 0.06;
}

.idea-card--focused {
  transform: rotate(0deg) scale(1.02);
  border-color: var(--card-accent);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.12);
  z-index: 10;
  background: var(--card-bg);
}

.idea-card--focused::before {
  opacity: 0.1;
}

.card-inner {
  position: relative;
  z-index: 1;
  padding: 20px;
  display: flex;
  flex-direction: column;
  min-height: 120px;
}

.card-accent-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--card-accent);
  margin-bottom: 12px;
  opacity: 0.8;
}

.card-platform {
  display: inline-block;
  align-self: flex-start;
  padding: 2px 10px;
  font-size: 11px;
  font-weight: 600;
  color: var(--card-accent);
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-radius: 12px;
  margin-bottom: 10px;
  letter-spacing: 0.02em;
}

.card-content {
  font-size: 15px;
  line-height: 1.6;
  color: var(--color-body, #3d3d3a);
  margin: 0 0 14px;
  flex: 1;
  display: -webkit-box;
  -webkit-line-clamp: 4;
  -webkit-box-orient: vertical;
  overflow: hidden;
  word-break: break-word;
}

.card-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
}

.card-author {
  color: var(--card-accent);
  font-weight: 600;
  letter-spacing: 0.02em;
}

.card-date {
  color: var(--color-muted-soft, #8e8b82);
}

@keyframes card-enter {
  from {
    opacity: 0;
    transform: rotate(var(--card-tilt)) translateY(20px) scale(0.95);
  }
  to {
    opacity: 1;
    transform: rotate(var(--card-tilt)) translateY(0) scale(1);
  }
}

@media (prefers-reduced-motion: reduce) {
  .idea-card {
    animation: none;
    transition: none;
  }
}
</style>
