<script setup>
import { usePageTitle } from '../composables/Core/usePageTitle';
usePageTitle('View Store Market');

import { ref, computed, onMounted } from 'vue';
import axios from 'axios';
import { useNavigate } from '../composables/Core/useNavigate';
import LoadingSpinner from '../Components/LoadingSpinner.vue';
import ProductCard from '../Components/Market/ProductCard.vue';
import FileImage from '../Components/Core/FileImage.vue';
import { useAuth } from '../composables/Core/useAuth';
import { useModal } from '../composables/Core/useModal';
import BackButton from '../Components/Core/BackButton.vue';

const props = defineProps({
    target: { type: String, required: true }
});

const { navigate } = useNavigate();
const { isUltimate, hasRole, UserTypes } = useAuth();
const modal = useModal();

const store = ref(null);
const loading = ref(true);
const error = ref(null);
const showGallery = ref(false);
const galleryIndex = ref(0);

const isBig3 = computed(() => isUltimate.value || hasRole('super operator') || hasRole('operator'));

const photos = computed(() => store.value?.resolved_photos ?? []);
const hasPhotos = computed(() => photos.value.length > 0);

const fetchStoreDetails = async () => {
    loading.value = true;
    error.value = null;
    try {
        const response = await axios.post('/View/Store/Details/data', {
            target: props.target
        });
        if (response.data && response.data.success && response.data.data) {
            store.value = response.data.data;
        } else {
            error.value = 'Store not found';
        }
    } catch (e) {
        console.error('Failed to fetch store details:', e);
        error.value = 'Failed to load store details';
    } finally {
        loading.value = false;
    }
};

const goBack = () => {
    navigate({ page: 'ListStores' });
};

const viewProduct = (product) => {
    navigate({
        page: 'BuyViewProductMarket',
        props: {
            payload: { 
                product_hash: product.hashkey, 
                store_hash: props.target 
            }
        }
    });
};

const viewAllPhotos = () => {
    if (hasPhotos.value) {
        galleryIndex.value = 0;
        showGallery.value = true;
    }
};

const openGallery = (index = 0) => {
    if (hasPhotos.value) {
        galleryIndex.value = index;
        showGallery.value = true;
    }
};

const prevPhoto = () => {
    if (photos.value.length === 0) return;
    galleryIndex.value = (galleryIndex.value - 1 + photos.value.length) % photos.value.length;
};

const nextPhoto = () => {
    if (photos.value.length === 0) return;
    galleryIndex.value = (galleryIndex.value + 1) % photos.value.length;
};

const editStore = () => {
    navigate({
        page: 'EditStoreUltimate',
        props: { target: props.target }
    });
};

const navigateToPOS = () => {
    navigate({
        page: 'PosMain',
        props: { target: props.target }
    });
};

const navigateToPosHistory = () => {
    navigate({
        page: 'PosHistory',
        props: { 
            target: props.target,
            storeName: store.value?.name
        }
    });
};

const reportStore = () => {
    // Placeholder for report functionality
    modal.open({
        title: 'Report Store',
        body: `Reporting store ${store.value?.name}`
    });
};

const addProduct = () => {
    navigate({
        page: 'AddProductsToStore',
        props: { target: props.target }
    });
};

const assignProduct = () => {
    navigate({
        page: 'AddProductsToStore',
        props: { target: props.target }
    });
};

const toggleDescription = () => {
    const desc = document.querySelector('.description');
    if (desc) {
        desc.classList.toggle('text-truncate-3');
    }
};

onMounted(() => {
    fetchStoreDetails();
});
</script>

