<template>
  <q-page padding class="category-selector-page">
    <div class="row justify-center">
      <div class="col-12 col-md-8 col-lg-6">
        <q-card>
          <q-card-section>
            <div class="text-h5">Select Categories</div>
            <div class="text-subtitle2 text-grey">Choose categories that best describe this life event</div>
          </q-card-section>

          <q-card-section>
            <!-- Search bar -->
            <q-input
              v-model="searchQuery"
              label="Search categories..."
              filled
              clearable
              class="q-mb-md"
            >
              <template v-slot:prepend>
                <q-icon name="search" />
              </template>
            </q-input>

            <!-- Category list -->
            <div class="category-list q-gutter-md">
              <div
                v-for="category in filteredCategories"
                :key="category.id"
                class="category-item"
                :class="{ 'selected': isCategorySelected(category.id) }"
                @click="toggleCategorySelection(category.id)"
              >
                <div class="category-icon">
                  <q-icon :name="category.icon" size="32px" color="primary" />
                </div>
                
                <div class="category-info">
                  <div class="category-name">{{ category.label }}</div>
                  <div v-if="category.description" class="category-description text-grey">{{ category.description }}</div>
                </div>

                <!-- Selection indicator -->
                <q-icon
                  v-if="isCategorySelected(category.id)"
                  name="check_circle"
                  color="positive"
                  size="24px"
                  class="selection-indicator"
                />
              </div>
            </div>
          </q-card-section>

          <!-- Selected categories section -->
          <q-card-section v-if="selectedCategories.length > 0" class="selected-section">
            <q-separator class="q-mb-md" />
            
            <div class="text-subtitle2 q-mb-md">Selected Categories ({{ selectedCategories.length }})</div>
            
            <div class="selected-categories-container">
              <div class="selected-categories-list">
                <div
                  v-for="category in selectedCategoriesData"
                  :key="category.id"
                  class="selected-category"
                  @mouseenter="hoveredCategoryId = category.id"
                  @mouseleave="hoveredCategoryId = null"
                >
                  <q-chip
                    :removable="hoveredCategoryId === category.id"
                    @remove="removeCategorySelection(category.id)"
                    color="primary"
                    text-color="white"
                    :icon="category.icon"
                    class="selected-category-chip"
                  >
                    {{ category.label }}
                  </q-chip>
                </div>
              </div>
            </div>
          </q-card-section>
        </q-card>
      </div>
    </div>

    <!-- Fixed Footer with Action Buttons -->
    <q-footer elevated class="bg-white text-dark selector-footer">
      <q-toolbar class="justify-between">
        <q-btn
          icon="arrow_back"
          color="grey"
          flat
          round
          @click="onCancel"
          class="q-ml-sm"
        >
          <q-tooltip class="bg-grey">Cancel</q-tooltip>
        </q-btn>
        
        <q-btn
          label="Add Categories"
          color="primary"
          unelevated
          :disable="selectedCategories.length === 0"
          @click="onAddCategories"
          class="q-mr-sm"
        />
      </q-toolbar>
    </q-footer>
  </q-page>
</template>

