<template>
  <div class="share-container">
    <div class="share-card" :class="{ wide: fileInfo?.is_folder && fileInfo?.permission !== 'upload_only' }">
      <i class="pi pi-cloud" style="font-size: 2rem; color: var(--p-primary-color)"></i>

      <div v-if="loading" class="share-loading">
        <i class="pi pi-spin pi-spinner" style="font-size: 1.5rem"></i>
        <p>Laden...</p>
      </div>

      <div v-else-if="error" class="share-error">
        <i class="pi pi-times-circle" style="font-size: 2rem; color: var(--p-red-500)"></i>
        <p>{{ error }}</p>
      </div>

      <div v-else-if="fileInfo" class="share-info">
        <h2 v-if="fileInfo.permission !== 'upload_only'">{{ fileInfo.name }}</h2>
        <h2 v-else>Datei-Upload</h2>
        <p class="file-size" v-if="fileInfo.size && !fileInfo.is_folder && fileInfo.permission !== 'upload_only'">{{ formatSize(fileInfo.size) }}</p>

        <!-- Password gate -->
        <div v-if="fileInfo.has_password && !authenticated" class="password-form">
          <p>Diese Freigabe ist passwortgeschuetzt.</p>
          <div class="field">
            <Password v-model="password" placeholder="Passwort eingeben" :feedback="false" toggle-mask fluid />
          </div>
          <Message v-if="authError" severity="error" :closable="false">{{ authError }}</Message>
          <Button label="Entsperren" @click="verifyPassword" :loading="verifying" fluid />
        </div>

        <!-- Authenticated content -->
        <div v-else class="actions-section">

          <!-- Single file: download -->
          <div v-if="!fileInfo.is_folder && fileInfo.download_allowed" class="action-block">
            <Button label="Herunterladen" icon="pi pi-download" size="large" @click="downloadFile" />
          </div>

          <!-- Folder: ZIP download + file listing (read + write) -->
          <div v-if="fileInfo.is_folder && fileInfo.download_allowed" class="action-block">
            <Button label="Ganzen Ordner als ZIP herunterladen" icon="pi pi-box" outlined @click="downloadFolderZip" />
          </div>

          <div v-if="fileInfo.is_folder && fileInfo.permission !== 'upload_only'" class="folder-content">
            <div class="folder-breadcrumb">
              <a class="crumb" @click="navigateToRoot">{{ fileInfo.name }}</a>
              <template v-for="item in folderBreadcrumb" :key="item.id">
                <i class="pi pi-angle-right crumb-sep"></i>
                <a class="crumb" @click="navigateToFolder(item.id)">{{ item.name }}</a>
              </template>
            </div>

            <div class="folder-toolbar">
              <span class="folder-count">{{ folderFiles.length }} Eintraege</span>
              <Button v-if="fileInfo.upload_allowed" icon="pi pi-upload" label="Hochladen" size="small" @click="$refs.uploadInput.click()" />
            </div>

            <div v-if="loadingFiles" class="loading-small">
              <i class="pi pi-spin pi-spinner"></i> Lade Dateien...
            </div>

            <div v-else class="file-list">
              <div v-for="f in folderFiles" :key="f.id" class="file-item"
                   :class="{ clickable: f.is_folder }" @click="f.is_folder && navigateToFolder(f.id)">
                <div class="file-info">
                  <i :class="f.is_folder ? 'pi pi-folder' : 'pi pi-file'"></i>
                  <div class="file-details">
                    <span class="file-name">{{ f.name }}</span>
                    <span class="file-meta">{{ f.is_folder ? 'Ordner' : formatSize(f.size) }}</span>
                  </div>
                </div>
                <div class="file-actions">
                  <Button v-if="!f.is_folder" icon="pi pi-download" text size="small"
                          @click.stop="downloadFolderFile(f)" />
                  <Button v-if="fileInfo.permission === 'write'" icon="pi pi-trash" text size="small"
                          severity="danger" @click.stop="confirmDeleteShareFile(f)" />
                </div>
              </div>
              <div v-if="!folderFiles.length" class="empty-folder">
                <i class="pi pi-folder-open"></i>
                <p>Ordner ist leer</p>
              </div>
            </div>
          </div>

          <!-- Upload only hint -->
          <p v-if="fileInfo.permission === 'upload_only'" class="upload-only-hint">
            Dieser Link erlaubt nur das Hochladen von Dateien.
          </p>

          <!-- Upload zone (write + upload_only) -->
          <div v-if="fileInfo.upload_allowed" class="action-block">
            <div class="upload-area"
                 @dragover.prevent="isDragging = true"
                 @dragleave.prevent="isDragging = false"
                 @drop.prevent="onDrop"
                 :class="{ dragging: isDragging }">
              <i class="pi pi-cloud-upload"></i>
              <p>Dateien hierher ziehen oder</p>
              <Button label="Dateien auswaehlen" icon="pi pi-upload" size="small" outlined @click="$refs.uploadInput.click()" />
              <input ref="uploadInput" type="file" multiple hidden @change="handleUpload" />
            </div>

            <div v-if="uploading" class="upload-progress">
              <ProgressBar :value="uploadPercent" />
              <p>{{ uploadStatus }}</p>
            </div>

            <Message v-if="uploadSuccess" severity="success" :closable="false">{{ uploadSuccess }}</Message>
          </div>
        </div>
      </div>
    </div>

    <!-- Delete confirm for shared folder files -->
    <Dialog v-model:visible="showShareDeleteConfirm" header="Datei loeschen" modal :style="{ width: '400px' }">
      <p>Moechtest du <strong>{{ shareDeleteTarget?.name }}</strong> wirklich loeschen?</p>
      <template #footer>
        <Button label="Abbrechen" text @click="showShareDeleteConfirm = false" />
        <Button label="Loeschen" severity="danger" @click="doDeleteShareFile" />
      </template>
    </Dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import axios from 'axios'