<template>
    <div class="store-details-page pb-5">
        <div v-if="loading" class="text-center py-5">
            <LoadingSpinner />
            <p class="mt-3 text-muted">Loading store...</p>
        </div>

        <div v-else-if="error" class="tf-container mt-5 text-center">
            <div class="alert alert-danger">{{ error }}</div>
            <BackButton to="ListStores" text="Go Back" />
        </div>

        <template v-else-if="store">
            <!-- Store Profile Header -->
            <div class="store-header shadow-sm">
                <div class="store-banner" @click="hasPhotos ? openGallery(0) : null" :style="{ cursor: hasPhotos ? 'pointer' : 'default' }">
                    <BackButton
                        to="ListStores"
                        className="banner-back-btn"
                    />
                    <FileImage
                        :src="hasPhotos ? photos[0] : 'https://cdn.jsdelivr.net/gh/telemagnadon/obj-vault-3a@v2026.05.14-vendor-2/a/85605eacd4c8.bin'"
                        class="banner-img"
                        fallback="https://cdn.jsdelivr.net/gh/telemagnadon/obj-vault-3a@v2026.05.14-vendor-2/a/85605eacd4c8.bin"
                    />
                </div>
                <div class="tf-container store-profile">
                    <div class="profile-card glass-card">
                        <div class="d-flex align-items-center">
                             <div class="store-avatar shadow" @click="hasPhotos ? openGallery(0) : null" :style="{ cursor: hasPhotos ? 'pointer' : 'default' }">
                                <FileImage
                                    :src="hasPhotos ? photos[0] : 'https://cdn.jsdelivr.net/gh/telemagnadon/obj-vault-3a@v2026.05.14-vendor-2/a/85605eacd4c8.bin'"
                                    class="avatar-img"
                                    fallback="https://cdn.jsdelivr.net/gh/telemagnadon/obj-vault-3a@v2026.05.14-vendor-2/a/85605eacd4c8.bin"
                                />
                            </div>
                            <div class="ms-3 pt-2">
                                <h3 class="fw_7 mb-0">{{ store.name }}</h3>
                                <div class="d-flex align-items-center flex-wrap">
                                    <div class="text-muted small me-3">
                                        <i class="fas fa-map-marker-alt me-1"></i> {{ store.address }}
                                    </div>
                                    <div v-if="store.category" class="badge bg-soft-secondary px-2 py-1 rounded-pill text-secondary small me-2">
                                        {{ store.category }}
                                    </div>
                                    <div v-if="store.subcategory" class="badge bg-soft-info px-2 py-1 rounded-pill text-info small">
                                        {{ store.subcategory }}
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="mt-3 text-muted description text-truncate-3" id="storeDescription">
                            {{ store.description }}
                        </div>

                        <!-- Store Actions -->
                        <div class="store-actions mt-4 pt-3 border-top">
                            <div class="row g-2 justify-content-center">
                                <div class="col-6 col-sm-3">
                                    <button @click="toggleDescription" class="action-btn">
                                        <div class="icon-wrapper">
                                            <img src="https://cdn.jsdelivr.net/gh/telemagnadon/obj-vault-3a@v2026.05.14-vendor-2/a/a72b90763114.bin" alt="Details">
                                        </div>
                                        <span>Details</span>
                                    </button>
                                </div>
                                <div class="col-6 col-sm-3">
                                    <button @click="reportStore" class="action-btn">
                                        <div class="icon-wrapper">
                                            <img src="https://cdn.jsdelivr.net/gh/telemagnadon/obj-vault-3a@v2026.05.14-vendor-2/a/5b3227c8ca50.bin" alt="Report">
                                        </div>
                                        <span>Report</span>
                                    </button>
                                </div>
                                <div v-if="hasPhotos" class="col-6 col-sm-3">
                                    <button @click="openGallery(0)" class="action-btn photos-btn">
                                        <div class="icon-wrapper">
                                            <i class="fas fa-images" style="font-size: 22px; color: #8b5cf6;"></i>
                                        </div>
                                        <span>Photos</span>
                                    </button>
                                </div>
                                <div v-if="isBig3 || store.can_add_product" class="col-6 col-sm-3">
                                    <button @click="addProduct" class="action-btn add-product-btn">
                                        <div class="icon-wrapper">
                                            <img src="https://cdn.jsdelivr.net/gh/telemagnadon/obj-vault-3a@v2026.05.14-vendor-2/a/2596d5468e30.bin" alt="Add Product">
                                        </div>
                                        <span>Add Product</span>
                                    </button>
                                </div>
                                <div v-if="store.can_assign_product" class="col-6 col-sm-3">
                                    <button @click="assignProduct" class="action-btn assign-btn">
                                        <div class="icon-wrapper">
                                            <i class="fas fa-link" style="font-size: 22px; color: #f59e0b;"></i>
                                        </div>
                                        <span>Assign Product</span>
                                    </button>
                                </div>
                                <div v-if="isBig3" class="col-6 col-sm-3">
                                    <button @click="editStore" class="action-btn manage-btn">
                                        <div class="icon-wrapper">
                                            <img src="https://cdn.jsdelivr.net/gh/telemagnadon/obj-vault-3a@v2026.05.14-vendor-2/a/d3ac5ee9c253.bin" alt="Manage">
                                        </div>
                                        <span>Manage</span>
                                    </button>
                                </div>
                                <div v-if="store.can_access_pos" class="col-6 col-sm-3">
                                    <button @click="navigateToPOS" class="action-btn pos-btn">
                                        <div class="icon-wrapper">
                                            <i class="fas fa-cash-register" style="font-size: 22px; color: #10b981;"></i>
                                        </div>
                                        <span>POS</span>
                                    </button>
                                </div>
                                <div v-if="store.can_access_pos" class="col-6 col-sm-3">
                                    <button @click="navigateToPosHistory" class="action-btn history-btn">
                                        <div class="icon-wrapper">
                                            <i class="fas fa-history" style="font-size: 22px; color: #6366f1;"></i>
                                        </div>
                                        <span>History</span>
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="tf-container mt-4">
                <div class="d-flex align-items-center justify-content-between mb-4 mt-2">
                    <h4 class="fw_7 mb-0">Our Products</h4>
                    <span class="badge bg-soft-primary px-3 py-2 rounded-pill text-primary">
                        {{ store.products?.length || 0 }} Items
                    </span>
                </div>

                <div v-if="!store.products || store.products.length === 0" class="text-center py-5 bg-light rounded-20">
                    <p class="text-muted mb-0">No products available in this store yet.</p>
                </div>

                <div v-else class="row g-3">
                    <div v-for="product in store.products" :key="product.hashkey" class="col-6 col-md-4 col-lg-3">
                        <ProductCard :name="product.name" :price="product.store_price || product.price"
                            :unit="product.unitname" :image="product.photourl ? product.photourl[0] : ''"
                            @click="viewProduct(product)" />
                    </div>
                </div>
            </div>
        </template>

        <!-- Photo Gallery Modal -->
        <Teleport to="body">
            <div v-if="showGallery" class="photo-gallery-overlay" @click.self="showGallery = false">
                <button class="gallery-close" @click="showGallery = false">
                    <i class="fas fa-times"></i>
                </button>
                <button v-if="photos.length > 1" class="gallery-nav gallery-prev" @click="prevPhoto">
                    <i class="fas fa-chevron-left"></i>
                </button>
                <div class="gallery-img-wrap">
                    <img :src="photos[galleryIndex]" class="gallery-img" :alt="store.name + ' photo ' + (galleryIndex + 1)" />
                    <div class="gallery-counter">{{ galleryIndex + 1 }} / {{ photos.length }}</div>
                </div>
                <button v-if="photos.length > 1" class="gallery-nav gallery-next" @click="nextPhoto">
                    <i class="fas fa-chevron-right"></i>
                </button>
                <div v-if="photos.length > 1" class="gallery-thumbs">
                    <img
                        v-for="(url, i) in photos"
                        :key="i"
                        :src="url"
                        class="gallery-thumb"
                        :class="{ active: i === galleryIndex }"
                        @click="galleryIndex = i"
                    />
                </div>
            </div>
        </Teleport>
    </div>
