<template>
  <div class="space-y-6">
    <!-- Header with Search and Filters -->
    <div class="flex flex-col md:flex-row items-center justify-between gap-4">
      <h2 class="text-2xl font-bold">檔案管理 (Demo)</h2>
      <div class="flex flex-wrap items-center gap-3 w-full md:w-auto">
        <!-- Media Type Filter -->
        <div class="flex items-center bg-gray-700 rounded p-1">
          <button 
            @click="setMediaType('all')"
            :class="{'bg-blue-600 text-white': mediaType === 'all', 'text-gray-300 hover:text-white': mediaType !== 'all'}"
            class="px-3 py-1 rounded text-sm transition"
          >
            全部
          </button>
          <button 
            @click="setMediaType('video')"
            :class="{'bg-blue-600 text-white': mediaType === 'video', 'text-gray-300 hover:text-white': mediaType !== 'video'}"
            class="px-3 py-1 rounded text-sm transition"
          >
            影片
          </button>
          <button 
            @click="setMediaType('photo')"
            :class="{'bg-blue-600 text-white': mediaType === 'photo', 'text-gray-300 hover:text-white': mediaType !== 'photo'}"
            class="px-3 py-1 rounded text-sm transition"
          >
            照片
          </button>
        </div>
        <!-- Status Filter -->
        <div class="flex items-center bg-gray-700 rounded p-1">
          <button 
            @click="setStatusFilter('all')"
            :class="{'bg-blue-600 text-white': statusFilter === 'all', 'text-gray-300 hover:text-white': statusFilter !== 'all'}"
            class="px-3 py-1 rounded text-sm transition"
          >
            全部
          </button>
          <button 
            @click="setStatusFilter('unregistered')"
            :class="{'bg-blue-600 text-white': statusFilter === 'unregistered', 'text-gray-300 hover:text-white': statusFilter !== 'unregistered'}"
            class="px-3 py-1 rounded text-sm transition"
          >
            未註冊
          </button>
          <button 
            @click="setStatusFilter('pending')"
            :class="{'bg-blue-600 text-white': statusFilter === 'pending', 'text-gray-300 hover:text-white': statusFilter !== 'pending'}"
            class="px-3 py-1 rounded text-sm transition"
          >
            待處理
          </button>
          <button 
            @click="setStatusFilter('processing')"
            :class="{'bg-blue-600 text-white': statusFilter === 'processing', 'text-gray-300 hover:text-white': statusFilter !== 'processing'}"
            class="px-3 py-1 rounded text-sm transition"
          >
            處理中
          </button>
          <button 
            @click="setStatusFilter('completed')"
            :class="{'bg-blue-600 text-white': statusFilter === 'completed', 'text-gray-300 hover:text-white': statusFilter !== 'completed'}"
            class="px-3 py-1 rounded text-sm transition"
          >
            已完成
          </button>
          <button 
            @click="setStatusFilter('unindexed')"
            :class="{'bg-blue-600 text-white': statusFilter === 'unindexed', 'text-gray-300 hover:text-white': statusFilter !== 'unindexed'}"
            class="px-3 py-1 rounded text-sm transition"
          >
            未入庫
          </button>
          <button 
            @click="setStatusFilter('indexed')"
            :class="{'bg-blue-600 text-white': statusFilter === 'indexed', 'text-gray-300 hover:text-white': statusFilter !== 'indexed'}"
            class="px-3 py-1 rounded text-sm transition"
          >
            已入庫
          </button>
        </div>
        <!-- Search input -->
        <input
          v-model="searchQuery"
          type="text"
          placeholder="搜尋檔名..."
          class="bg-gray-700 border border-gray-600 rounded-lg px-3 py-2 text-white text-sm w-full md:w-48 focus:outline-none focus:border-blue-500"
        />
      </div>
    </div>

    <!-- Loading -->
    <div v-if="loading" class="flex justify-center py-12">
      <div class="animate-spin rounded-full h-10 w-10 border-b-2 border-blue-500"></div>
    </div>

    <!-- Error -->
    <div v-else-if="error" class="bg-red-900/50 border border-red-700 rounded p-4 text-red-300">
      {{ error }}
    </div>

    <!-- File List -->
    <div v-else class="bg-gray-800 rounded-lg border border-gray-700 overflow-x-auto">
      <table class="min-w-full divide-y divide-gray-700">
        <thead class="bg-gray-900">
          <tr>
            <th class="px-6 py-3 text-left text-xs font-medium text-gray-300 uppercase tracking-wider">檔案名稱</th>
            <th class="px-6 py-3 text-left text-xs font-medium text-gray-300 uppercase tracking-wider">類型</th>
            <th class="px-6 py-3 text-left text-xs font-medium text-gray-300 uppercase tracking-wider">狀態</th>
            <th class="px-6 py-3 text-left text-xs font-medium text-gray-300 uppercase tracking-wider">UUID</th>
            <th class="px-6 py-3 text-left text-xs font-medium text-gray-300 uppercase tracking-wider">操作</th>
          </tr>
        </thead>
        <tbody class="divide-y divide-gray-700 bg-gray-800">
           <tr v-for="file in displayFiles" :key="file.file_uuid || file.file_path" class="hover:bg-gray-750 transition">
             <td class="px-6 py-4 whitespace-nowrap">
               <div class="text-sm font-medium text-white truncate max-w-xs" :title="file.file_name">
                 {{ file.file_name }}
               </div>
             </td>
             <td class="px-6 py-4 whitespace-nowrap text-sm">
               <span v-if="file.media_type === 'video'" class="px-2 py-0.5 rounded text-xs bg-blue-900 text-blue-200">
                 🎬 影片
               </span>
               <span v-else class="px-2 py-0.5 rounded text-xs bg-green-900 text-green-200">
                 📷 照片
               </span>
             </td>
             <td class="px-6 py-4 whitespace-nowrap text-sm">
               <span v-if="file.status === 'completed' && file.is_indexed" class="px-2 py-0.5 rounded text-xs bg-green-900 text-green-200">
                 ✅ 已入庫
               </span>
               <span v-else-if="file.status === 'completed' && !file.is_indexed" class="px-2 py-0.5 rounded text-xs bg-yellow-900 text-yellow-200">
                 ⏳ 未入庫
               </span>
               <span v-else-if="file.status === 'completed'" class="px-2 py-0.5 rounded text-xs bg-green-900 text-green-200">
                 ✅ 已完成
               </span>
               <span v-else-if="file.status === 'processing'" class="px-2 py-0.5 rounded text-xs bg-yellow-900 text-yellow-200">
                 🔄 處理中
               </span>
               <span v-else-if="file.status === 'pending'" class="px-2 py-0.5 rounded text-xs bg-blue-900 text-blue-200">
                 ⏳ 待處理
               </span>
               <span v-else class="px-2 py-0.5 rounded text-xs bg-gray-600 text-gray-300">
                 📦 未註冊
               </span>
             </td>
             <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-300 font-mono text-xs">
               {{ file.file_uuid || '-' }}
             </td>
             <td class="px-6 py-4 whitespace-nowrap text-right">
               <div class="flex justify-end gap-2">
                 <!-- Enter Demo / Workbench (Completed) -->
                 <button 
                   v-if="file.status === 'completed'"
                   @click="enterWorkbench(file.file_uuid)"
                   class="px-3 py-1 bg-purple-600 hover:bg-purple-700 text-white text-xs rounded transition"
                 >
                   臉部工作台
                 </button>

                 <!-- Start Processing (Pending) -->
                 <button 
                   v-if="file.status === 'pending'"
                   @click="startProcessing(file.file_uuid)"
                   class="px-3 py-1 bg-yellow-600 hover:bg-yellow-700 text-white text-xs rounded transition"
                 >
                   開始處理
                 </button>
                 
                 <!-- Register (Unregistered) -->
                 <button 
                   v-if="!file.status || file.status === 'unregistered'"
                   @click="registerFile(file.file_path)"
                   class="px-3 py-1 bg-green-600 hover:bg-green-700 text-white text-xs rounded transition"
                 >
                   註冊
                 </button>

                 <!-- Unregister (Pending/Processing/Completed) -->
                 <button 
                   v-if="file.file_uuid"
                   @click="unregisterFile(file.file_uuid, file.file_name)"
                   class="px-3 py-1 bg-red-600 hover:bg-red-700 text-white text-xs rounded transition"
                 >
                   取消註冊
                 </button>
               </div>
             </td>
           </tr>
        </tbody>
      </table>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { registerVideo, unregisterVideo, httpFetch, getCurrentConfig } from '@/api/client'

