<template>
    <Teleport to="body">
        <Transition name="wlm-fade">
            <div v-if="open" class="wlm-overlay" @click.self="$emit('close')">
                <Transition name="wlm-panel">
                    <div v-if="open" class="wlm-panel">

                        <div class="wlm-header">
                            <div>
                                <h3 class="wlm-title">Delivery logs</h3>
                                <p class="wlm-subtitle" v-if="webhook">{{ webhook.name }}</p>
                            </div>
                            <button class="wlm-close" @click="$emit('close')" aria-label="Close">
                                <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12"/></svg>
                            </button>
                        </div>

                        <div class="wlm-body">
                            <p v-if="loading" class="wlm-state">Loading…</p>
                            <p v-else-if="!logs.length" class="wlm-state">No deliveries yet.</p>
                            <div v-else class="log-list">
                                <div v-for="log in logs" :key="log.id" class="log-row">
                                    <span class="log-status" :class="'log-status--' + log.status">{{ log.status }}</span>
                                    <div class="log-body">
                                        <div class="log-message">{{ log.message || '-' }}</div>
                                        <div class="log-meta">
                                            {{ formatDate(log.created_at) }}
                                            <template v-if="log.ip_address"> · {{ log.ip_address }}</template>
                                            <template v-if="log.contact"> · {{ log.contact.first_name }} {{ log.contact.last_name }} ({{ log.contact.email }})</template>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                    </div>
                </Transition>
            </div>
        </Transition>
    </Teleport>
</template>

<script setup>
import { ref, watch } from 'vue';
import { useTenantTimezone } from '@/Composables/useTenantTimezone';

const props = defineProps({
    open:    { type: Boolean, default: false },
    webhook: { type: Object,  default: null  },
});

defineEmits(['close']);

const { formatDateTime: formatDate } = useTenantTimezone();

const loading = ref(false);
const logs    = ref([]);

async function fetchLogs() {
    if (!props.webhook) return;
    loading.value = true;
    logs.value = [];
    try {
        const res = await window.axios.get(route('webhooks.logs', props.webhook.id));
        logs.value = res.data.data ?? [];
    } finally {
        loading.value = false;
    }
}

// Watch both open state and webhook object so logs always load on open
watch([() => props.open, () => props.webhook], ([open]) => {
    if (open && props.webhook) {
        fetchLogs();
    } else {
        logs.value = [];
    }
}, { immediate: true });
</script>

<style>
/* Overlay — not scoped so Teleport children inherit it */
.wlm-overlay {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.5);
    backdrop-filter: blur(3px);
    z-index: 9999;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

.wlm-panel {
    background: var(--bg-surface, #fff);
    border-radius: 14px;
    box-shadow: 0 24px 48px rgba(0,0,0,0.15), 0 0 0 1px rgba(0,0,0,0.06);
    width: 640px;
    max-width: 96vw;
    max-height: 80vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.wlm-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    padding: 18px 22px;
    border-bottom: 1px solid var(--border-color, #e5e7eb);
    flex-shrink: 0;
    gap: 12px;
}

.wlm-title {
    font-size: 16px;
    font-weight: 600;
    color: var(--text-primary, #111827);
    margin: 0;
}

.wlm-subtitle {
    font-size: 12px;
    color: var(--text-secondary, #6b7280);
    margin: 2px 0 0;
}

.wlm-close {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 30px;
    height: 30px;
    border: none;
    background: transparent;
    border-radius: 6px;
    color: var(--text-muted, #9ca3af);
    cursor: pointer;
    flex-shrink: 0;
    transition: background 0.15s, color 0.15s;
}
.wlm-close:hover { background: var(--bg-hover, #f3f4f6); color: var(--text-primary, #111827); }
.wlm-close svg { width: 18px; height: 18px; }

.wlm-body {
    padding: 16px 22px;
    overflow-y: auto;
    flex: 1;
}

.wlm-state {
    text-align: center;
    color: #9ca3af;
    font-size: 13px;
    padding: 32px 0;
    margin: 0;
}

/* ── Overlay fade ── */
.wlm-fade-enter-active  { transition: opacity 0.22s ease; }
.wlm-fade-leave-active  { transition: opacity 0.18s ease; }
.wlm-fade-enter-from,
.wlm-fade-leave-to      { opacity: 0; }

/* ── Panel spring ── */
.wlm-panel-enter-active { transition: transform 0.28s cubic-bezier(0.34, 1.56, 0.64, 1), opacity 0.22s ease; }
.wlm-panel-leave-active { transition: transform 0.18s ease, opacity 0.16s ease; }
.wlm-panel-enter-from   { transform: scale(0.93) translateY(16px); opacity: 0; }
.wlm-panel-leave-to     { transform: scale(0.96) translateY(8px);  opacity: 0; }

/* ── Log rows ── */
.log-list { display: flex; flex-direction: column; gap: 8px; }

.log-row {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    padding: 10px 12px;
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: 8px;
}

.log-status {
    flex-shrink: 0;
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
    padding: 2px 8px;
    border-radius: 20px;
}
.log-status--success  { background: #ecfdf5; color: #059669; }
.log-status--failed   { background: #fef2f2; color: #dc2626; }
.log-status--rejected { background: #fffbeb; color: #b45309; }

.log-body { flex: 1; min-width: 0; }
.log-message { font-size: 13px; color: var(--text-primary); font-weight: 500; }
.log-meta { font-size: 12px; color: #9ca3af; margin-top: 2px; }
</style>
