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

import { onMounted } from 'vue'
import { useNavigate } from '../composables/Core/useNavigate'
import { usePhotoList } from '../composables/usePhotoList'
import { extractHashkeyFromUrl } from '../composables/useUrlArgument'

const props = defineProps({
  photos: {
    type: Array,
    default: null
  },
  target: {
    type: String,
    default: null
  },
  type: {
    type: String,
    default: 'StoreMarket'
  }
})

const { navigate } = useNavigate()
import BackButton from '../Components/Core/BackButton.vue'
const { photos: photoList, loading, fetchPhotos, blobCache } = usePhotoList()

// Get target from URL if not in props
const urlParams = new URL(window.location.href).searchParams
const targetHash = props.target || extractHashkeyFromUrl(window.location.pathname) || urlParams.get('target') || urlParams.get('t')
const targetType = props.type || urlParams.get('type') || 'StoreMarket'

onMounted(async () => {
  document.title = 'Photos'
  
  if (!targetHash) {
    navigate({ page: 'ListStores' })
    return
  }
  
  if (props.photos && Array.isArray(props.photos)) {
    photoList.value = props.photos
    // Ensure they are cached
    const { useFileBlobCache } = await import('../composables/useFileBlobCache')
    const { preCacheFiles } = useFileBlobCache()
    await preCacheFiles(photoList.value)
  } else if (targetHash) {
    await fetchPhotos(targetHash, targetType)
  }
})

const viewPhoto = (hash) => {
  navigate({ page: 'PhotoViewer', props: { target: hash } })
}
</script>

<template>
  <div class="view-all-photos-page pb-5">
    <div class="tf-container">
      <div class="pt-4 px-2">
        <BackButton text="" />
      </div>
      <div class="header-section text-center mb-5 mt-2">
        <h2 class="fw_7 title-gradient mb-3">Photo Gallery</h2>
        <p class="text-muted">All store photos</p>
      </div>

      <!-- Loading State -->
      <div v-if="loading" class="text-center py-10">
        <div class="spinner-grow text-primary" role="status">
          <span class="visually-hidden">Loading...</span>
        </div>
        <p class="mt-3 text-muted fw_5">Fetching your photos...</p>
      </div>

      <!-- Empty State -->
      <div v-else-if="photoList.length === 0" class="text-center py-10 empty-state">
        <div class="empty-icon-wrapper mb-4">
          <i class="fas fa-camera-retro fa-4x text-light-gray"></i>
        </div>
        <h4 class="fw_6">No Photos Yet</h4>
        <p class="text-muted">This gallery is currently empty.</p>
        <button @click="navigate({ page: 'Home' })" class="btn-premium mt-3">
          Go Back Home
        </button>
      </div>

      <!-- Photo Grid -->
      <div v-else class="gallery-grid">
        <div 
          v-for="(photoHash, index) in photoList" 
          :key="photoHash"
          class="gallery-item-wrapper"
        >
          <div class="gallery-card" @click="viewPhoto(photoHash)">
            <img 
              v-if="blobCache[photoHash]"
              :src="blobCache[photoHash]" 
              alt="Gallery Photo" 
              class="gallery-image"
              loading="lazy"
            >
            <div v-else class="image-skeleton animate-pulse"></div>
            
            <div class="gallery-overlay">
              <div class="overlay-content">
                <i class="fas fa-expand-alt fa-lg"></i>
                <span>View Full</span>
              </div>
            </div>
          </div>
        </div>
      </div>

      <div class="text-center mt-5 footer-stats" v-if="photoList.length > 0">
        <span class="badge rounded-pill bg-light text-dark p-2 px-4 shadow-sm">
          {{ photoList.length }} {{ photoList.length === 1 ? 'Photo' : 'Photos' }}
        </span>
      </div>
    </div>
  </div>
</template>

<style scoped>
.title-gradient {
  background: linear-gradient(45deg, #2563eb, #7c3aed);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  font-size: 2.2rem;
  line-height: 1.4;
  padding-bottom: 0.1em;
}

.gallery-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 16px;
  padding: 8px;
}

.gallery-item-wrapper {
  perspective: 1000px;
}

.gallery-card {
  position: relative;
  border-radius: 16px;
  overflow: hidden;
  aspect-ratio: 1;
  background: #f1f5f9;
  cursor: pointer;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}

.gallery-card:hover {
  transform: translateY(-8px) scale(1.02);
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
}

.gallery-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.5s ease;
}

.gallery-card:hover .gallery-image {
  transform: scale(1.1);
}

.gallery-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.3s ease;
  backdrop-filter: blur(2px);
}

.gallery-card:hover .gallery-overlay {
  opacity: 1;
}

.overlay-content {
  color: white;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  transform: translateY(20px);
  transition: transform 0.3s ease;
}

.gallery-card:hover .overlay-content {
  transform: translateY(0);
}

.image-skeleton {
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, #f1f5f9 25%, #e2e8f0 50%, #f1f5f9 75%);
  background-size: 200% 100%;
}

.animate-pulse {
  animation: pulse 1.5s infinite linear;
}

@keyframes pulse {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

.empty-icon-wrapper {
  color: #cbd5e1;
}

.btn-premium {
  background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%);
  color: white;
  border: none;
  padding: 12px 24px;
  border-radius: 12px;
  font-weight: 600;
  transition: all 0.3s;
}

.btn-premium:hover {
  filter: brightness(1.1);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(37, 99, 235, 0.4);
}

.footer-stats {
  font-family: 'Outfit', sans-serif;
}

@media (max-width: 576px) {
  .gallery-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
  }
}
</style>