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

interface Backup {
  id: string
  name: string
  size: number
  is_successful: boolean
  is_locked: boolean
  created_at: string
  completed_at: string | null
}

interface Props {
  service: Service
  backups: Backup[]
}

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/backups`"
  >
    <VCard elevation="0">
      <VCardTitle class="d-flex align-center gap-2">
        <VIcon icon="tabler-archive" color="primary" />
        Backups
      </VCardTitle>
      <VCardText>
        <VTable v-if="backups.length > 0" hover>
          <thead>
            <tr>
              <th class="font-weight-bold">Name</th>
              <th class="font-weight-bold">Size</th>
              <th class="font-weight-bold">Status</th>
              <th class="font-weight-bold">Created</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="backup in backups" :key="backup.id">
              <td class="font-weight-medium">
                {{ backup.name }}
                <VIcon v-if="backup.is_locked" icon="tabler-lock" size="16" class="ms-1" color="warning" />
              </td>
              <td>{{ formatSize(backup.size) }}</td>
              <td>
                <VChip
                  :color="backup.is_successful ? 'success' : 'error'"
                  size="small"
                >
                  {{ backup.is_successful ? 'Complete' : 'Failed' }}
                </VChip>
              </td>
              <td>{{ new Date(backup.created_at).toLocaleString() }}</td>
            </tr>
          </tbody>
        </VTable>

        <div v-else class="text-center pa-8 text-medium-emphasis">
          <VIcon icon="tabler-archive-off" size="48" class="mb-2" />
          <div class="text-body-1 mb-2">No backups</div>
          <div class="text-body-2">Backup management requires Pterodactyl API configuration.</div>
        </div>
      </VCardText>
    </VCard>
  </ServiceTabLayout>
</template>
