<script setup lang="ts">
import type { AdminNotificationTestResult, AdminScheduledJobRuntimeState } from '@/types/admin'
import type { EditableNotificationRecipient, EditableScheduledJob } from '../../composables/types'
import AdminNotificationInfoCard from './AdminNotificationInfoCard.vue'
import AdminNotificationBarkCard from './AdminNotificationBarkCard.vue'
import AdminNotificationScheduledJobsCard from './AdminNotificationScheduledJobsCard.vue'
import AdminNotificationTestCard from './AdminNotificationTestCard.vue'

type NotificationForm = {
  enabled: boolean
  barkEnabled: boolean
  barkServerUrl: string
  testTitle: string
  testBody: string
  testUrl: string
}

type ScheduledJobsForm = {
  enabled: boolean
}

type NotificationStats = {
  configuredRecipientCount: number
  enabledRecipientCount: number
  barkReady: boolean
  lastSuccessCount: number
  lastFailedCount: number
  scheduledJobEnabledCount: number
}

type Props = {
  notificationFilePath: string
  notificationForm: NotificationForm
  notificationRecipients: EditableNotificationRecipient[]
  notificationSaving: boolean
  notificationTesting: boolean
  notificationResultError: string
  notificationTestResult: AdminNotificationTestResult | null
  scheduledJobsFilePath: string
  scheduledJobsForm: ScheduledJobsForm
  scheduledJobs: EditableScheduledJob[]
  scheduledJobRuntime: AdminScheduledJobRuntimeState[]
  scheduledJobsSaving: boolean
  scheduledJobRunningId: string
  notificationStats: NotificationStats
  addNotificationRecipient: () => void
  removeNotificationRecipient: (id: string) => void
  handleNotificationSaveConfig: () => void | Promise<void>
  handleNotificationTest: () => void | Promise<void>
  handleScheduledJobsSaveConfig: () => void | Promise<void>
  handleScheduledJobRunNow: (jobId: string) => void | Promise<void>
  getScheduledJobRuntime: (jobId: string) => AdminScheduledJobRuntimeState | null
}

defineProps<Props>()
</script>

<template>
  <div class="platform-section">
    <AdminNotificationInfoCard
      :notification-file-path="notificationFilePath"
      :scheduled-jobs-file-path="scheduledJobsFilePath"
    />

    <AdminNotificationBarkCard
      :notification-form="notificationForm"
      :notification-recipients="notificationRecipients"
      :notification-saving="notificationSaving"
      :notification-result-error="notificationResultError"
      :notification-stats="notificationStats"
      :add-notification-recipient="addNotificationRecipient"
      :remove-notification-recipient="removeNotificationRecipient"
      :handle-notification-save-config="handleNotificationSaveConfig"
    />

    <AdminNotificationScheduledJobsCard
      :scheduled-jobs-form="scheduledJobsForm"
      :scheduled-jobs="scheduledJobs"
      :scheduled-job-runtime="scheduledJobRuntime"
      :scheduled-jobs-saving="scheduledJobsSaving"
      :scheduled-job-running-id="scheduledJobRunningId"
      :handle-scheduled-jobs-save-config="handleScheduledJobsSaveConfig"
      :handle-scheduled-job-run-now="handleScheduledJobRunNow"
      :get-scheduled-job-runtime="getScheduledJobRuntime"
    />

    <AdminNotificationTestCard
      :notification-form="notificationForm"
      :notification-testing="notificationTesting"
      :notification-test-result="notificationTestResult"
      :notification-stats="notificationStats"
      :handle-notification-test="handleNotificationTest"
    />
  </div>
</template>

<style scoped>
.platform-section {
  display: grid;
  gap: var(--space-4);
}
</style>
