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

interface Database {
  id: string
  name: string
  host: string
  port: number
  username: string
  max_connections: number
}

interface Props {
  service: Service
  databases: Database[]
}

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` },
])
</script>

<template>
  <ServiceTabLayout
    :service="service"
    :tabs="tabs"
    :current-tab="`/services/${service.id}/game-panel/databases`"
  >
    <VCard elevation="0">
      <VCardTitle class="d-flex align-center gap-2">
        <VIcon icon="tabler-database" color="primary" />
        Databases
      </VCardTitle>
      <VCardText>
        <VTable v-if="databases.length > 0" hover>
          <thead>
            <tr>
              <th class="font-weight-bold">Name</th>
              <th class="font-weight-bold">Host</th>
              <th class="font-weight-bold">Username</th>
              <th class="font-weight-bold">Connections</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="db in databases" :key="db.id">
              <td class="font-weight-medium">{{ db.name }}</td>
              <td><code>{{ db.host }}:{{ db.port }}</code></td>
              <td><code>{{ db.username }}</code></td>
              <td>{{ db.max_connections }}</td>
            </tr>
          </tbody>
        </VTable>

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