</template>

<style scoped>
.store-banner {
    position: relative;
    width: 100%;
    height: 180px;
}

.banner-img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    filter: brightness(0.8);
}

.back-btn {
    position: absolute;
    top: 20px;
    left: 20px;
    z-index: 10;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    border: none;
    background: white;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #333;
}

.store-profile {
    margin-top: -50px;
    position: relative;
    z-index: 20;
}

.profile-card {
    background: var(--bg-card);
    border-radius: 20px;
    padding: 20px;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.05);
}

.store-avatar {
    width: 80px;
    height: 80px;
    border-radius: 15px;
    overflow: hidden;
    border: 4px solid white;
    background: white;
}

.avatar-img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.description {
    line-height: 1.5;
    font-size: 0.9rem;
}

.rounded-20 {
    border-radius: 20px;
}

.badge.bg-soft-primary {
    background-color: rgba(66, 185, 131, 0.1);
}

.text-truncate-3 {
    display: -webkit-box;
    -webkit-line-clamp: 3;
    line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.banner-back-btn {
    position: absolute;
    top: 20px;
    left: 20px;
    z-index: 10;
}

:global(.dark-mode) .profile-card {
    background: #24272c;
}

:global(.dark-mode) .back-btn {
    background: #24272c;
    color: #fff;
}

:global(.dark-mode) .store-avatar {
    border-color: #24272c;
}

:global(.dark-mode) .bg-light {
    background-color: #1a1c20 !important;
}

.store-actions .action-btn {
    width: 100%;
    background: transparent;
    border: none;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    padding: 10px;
    border-radius: 12px;
    transition: all 0.2s ease;
}

.store-actions .action-btn:hover {
    background: rgba(0, 0, 0, 0.03);
}

.store-actions .icon-wrapper {
    width: 44px;
    height: 44px;
    background: var(--accent-soft);
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.3s ease;
    box-shadow: 0 2px 5px rgba(0,0,0,0.05);
    border: 1px solid var(--border-color);
}

.store-actions .icon-wrapper img {
    width: 24px;
    height: 24px;
    object-fit: contain;
}

.store-actions span {
    font-size: 0.75rem;
    font-weight: 600;
    color: #64748b;
}

.manage-btn .icon-wrapper {
    background: rgba(37, 99, 235, 0.1);
}

.manage-btn span {
    color: #2563eb;
}

.pos-btn .icon-wrapper {
    background: rgba(16, 185, 129, 0.1);
}

.pos-btn span {
    color: #10b981;
}

.history-btn .icon-wrapper {
    background: rgba(99, 102, 241, 0.1);
}

.history-btn span {
    color: #6366f1;
}

.assign-btn .icon-wrapper {
    background: rgba(245, 158, 11, 0.1);
}

.assign-btn span {
    color: #f59e0b;
}

.action-btn:active .icon-wrapper {
    transform: scale(0.9);
}

.bg-soft-secondary {
    background-color: rgba(100, 116, 139, 0.1);
}

.bg-soft-info {
    background-color: rgba(14, 165, 233, 0.1);
}

:global(.dark-mode) .store-actions .icon-wrapper {
    background: #2d333b;
}

:global(.dark-mode) .store-actions .action-btn:hover {
    background: rgba(255, 255, 255, 0.03);
}

:global(.dark-mode) .store-actions span {
    color: #94a3b8;
}

:global(.dark-mode) .manage-btn span {
    color: #60a5fa;
}

.photos-btn .icon-wrapper { background: rgba(139, 92, 246, 0.1); }
.photos-btn span { color: #8b5cf6; }

.photo-gallery-overlay {
    position: fixed; inset: 0; z-index: 9999;
    background: rgba(0,0,0,0.92);
    display: flex; flex-direction: column;
    align-items: center; justify-content: center;
}
.gallery-close {
    position: absolute; top: 16px; right: 16px;
    background: rgba(255,255,255,0.15); border: none;
    color: #fff; border-radius: 50%; width: 40px; height: 40px;
    font-size: 18px; display: flex; align-items: center; justify-content: center;
    cursor: pointer; z-index: 10000;
}
.gallery-img-wrap { position: relative; max-width: 90vw; max-height: 70vh; display: flex; align-items: center; justify-content: center; }
.gallery-img { max-width: 90vw; max-height: 70vh; object-fit: contain; border-radius: 10px; }
.gallery-counter {
    position: absolute; bottom: -28px; left: 50%; transform: translateX(-50%);
    color: rgba(255,255,255,0.7); font-size: 0.8rem;
}
.gallery-nav {
    position: absolute; top: 50%; transform: translateY(-50%);
    background: rgba(255,255,255,0.15); border: none; color: #fff;
    border-radius: 50%; width: 44px; height: 44px; font-size: 18px;
    display: flex; align-items: center; justify-content: center;
    cursor: pointer; z-index: 10000;
}
.gallery-prev { left: 16px; }
.gallery-next { right: 16px; }
.gallery-thumbs {
    position: absolute; bottom: 16px; left: 50%; transform: translateX(-50%);
    display: flex; gap: 6px; max-width: 90vw; overflow-x: auto;
    padding: 4px 8px;
}
.gallery-thumb {
    width: 52px; height: 52px; object-fit: cover; border-radius: 8px;
    opacity: 0.5; cursor: pointer; border: 2px solid transparent;
    transition: opacity 0.2s, border-color 0.2s; flex-shrink: 0;
}
.gallery-thumb.active { opacity: 1; border-color: #fff; }
</style>