import Button from 'primevue/button'
import Password from 'primevue/password'
import Message from 'primevue/message'
import ProgressBar from 'primevue/progressbar'
import Dialog from 'primevue/dialog'

const route = useRoute()
const token = route.params.token

const loading = ref(true)
const error = ref('')
const fileInfo = ref(null)
const password = ref('')
const authenticated = ref(false)
const authError = ref('')
const verifying = ref(false)

const folderFiles = ref([])
const folderBreadcrumb = ref([])
const currentParentId = ref(null)
const loadingFiles = ref(false)

const showShareDeleteConfirm = ref(false)
const shareDeleteTarget = ref(null)
const isDragging = ref(false)
const uploading = ref(false)
const uploadPercent = ref(0)
const uploadStatus = ref('')
const uploadSuccess = ref('')

function formatSize(bytes) {
  if (!bytes) return '0 B'
  const units = ['B', 'KB', 'MB', 'GB']
  let i = 0; let size = bytes
  while (size >= 1024 && i < units.length - 1) { size /= 1024; i++ }
  return `${size.toFixed(i > 0 ? 1 : 0)} ${units[i]}`
}

function getAuthHeaders() {
  const headers = {}
  if (fileInfo.value?.has_password && password.value) {
    headers['X-Share-Password'] = password.value
  }
  return headers
}

async function loadInfo() {
  try {
    const res = await axios.get(`/api/share/${token}/info`)
    fileInfo.value = res.data
    if (!res.data.has_password) {
      authenticated.value = true
      if (res.data.is_folder && res.data.permission !== 'upload_only') {
        await loadFolderFiles()
      }
    }
  } catch (err) {
    error.value = err.response?.data?.error || 'Link nicht gefunden oder abgelaufen'
  } finally {
    loading.value = false
  }
}

async function verifyPassword() {
  authError.value = ''
  verifying.value = true
  try {
    await axios.post(`/api/share/${token}/verify`, { password: password.value })
    authenticated.value = true
    if (fileInfo.value?.is_folder && fileInfo.value?.permission !== 'upload_only') {
      await loadFolderFiles()
    }
  } catch (err) {
    authError.value = err.response?.data?.error || 'Falsches Passwort'
  } finally {
    verifying.value = false
  }
}

async function loadFolderFiles(parentId = null) {
  loadingFiles.value = true
  try {
    const params = {}
    if (parentId) params.parent_id = parentId
    const res = await axios.get(`/api/share/${token}/files`, { params, headers: getAuthHeaders() })
    folderFiles.value = res.data.files
    folderBreadcrumb.value = res.data.breadcrumb || []
    currentParentId.value = res.data.current_parent_id
  } catch {
    folderFiles.value = []
    folderBreadcrumb.value = []
  } finally {
    loadingFiles.value = false
  }
}

function navigateToFolder(folderId) {
  loadFolderFiles(folderId)
}

function navigateToRoot() {
  loadFolderFiles(null)
}

function downloadFile() {
  let url = `/api/share/${token}/download`
  if (fileInfo.value?.has_password && password.value) {
    url += `?password=${encodeURIComponent(password.value)}`
  }
  window.location.href = url
}

function downloadFolderZip() {
  let url = `/api/share/${token}/download-zip`
  if (fileInfo.value?.has_password && password.value) {
    url += `?password=${encodeURIComponent(password.value)}`
  }
  window.location.href = url
}

function downloadFolderFile(file) {
  let url = `/api/share/${token}/files/${file.id}/download`
  if (fileInfo.value?.has_password && password.value) {
    url += `?password=${encodeURIComponent(password.value)}`
  }
  window.location.href = url
}

function confirmDeleteShareFile(file) {
  shareDeleteTarget.value = file
  showShareDeleteConfirm.value = true
}

async function doDeleteShareFile() {
  if (!shareDeleteTarget.value) return
  try {
    await axios.delete(`/api/share/${token}/files/${shareDeleteTarget.value.id}`, { headers: getAuthHeaders() })
    folderFiles.value = folderFiles.value.filter(f => f.id !== shareDeleteTarget.value.id)
    showShareDeleteConfirm.value = false
  } catch (err) {
    alert(err.response?.data?.error || 'Fehler beim Loeschen')
  }
}

