<template>
  <v-container fluid class="pa-6">
    <PushZipUpload
      :uploaded-source="form.uploadedSource"
      :validated-source="form.validatedSource"
      v-model:source-path="form.sourcePath"
      :source-path-validating="form.sourcePathValidating"
      :is-dragging="form.isDragging"
      :source-uploading="form.sourceUploading"
      @dragover="form.isDragging = true"
      @dragleave="form.isDragging = false"
      @drop="form.handleFileDrop"
      @file-select="form.handleFileSelect"
      @zip-extract-select="form.handleZipExtractSelect"
      @clear="form.clearUploadedSource"
      @clear-validated="form.clearValidatedSource"
      @validate="form.validateSourcePath"
    />

    <PushStagingPanel
      v-if="form.uploadedSource"
      :root-path="form.uploadedSource.sourcePath"
      :relative-path="staging.relativePath"
      :entries="staging.entries"
      :loading="staging.loading"
      :selected-count="staging.selectedCount"
      :all-selected="staging.allSelected"
      :some-selected="staging.someSelected"
      :is-selected="staging.isSelected"
      v-model:rename-open="staging.renameOpen"
      v-model:rename-value="staging.renameValue"
      :rename-loading="staging.renameLoading"
      v-model:delete-open="staging.deleteOpen"
      :delete-targets="staging.deleteTargets"
      :delete-loading="staging.deleteLoading"
      @refresh="staging.refreshCurrent()"
      @go-root="staging.goRoot"
      @open-dir="staging.openDirectory"
      @preview="staging.previewFile"
      @rename="staging.openRename"
      @delete="staging.openDelete"
      @batch-delete="staging.openBatchDelete"
      @toggle-select="staging.toggleSelect"
      @toggle-select-all="staging.toggleSelectAll"
      @confirm-rename="staging.confirmRename"
      @confirm-delete="staging.confirmDelete"
    />

    <PushStagingPreviewDialog
      v-model="staging.previewOpen"
      :loading="staging.previewLoading"
      :result="staging.previewResult"
    />

    <PushToolbar
      v-model:target-path="form.targetPath"
      :submitting="progress.submitting"
      :pushing="progress.pushing"
      @preview="preview.doPreview"
      @push="progress.doPush"
    />

    <PushServerGrid
      :select-all="servers.selectAll"
      :server-search="servers.serverSearch"
      :search-history="servers.serverSearchHistory"
      :servers-loading="servers.serversLoading"
      :servers-by-category="servers.serversByCategory"
      :selected-ids="servers.selectedIds"
      :push-status="servers.pushStatus"
      :is-category-all-selected="servers.isCategoryAllSelected"
      @update:server-search="servers.onServerSearchUpdate"
      @search-commit="servers.commitServerSearch"
      @toggle-all="servers.toggleAll"
      @refresh="servers.loadServers"
      @toggle-category="servers.toggleCategory"
      @toggle-server="servers.toggleServer"
    />

    <PushSyncModeCard
      v-model:sync-mode="form.syncMode"
      :sync-mode-desc="form.syncModeDesc"
    />

    <PushProgressList
      :items="progress.wsProgressItems"
      :batch-id="progress.pushBatchId"
      :pushing="progress.pushing"
      :cancelling="progress.cancelling"
      :retrying-all="progress.retryingAll"
      :failed-retry-count="progress.failedRetryCount"
      :done-count="progress.doneCount"
      :total-progress="progress.totalProgress"
      @cancel="progress.cancelPush"
      @retry-all="progress.retryAllFailed"
    />

    <PushPreviewDialog
      v-model="preview.showPreview"
      :loading="preview.previewLoading"
      :results="preview.previewResults"
      :selected-count="servers.selectedIds.size"
      :preview-done="preview.previewDone"
      :preview-total="preview.previewTotal"
      :large-batch="preview.previewLargeBatch"
      :warn-threshold="preview.previewWarnThreshold"
      :error-count="preview.previewErrorCount"
      :sync-mode="form.syncMode"
      @confirm-push="onConfirmAndPush"
    />

    <PushDiagnoseDialog
      v-model="logs.showDiagnose"
      :loading="logs.diagnoseLoading"
      :result="logs.diagnoseResult"
      :target-name="logs.diagnoseTarget?.server_name ?? undefined"
    />

    <PushHistoryTable
      :items="logs.logs"
      :headers="logs.logHeaders"
      :total="logs.logTotal"
      :loading="logs.logLoading"
      v-model:page="logs.logPage"
      v-model:status-filter="logs.logStatusFilter"
      v-model:sync-mode-filter="logs.logSyncModeFilter"
      v-model:server-filter="logs.logServerFilter"
      :status-options="logs.logStatusOptions"
      :sync-mode-options="logs.logSyncModeOptions"
      :server-options="logs.logServerOptions"
      :reconciling="logs.reconcilingLogs"
      @filter-change="logs.onLogFilterChange"
      @page-change="logs.loadLogs"
      @reconcile="logs.reconcileStaleLogs"
      @diagnose="logs.openDiagnose"
    />
  </v-container>
</template>

<script setup lang="ts">
import PushStagingPanel from '@/components/push/PushStagingPanel.vue'
import PushStagingPreviewDialog from '@/components/push/PushStagingPreviewDialog.vue'
import PushDiagnoseDialog from '@/components/push/PushDiagnoseDialog.vue'
import PushHistoryTable from '@/components/push/PushHistoryTable.vue'
import PushPreviewDialog from '@/components/push/PushPreviewDialog.vue'
import PushProgressList from '@/components/push/PushProgressList.vue'
import PushServerGrid from '@/components/push/PushServerGrid.vue'
import PushSyncModeCard from '@/components/push/PushSyncModeCard.vue'
import PushToolbar from '@/components/push/PushToolbar.vue'
import PushZipUpload from '@/components/push/PushZipUpload.vue'
import { usePushPage } from '@/composables/push'

defineOptions({ name: 'PushPage' })

const { form, staging, servers, progress, preview, logs, onConfirmAndPush } = usePushPage()
</script>