const VIDEO_EXTENSIONS = ['mp4', 'mov', 'mkv', 'avi', 'webm', 'wmv', 'flv', 'm4v']
const PHOTO_EXTENSIONS = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp', 'tiff', 'tif']

const router = useRouter()
const files = ref<any[]>([])
const loading = ref(false)
const error = ref<string | null>(null)
const searchQuery = ref('')
const statusFilter = ref('all')
const mediaType = ref('all')

function getMediaType(fileName: string): 'video' | 'photo' {
  const ext = fileName.split('.').pop()?.toLowerCase() || ''
  if (VIDEO_EXTENSIONS.includes(ext)) return 'video'
  if (PHOTO_EXTENSIONS.includes(ext)) return 'photo'
  return 'video'
}

const displayFiles = computed(() => {
  let result = files.value
  
  // Filter by media type
  if (mediaType.value !== 'all') {
    result = result.filter(f => f.media_type === mediaType.value)
  }

  // Filter by search
  if (searchQuery.value) {
    const q = searchQuery.value.toLowerCase()
    result = result.filter(f => 
      f.file_name.toLowerCase().includes(q) || 
      (f.file_path && f.file_path.toLowerCase().includes(q))
    )
  }

  // Filter by status
  if (statusFilter.value !== 'all') {
    if (statusFilter.value === 'indexed') {
      result = result.filter(f => f.status === 'completed' && f.is_indexed)
    } else if (statusFilter.value === 'unindexed') {
      result = result.filter(f => f.status === 'completed' && !f.is_indexed)
    } else {
      result = result.filter(f => f.status === statusFilter.value)
    }
  }

  return result
})

