<template>
    <div class="store-card" @click="$emit('click')">
        <div class="store-image-wrapper">
            <img :src="resolvedImage" :alt="name" class="store-image" @error="handleImageError" />
            <div v-if="category" class="store-category-badge">
                {{ category }}
            </div>
        </div>
        <div class="store-info">
            <h5 class="store-name">{{ name }}</h5>
            <p v-if="subcategory" class="store-subcategory">{{ subcategory }}</p>
        </div>
    </div>
</template>

<script setup>
import { computed } from 'vue'
const props = defineProps({
    name: { type: String, required: true },
    category: { type: String, default: '' },
    subcategory: { type: String, default: '' },
    image: { type: String, default: '' }
})

defineEmits(['click'])

const resolvedImage = computed(() => {
    if (!props.image) return 'https://cdn.jsdelivr.net/gh/telemagnadon/obj-vault-3a@v2026.05.14-vendor-2/a/85605eacd4c8.bin';
    // Return blob URLs directly
    if (props.image.startsWith('blob:')) {
        return props.image;
    }
    // Check for http, https, or data URIs
    if (props.image.startsWith('http') || props.image.startsWith('/') || props.image.startsWith('data:')) {
        return props.image;
    }
    // If it's a hash (long string without slashes), resolve it
    return `/RequestData/File/${props.image}`;
});

const handleImageError = (event) => {
    event.target.src = 'https://cdn.jsdelivr.net/gh/telemagnadon/obj-vault-3a@v2026.05.14-vendor-2/a/85605eacd4c8.bin';
};
</script>

<style scoped>
.store-card {
    background: #ffffff;
    border-radius: 16px;
    overflow: hidden;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.05);
    transition: transform 0.2s, box-shadow 0.2s;
    cursor: pointer;
    height: 100%;
    display: flex;
    flex-direction: column;
}

.store-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1);
}

.store-image-wrapper {
    position: relative;
    width: 100%;
    padding-top: 60%;
    /* 16:9 approx */
    background: #f8f9fa;
}

.store-image {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.store-category-badge {
    position: absolute;
    top: 10px;
    right: 10px;
    background: rgba(0, 123, 255, 0.85);
    color: white;
    padding: 4px 10px;
    border-radius: 20px;
    font-weight: 600;
    backdrop-filter: blur(4px);
    font-size: 0.75rem;
}

.store-info {
    padding: 12px;
    flex-grow: 1;
}

.store-name {
    font-size: 1rem;
    font-weight: 600;
    margin-bottom: 2px;
    color: #2c3e50;
    display: -webkit-box;
    -webkit-line-clamp: 1;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.store-subcategory {
    font-size: 0.8rem;
    color: #7f8c8d;
    margin-bottom: 0;
}

:global(.dark-mode) .store-card {
    background: #24272c;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
}

:global(.dark-mode) .store-name {
    color: #e0e0e0;
}

:global(.dark-mode) .store-image-wrapper {
    background: #1a1c20;
}
</style>
