<script lang="ts" setup>
import { computed } from 'vue'
import ServiceTabLayout from '@/Components/Panel/ServiceTabLayout.vue'
import type { Service } from '@/types'

interface FileEntry {
  name: string
  mode: string
  size: number
  is_file: boolean
  is_symlink: boolean
  mimetype: string
  created_at: string
  modified_at: string
}

interface Props {
  service: Service
  files: FileEntry[]
  currentPath: string
}

const props = defineProps<Props>()

const tabs = computed(() => [
  { label: 'Dashboard', icon: 'tabler-dashboard', to: `/services/${props.service.id}/game-panel` },
  { label: 'Console', icon: 'tabler-terminal-2', to: `/services/${props.service.id}/game-panel/console` },
  { label: 'Files', icon: 'tabler-folder', to: `/services/${props.service.id}/game-panel/files` },
  { label: 'Databases', icon: 'tabler-database', to: `/services/${props.service.id}/game-panel/databases` },
  { label: 'Backups', icon: 'tabler-archive', to: `/services/${props.service.id}/game-panel/backups` },
  { label: 'Schedules', icon: 'tabler-clock', to: `/services/${props.service.id}/game-panel/schedules` },
  { label: 'Startup', icon: 'tabler-rocket', to: `/services/${props.service.id}/game-panel/startup` },
  { label: 'Settings', icon: 'tabler-settings', to: `/services/${props.service.id}/game-panel/settings` },
  { label: 'Sub-Users', icon: 'tabler-users', to: `/services/${props.service.id}/game-panel/sub-users` },
])

function formatSize(bytes: number): string {
  if (bytes === 0) return '0 B'
  const k = 1024
  const sizes = ['B', 'KB', 'MB', 'GB']
  const i = Math.floor(Math.log(bytes) / Math.log(k))
  return `${parseFloat((bytes / Math.pow(k, i)).toFixed(2))} ${sizes[i]}`
}
</script>

<template>
  <ServiceTabLayout
    :service="service"
    :tabs="tabs"
    :current-tab="`/services/${service.id}/game-panel/files`"
  >
    <VCard elevation="0">
      <VCardTitle class="d-flex align-center gap-2">
        <VIcon icon="tabler-folder" color="primary" />
        File Manager
      </VCardTitle>
      <VCardSubtitle>
        <code>{{ currentPath }}</code>
      </VCardSubtitle>
      <VCardText>
        <VTable v-if="files.length > 0" hover>
          <thead>
            <tr>
              <th class="font-weight-bold">Name</th>
              <th class="font-weight-bold">Size</th>
              <th class="font-weight-bold">Modified</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="file in files" :key="file.name">
              <td>
                <div class="d-flex align-center gap-2">
                  <VIcon
                    :icon="file.is_file ? 'tabler-file' : 'tabler-folder-filled'"
                    :color="file.is_file ? 'secondary' : 'warning'"
                    size="20"
                  />
                  <span class="font-weight-medium">{{ file.name }}</span>
                </div>
              </td>
              <td>{{ file.is_file ? formatSize(file.size) : '--' }}</td>
              <td>{{ new Date(file.modified_at).toLocaleString() }}</td>
            </tr>
          </tbody>
        </VTable>

        <div v-else class="text-center pa-8 text-medium-emphasis">
          <VIcon icon="tabler-folder-off" size="48" class="mb-2" />
          <div class="text-body-1 mb-2">No files found</div>
          <div class="text-body-2">The file manager requires Pterodactyl API configuration.</div>
        </div>
      </VCardText>
    </VCard>
  </ServiceTabLayout>
</template>