function setStatusFilter(status: string) {
  statusFilter.value = status
}

function setMediaType(type: string) {
  mediaType.value = type
}

async function fetchFiles() {
  loading.value = true
  try {
    const config = getCurrentConfig()
    
    // Get registered files with real processing status FIRST
    let regFiles: any[] = []
    try {
      const regResp = await httpFetch<any>(`${config.api_base_url}/api/v1/files?page=1&page_size=200`)
      regFiles = (regResp?.files || regResp?.data || []).map((f: any) => {
        const mediaType = getMediaType(f.file_name)
        return {
          ...f,
          media_type: mediaType,
          status: f.status || 'pending',
          is_indexed: (f.total_chunks || 0) > 0
        }
      })
    } catch {
      // Registered files API may not be available
    }

    // Get scan results
    const scanResp = await httpFetch<any>(`${config.api_base_url}/api/v1/files/scan`)
    const scanFiles: any[] = (scanResp?.files || []).map((f: any) => {
      const mediaType = getMediaType(f.file_name)
      return {
        ...f,
        media_type: mediaType,
        is_indexed: false
      }
    })

    // Build a set of registered file paths for quick lookup
    const registeredPaths = new Set(regFiles.map(f => f.file_path))

    // Merge: start with scan results, override with registered data where available
    const merged = new Map<string, any>()
    
    // First pass: add all scan results
    for (const f of scanFiles) {
      const isRegistered = registeredPaths.has(f.file_path)
      merged.set(f.file_path, {
        ...f,
        status: isRegistered ? 'pending' : 'unregistered'
      })
    }
    
    // Second pass: override with real registered data
    for (const f of regFiles) {
      merged.set(f.file_path, f)
    }

    files.value = Array.from(merged.values())
  } catch (e) {
    console.error('Failed to fetch files:', e)
    error.value = String(e)
  } finally {
    loading.value = false
  }
}

async function registerFile(filePath: string) {
  if (!filePath) { alert('無法註冊：缺少檔案路徑'); return }
  try {
    await registerVideo(filePath)
    // Refresh list
    await fetchFiles()
  } catch (e) {
    console.error('Register failed:', e)
    alert('註冊失敗：' + e)
  }
}

async function unregisterFile(fileUuid: string, fileName: string) {
  if (!fileUuid) { alert('無法取消註冊：缺少 UUID'); return }
  const displayName = fileName || '未知檔案'
  if (!confirm(`確定要取消註冊 "${displayName}" 嗎？這將刪除資料庫中的相關記錄。`)) {
    return
  }
  
  try {
    await unregisterVideo(fileUuid)
    await fetchFiles()
  } catch (e) {
    console.error('Unregister failed:', e)
    alert('取消註冊失敗：' + e)
  }
}

async function startProcessing(fileUuid: string) {
  if (!fileUuid) { alert('無法處理：缺少 UUID'); return }
  if (!confirm('確定要開始分析處理此檔案嗎？')) return
  
  try {
    const config = getCurrentConfig()
    await httpFetch(`${config.api_base_url}/api/v1/file/${fileUuid}/process`, {
      method: 'POST',
      body: JSON.stringify({})
    })
    
    // After triggering, status should change to processing
    // We can poll or just refresh
    await fetchFiles()
  } catch (e) {
    console.error('Start processing failed:', e)
    alert('開始處理失敗：' + e)
  }
}

function enterWorkbench(fileUuid: string) {
  if (!fileUuid) { alert('無法開啟工作台：缺少 UUID'); return }
  router.push(`/file/${fileUuid}`)
}

onMounted(fetchFiles)
</script>