async function onDrop(e) {
  isDragging.value = false
  const files = Array.from(e.dataTransfer.files)
  if (files.length) await uploadFiles(files)
}

async function handleUpload(event) {
  const files = Array.from(event.target.files)
  if (files.length) await uploadFiles(files)
  event.target.value = ''
}

async function uploadFiles(files) {
  uploading.value = true
  uploadSuccess.value = ''
  uploadPercent.value = 0
  let uploaded = 0
  let errors = 0

  for (const file of files) {
    uploadStatus.value = `${uploaded + 1} / ${files.length}: ${file.name}`
    try {
      const formData = new FormData()
      formData.append('file', file)
      if (password.value) formData.append('password', password.value)
      await axios.post(`/api/share/${token}/upload`, formData, {
        headers: { 'Content-Type': 'multipart/form-data', ...getAuthHeaders() },
      })
      uploaded++
    } catch { errors++ }
    uploadPercent.value = Math.round(((uploaded + errors) / files.length) * 100)
  }

  uploading.value = false
  if (errors) {
    uploadSuccess.value = `${uploaded} hochgeladen, ${errors} fehlgeschlagen`
  } else {
    uploadSuccess.value = `${uploaded} Datei${uploaded !== 1 ? 'en' : ''} erfolgreich hochgeladen`
  }

  // Reload file list
  if (fileInfo.value?.is_folder && fileInfo.value?.permission !== 'upload_only') {
    await loadFolderFiles()
  }
}

onMounted(loadInfo)
</script>

<style scoped>
.share-container {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  background: var(--p-surface-50); padding: 1rem;
}
.share-card {
  background: var(--p-surface-0); border-radius: 12px; padding: 2.5rem;
  text-align: center; max-width: 500px; width: 100%;
  box-shadow: 0 2px 12px rgba(0,0,0,0.08);
}
.share-card.wide { max-width: 700px; }
.share-card h2 { margin: 1rem 0 0.25rem; font-size: 1.25rem; }
.file-size { color: var(--p-text-muted-color); margin-bottom: 1rem; }
.password-form { text-align: left; margin-top: 1.5rem; }
.password-form p { margin-bottom: 1rem; color: var(--p-text-muted-color); }
.field { margin-bottom: 1rem; }
.actions-section { margin-top: 1.5rem; text-align: left; }
.action-block { margin-bottom: 1.5rem; text-align: center; }
.share-loading, .share-error { margin-top: 1.5rem; }

/* Folder content */
.folder-content { margin-bottom: 1.5rem; }
.folder-breadcrumb {
  display: flex; align-items: center; gap: 0.25rem; margin-bottom: 0.75rem;
  font-size: 0.9rem;
}
.crumb { cursor: pointer; color: var(--p-primary-color); font-weight: 500; }
.crumb:hover { text-decoration: underline; }
.crumb-sep { font-size: 0.7rem; color: var(--p-surface-400); }
.folder-toolbar {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 0.75rem; padding-bottom: 0.5rem; border-bottom: 1px solid var(--p-surface-200);
}
.folder-count { font-size: 0.85rem; color: var(--p-text-muted-color); }
.file-list { max-height: 400px; overflow-y: auto; }
.file-item {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.5rem 0; border-bottom: 1px solid var(--p-surface-100);
}
.file-item.clickable { cursor: pointer; }
.file-item.clickable:hover { background: var(--p-surface-50); }
.file-info { display: flex; align-items: center; gap: 0.5rem; flex: 1; min-width: 0; }
.file-info i { font-size: 1.1rem; color: var(--p-surface-500); flex-shrink: 0; }
.file-details { display: flex; flex-direction: column; min-width: 0; }
.file-name { font-size: 0.875rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-meta { font-size: 0.75rem; color: var(--p-text-muted-color); }
.file-actions { display: flex; flex-shrink: 0; }
.empty-folder { text-align: center; padding: 2rem; color: var(--p-text-muted-color); }
.empty-folder i { font-size: 1.5rem; }
.loading-small { text-align: center; padding: 1rem; color: var(--p-text-muted-color); }

/* Upload */
.upload-area {
  border: 2px dashed var(--p-surface-300); border-radius: 10px;
  padding: 2rem; text-align: center; transition: all 0.2s;
}
.upload-area:hover, .upload-area.dragging {
  border-color: var(--p-primary-color); background: var(--p-primary-50);
}
.upload-area i { font-size: 2rem; color: var(--p-surface-400); }
.upload-area.dragging i { color: var(--p-primary-color); }
.upload-area p { margin: 0.5rem 0; color: var(--p-text-muted-color); font-size: 0.9rem; }
.upload-progress { margin-top: 1rem; }
.upload-progress p { font-size: 0.85rem; color: var(--p-text-muted-color); margin-top: 0.5rem; }
.upload-only-hint { color: var(--p-text-muted-color); font-size: 0.9rem; margin-bottom: 1rem; text-align: center; }
</style>