<script>
import { ref, computed, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { categoryStructure } from '../utils/editFormOptions.js'

export default {
  name: 'CategorySelector',
  setup() {
    const router = useRouter()
    const route = useRoute()
    
    // State
    const searchQuery = ref('')
    const selectedCategories = ref([])
    const hoveredCategoryId = ref(null)
    
    // Category icons mapping
    const categoryIcons = {
      'career': 'work',
      'education': 'school',
      'awards': 'emoji_events',
      'personal-celebrations': 'celebration',
      'relationships': 'favorite',
      'parenthood': 'child_care',
      'passing': 'sentiment_very_dissatisfied',
      'festivities': 'party_mode',
      'social-events': 'groups',
      'community': 'volunteer_activism',
      'health': 'health_and_safety',
      'travel': 'flight',
      'hobbies': 'palette',
      'sports': 'sports_soccer',
      'technology': 'computer',
      'financial': 'attach_money',
      'legal': 'gavel',
      'spiritual': 'spa',
      'creative': 'brush',
      'learning': 'menu_book'
    }
    
    // Convert category structure to flat list without subcategories
    const categories = ref(
      categoryStructure.map((cat, index) => ({
        id: index + 1,
        label: cat.label,
        value: cat.value,
        icon: categoryIcons[cat.value] || 'category',
        description: null // Could be added later
      }))
    )
    
    // Computed
    const filteredCategories = computed(() => {
      if (!searchQuery.value) {
        return categories.value
      }
      return categories.value.filter(category =>
        category.label.toLowerCase().includes(searchQuery.value.toLowerCase())
      )
    })
    
    const selectedCategoriesData = computed(() => {
      return categories.value.filter(category => selectedCategories.value.includes(category.id))
    })
    
    // Methods
    const isCategorySelected = (categoryId) => {
      return selectedCategories.value.includes(categoryId)
    }
    
    const toggleCategorySelection = (categoryId) => {
      const index = selectedCategories.value.indexOf(categoryId)
      if (index > -1) {
        selectedCategories.value.splice(index, 1)
      } else {
        selectedCategories.value.push(categoryId)
      }
    }
    
    const removeCategorySelection = (categoryId) => {
      const index = selectedCategories.value.indexOf(categoryId)
      if (index > -1) {
        selectedCategories.value.splice(index, 1)
      }
    }
    
    const onCancel = () => {
      router.go(-1)
    }
    
    const onAddCategories = () => {
      // Pass selected categories back to the edit page
      const selectedCategoriesLabels = selectedCategoriesData.value.map(category => category.label)
      
      // Navigate back with the selected categories data
      router.push({
        name: 'edit',
        query: {
          ...route.query,
          selectedCategories: JSON.stringify(selectedCategoriesLabels)
        }
      })
    }
    
    // Load previously selected categories from route query
    onMounted(() => {
      if (route.query.currentSelection) {
        try {
          const currentSelection = JSON.parse(route.query.currentSelection)
          // Convert labels back to IDs
          selectedCategories.value = categories.value
            .filter(category => currentSelection.includes(category.label))
            .map(category => category.id)
        } catch (error) {
          console.error('Error parsing current selection:', error)
        }
      }
    })
    
    return {
      searchQuery,
      selectedCategories,
      hoveredCategoryId,
      categories,
      filteredCategories,
      selectedCategoriesData,
      isCategorySelected,
      toggleCategorySelection,
      removeCategorySelection,
      onCancel,
      onAddCategories
    }
  }
}
</script>

<style scoped>
.category-selector-page {
  min-height: 100vh;
  padding-bottom: 80px; /* Space for footer */
}

.category-list {
  max-height: 400px;
  overflow-y: auto;
}

.category-item {
  display: flex;
  align-items: center;
  padding: 16px;
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.2s ease;
  border: 2px solid transparent;
  background: rgba(0, 0, 0, 0.02);
}

.category-item:hover {
  background-color: rgba(25, 118, 210, 0.1);
  transform: translateY(-1px);
}

.category-item.selected {
  background-color: rgba(25, 118, 210, 0.15);
  border-color: var(--q-primary);
}

.category-icon {
  margin-right: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 48px;
  height: 48px;
  border-radius: 12px;
  background: rgba(25, 118, 210, 0.1);
}

.category-info {
  flex: 1;
}

.category-name {
  font-weight: 500;
  font-size: 16px;
  line-height: 1.2;
  margin-bottom: 4px;
}

.category-description {
  font-size: 14px;
}

.selection-indicator {
  margin-left: 16px;
}

.selected-section {
  background-color: rgba(0, 0, 0, 0.02);
}

.selected-categories-container {
  max-height: 200px;
  overflow-y: auto;
}

.selected-categories-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.selected-category {
  position: relative;
}

.selected-category-chip {
  font-size: 14px;
  padding: 8px 16px;
  min-height: 36px;
}

/* Responsive design */
@media (max-width: 600px) {
  .category-item {
    padding: 12px;
  }
  
  .category-icon {
    margin-right: 12px;
    width: 40px;
    height: 40px;
  }
  
  .selected-categories-list {
    gap: 6px;
  }
}

/* Footer styling */
.selector-footer {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  border-top: 1px solid #e0e0e0;
}

.selector-footer .q-toolbar {
  min-height: 60px;
  padding: 8px 16px;
}
</style>