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

interface Schedule {
  id: number
  name: string
  cron_minute: string
  cron_hour: string
  cron_day_of_month: string
  cron_month: string
  cron_day_of_week: string
  is_active: boolean
  last_run_at: string | null
  next_run_at: string | null
}

interface Props {
  service: Service
  schedules: Schedule[]
}

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/schedules`"
  >
    <VCard elevation="0">
      <VCardTitle class="d-flex align-center gap-2">
        <VIcon icon="tabler-clock" color="primary" />
        Task Schedules
      </VCardTitle>
      <VCardText>
        <VTable v-if="schedules.length > 0" hover>
          <thead>
            <tr>
              <th class="font-weight-bold">Name</th>
              <th class="font-weight-bold">Schedule</th>
              <th class="font-weight-bold">Status</th>
              <th class="font-weight-bold">Next Run</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="schedule in schedules" :key="schedule.id">
              <td class="font-weight-medium">{{ schedule.name }}</td>
              <td>
                <code>{{ schedule.cron_minute }} {{ schedule.cron_hour }} {{ schedule.cron_day_of_month }} {{ schedule.cron_month }} {{ schedule.cron_day_of_week }}</code>
              </td>
              <td>
                <VChip :color="schedule.is_active ? 'success' : 'secondary'" size="small">
                  {{ schedule.is_active ? 'Active' : 'Inactive' }}
                </VChip>
              </td>
              <td>{{ schedule.next_run_at ? new Date(schedule.next_run_at).toLocaleString() : '--' }}</td>
            </tr>
          </tbody>
        </VTable>

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