<template>
    <div class="handover-card" :class="{ collapsed: isCollapsed }">
        <div class="handover-card-header" @click="isCollapsed = !isCollapsed">
            <DocumentTextIcon class="handover-icon" />
            <span class="handover-label">{{ label }}</span>
            <div class="handover-actions" @click.stop>
                <button class="handover-copy-btn" @click="copy" :title="copied ? 'Copied!' : 'Copy'">
                    <CheckIcon v-if="copied" class="w-3.5 h-3.5" />
                    <ClipboardDocumentIcon v-else class="w-3.5 h-3.5" />
                </button>
            </div>
            <ChevronDownIcon class="chevron" :class="{ open: !isCollapsed }" />
        </div>
        <div v-if="!isCollapsed" class="handover-card-body markdown-body" v-html="parseMd(content)"></div>
    </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { DocumentTextIcon, ClipboardDocumentIcon, CheckIcon, ChevronDownIcon } from '@heroicons/vue/20/solid';
import { parseMd } from '../composables/useMessages';

const props = defineProps<{
    content: string;
    label?: string;
}>();

const isCollapsed = ref(true);
const copied = ref(false);

function copy() {
    navigator.clipboard.writeText(props.content).then(() => {
        copied.value = true;
        setTimeout(() => { copied.value = false; }, 1500);
    });
}
</script>

<style scoped>
.handover-card {
    align-self: flex-start;
    max-width: min(520px, 90%);
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface);
    overflow: hidden;
    margin: 0.25rem 0;
    /* subtle paper-like top highlight */
    box-shadow: 0 1px 0 rgba(255,255,255,0.04) inset, 0 1px 4px rgba(0,0,0,0.25);
}

.handover-card-header {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.45rem 0.75rem;
    cursor: pointer;
    user-select: none;
}
.handover-card-header:hover { background: rgba(255,255,255,0.03); }

.handover-icon {
    width: 14px;
    height: 14px;
    color: var(--text-dim);
    opacity: 0.7;
    flex-shrink: 0;
}

.handover-label {
    flex: 1;
    font-weight: 600;
    color: var(--text-dim);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.handover-actions {
    display: flex;
    align-items: center;
    gap: 0.25rem;
}

.handover-copy-btn {
    background: none;
    border: none;
    padding: 0.15rem 0.3rem;
    border-radius: 3px;
    cursor: pointer;
    color: var(--text-dim);
    opacity: 0.6;
    display: flex;
    align-items: center;
    transition: opacity 0.15s;
}
.handover-copy-btn:hover { opacity: 1; background: rgba(255,255,255,0.07); }
.handover-copy-btn svg { width: 13px; height: 13px; }

.chevron {
    width: 14px;
    height: 14px;
    color: var(--text-dim);
    opacity: 0.6;
    flex-shrink: 0;
    transition: transform 0.2s ease;
    transform: rotate(-90deg);
}
.chevron.open { transform: rotate(0deg); }

.handover-card-body {
    padding: 0.75rem 0.9rem;
    border-top: 1px solid var(--border);
    line-height: 1.6;
    color: var(--text);
    background: rgba(255,255,255,0.015);
    overflow-y: auto;
    max-height: 420px;
}

/* Markdown styles */
.handover-card-body :deep(p) { margin: 0.35rem 0; }
.handover-card-body :deep(h1),
.handover-card-body :deep(h2),
.handover-card-body :deep(h3) {
    margin: 0.7rem 0 0.3rem;
    color: var(--text);
    font-weight: 700;
}
.handover-card-body :deep(h1) { }
.handover-card-body :deep(ul),
.handover-card-body :deep(ol) {
    padding-left: 1.25rem;
    margin: 0.3rem 0;
    list-style-position: outside;
}
.handover-card-body :deep(li) { margin: 0.25rem 0; line-height: 1.5; }
.handover-card-body :deep(code) {
    background: rgba(255,255,255,0.08);
    padding: 0.1rem 0.3rem;
    border-radius: 3px;
    font-family: var(--font-mono);
}
.handover-card-body :deep(pre) {
    background: rgba(0,0,0,0.35);
    padding: 0.6rem 0.75rem;
    border-radius: 5px;
    overflow-x: auto;
    margin: 0.4rem 0;
}
.handover-card-body :deep(pre code) { background: none; padding: 0; }
.handover-card-body :deep(strong) { color: var(--text); font-weight: 600; }
.handover-card-body :deep(a) { color: var(--accent); text-decoration: underline; }
.handover-card-body :deep(hr) {
    border: none;
    border-top: 1px solid var(--border);
    margin: 0.6rem 0;
}
</style>
