<script setup lang="ts">
import { 
  X, 
  ChevronRight, 
  ArrowLeft,
  FileCode,
  Box,
  Activity,
  Upload,
  FolderPlus,
  Compass
} from 'lucide-vue-next'
import { useKnowledgeView } from '@/pages/knowledge/composables/useKnowledgeView'
import './KnowledgeHud.css'
import { watch } from 'vue'

const props = defineProps<{
  folder: any
}>()

const emit = defineEmits(['close', 'open-preview', 'trigger-new-folder', 'trigger-upload'])

const {
  documents,
  visibleFolders,
  isLoadingDocuments,
  isRoot,
  enterFolder,
  goBack,
  getFileTypeColor,
  formatFileSize,
} = useKnowledgeView()

watch(() => props.folder, (newFolder) => {
  if (newFolder) {
    enterFolder(newFolder)
  }
}, { immediate: true })

function handleFileClick(doc: any) {
  emit('open-preview', doc)
}
</script>

<template>
  <div class="knowledge-slide-panel">
    <header class="slide-header-jarvis">
      <div class="header-top-tech">
        <div class="coord-tag">
          <Compass :size="10" />
          <span>SEC_0x{{ props.folder?.id.substring(0,6).toUpperCase() }}</span>
        </div>
        <button class="close-btn-jarvis" @click="emit('close')"><X :size="16" /></button>
      </div>
      <div class="title-main-jarvis">
        <button v-if="!isRoot" class="back-btn-jarvis" @click="goBack"><ArrowLeft :size="14" /></button>
        <h3 class="slide-title-jarvis">{{ props.folder?.name.toUpperCase() }}</h3>
      </div>
    </header>

    <div class="action-row-jarvis">
      <button class="action-btn-jarvis" @click="emit('trigger-new-folder', props.folder?.id)">
        <FolderPlus :size="10" />
        <span>New_Sub</span>
      </button>
      <button class="action-btn-jarvis amber" @click="emit('trigger-upload')">
        <Upload :size="10" />
        <span>Upload_Data</span>
      </button>
    </div>

    <div class="slide-content slim-scroll">
      <!-- Sub-sectors -->
      <section v-if="visibleFolders.length" class="hud-section-jarvis">
        <div class="matrix-label">
          <span class="label-text">CHILD_NODES</span>
          <div class="label-line"></div>
        </div>
        <div class="jarvis-grid">
          <div 
            v-for="(sub, idx) in visibleFolders" 
            :key="sub.id" 
            class="hud-item-jarvis"
            @click="enterFolder(sub)"
          >
            <span class="item-idx">{{ idx.toString(16).padStart(2, '0') }}</span>
            <Box :size="14" class="item-icon-cyan" />
            <span class="item-name-jarvis">{{ sub.name }}</span>
            <ChevronRight :size="12" class="item-arrow-cyan" />
          </div>
        </div>
      </section>

      <!-- Data Objects -->
      <section v-if="documents.length || isLoadingDocuments" class="hud-section-jarvis">
        <div class="matrix-label">
          <span class="label-text">DATA_STREAMS</span>
          <div class="label-line"></div>
        </div>

        <div v-if="isLoadingDocuments" class="jarvis-loader">
          <Activity :size="16" class="spin-tech" />
          <span>QUERYING_BUFFER...</span>
        </div>

        <div v-else class="jarvis-grid">
          <div 
            v-for="(doc, idx) in documents" 
            :key="doc.id" 
            class="hud-item-jarvis doc-style"
            @click="handleFileClick(doc)"
          >
            <span class="item-idx">{{ (idx + 10).toString(16).toUpperCase() }}</span>
            <FileCode :size="14" :style="{ color: getFileTypeColor(doc.file_type) }" />
            <div class="item-info-jarvis">
              <span class="item-name-jarvis">{{ doc.title }}</span>
              <span class="item-meta-jarvis">{{ formatFileSize(doc.file_size) }} // {{ doc.file_type.toUpperCase() }}</span>
            </div>
          </div>
        </div>
      </section>
    </div>
  </div>
</template>

<style scoped>
.header-top-tech { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.coord-tag { display: flex; align-items: center; gap: 6px; font-family: var(--font-mono); font-size: 8px; color: var(--jarvis-cyan); opacity: 0.6; }
.title-main-jarvis { display: flex; align-items: center; gap: 12px; }
.close-btn-jarvis, .back-btn-jarvis { background: rgba(255,255,255,0.05); border: 1px solid rgba(255,255,255,0.1); color: #fff; cursor: pointer; padding: 6px; transition: 0.2s; }
.close-btn-jarvis:hover { background: var(--accent-red); border-color: var(--accent-red); }
.back-btn-jarvis:hover { border-color: var(--jarvis-cyan); color: var(--jarvis-cyan); }

.action-btn-jarvis.amber { border-color: var(--jarvis-amber); color: var(--jarvis-amber); }
.action-btn-jarvis.amber:hover { background: var(--jarvis-amber); color: #000; }

.hud-section-jarvis { margin-bottom: 25px; }
.matrix-label { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
.label-text { font-family: var(--font-display); font-size: 9px; color: var(--jarvis-cyan); letter-spacing: 0.2em; }
.label-line { flex: 1; height: 1px; background: linear-gradient(90deg, rgba(0, 245, 212, 0.3), transparent); }

.jarvis-grid { display: flex; flex-direction: column; gap: 6px; }
.item-idx { font-family: var(--font-mono); font-size: 8px; opacity: 0.3; color: var(--jarvis-cyan); width: 15px; }
.item-icon-cyan { color: var(--jarvis-cyan); }
.item-name-jarvis { flex: 1; font-family: var(--font-mono); font-size: 12px; color: rgba(255,255,255,0.9); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item-arrow-cyan { color: var(--jarvis-cyan); opacity: 0.5; }

.item-info-jarvis { display: flex; flex-direction: column; min-width: 0; }
.item-meta-jarvis { font-size: 8px; font-family: var(--font-mono); color: var(--jarvis-cyan); opacity: 0.4; margin-top: 2px; }

.jarvis-loader { display: flex; align-items: center; gap: 12px; color: var(--jarvis-cyan); font-family: var(--font-mono); font-size: 10px; padding: 20px; }
.spin-tech { animation: spin 2s linear infinite; }
@keyframes spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }
</style>

