<template>
  <!-- #713: re-extract PostAttachments that are really archives but were filed
       opaquely before the magic-byte gate, and attach their images to the post. -->
  <MaintenanceTile
    icon="mdi-folder-zip-outline"
    title="Re-extract archives"
    blurb="Extract images from zip attachments filed opaquely."
    :open="busy"
  >
      <p class="text-body-2 mb-3">
        Some posts attached an archive (zip) whose images weren't extracted
        because the downloaded file had no usable extension. This scans existing
        attachments, extracts any that are really archives, and attaches their
        images to the post. Idempotent — safe to run more than once.
      </p>
      <v-btn color="primary" rounded="pill" :loading="busy" @click="run">
        <v-icon start>mdi-folder-zip-outline</v-icon> Re-extract archives now
      </v-btn>
      <span v-if="queued" class="ml-3 text-caption text-success">Queued ✓</span>
      <QueueStatusBar queue="maintenance" queue-label="Maintenance" />
  </MaintenanceTile>
</template>

<script setup>
import { ref } from 'vue'

import { useApi } from '../../composables/useApi.js'
import { toast } from '../../utils/toast.js'
import MaintenanceTile from '../common/MaintenanceTile.vue'
import QueueStatusBar from './QueueStatusBar.vue'

const api = useApi()
const busy = ref(false)
const queued = ref(false)

async function run () {
  busy.value = true
  queued.value = false
  try {
    await api.post('/api/admin/maintenance/reextract-archives')
    queued.value = true
    toast({ text: 'Archive re-extract queued', type: 'success' })
  } catch (e) {
    toast({ text: e?.body?.detail || e?.message || 'Failed to queue', type: 'error' })
  } finally {
    busy.value = false
  }
}
</script>
