<template>
  <v-card>
    <v-card-title>Trigger scan</v-card-title>
    <v-card-text>
      <div v-if="store.activeBatch" class="d-flex align-center mb-3" style="gap: 12px;">
        <v-progress-circular
          indeterminate color="accent" size="20"
        />
        <span>
          Scanning {{ store.activeBatch.source_path }} —
          imported {{ store.activeBatch.imported }},
          skipped {{ store.activeBatch.skipped }},
          failed {{ store.activeBatch.failed }} /
          {{ store.activeBatch.total_files }} files
        </span>
        <v-spacer />
        <v-btn
          variant="text" rounded="pill" size="small" color="warning"
          :loading="clearing" @click="onClearStuck"
        >
          Clear stuck
        </v-btn>
      </div>

      <p class="text-body-2 mb-3">
        <span v-if="!store.activeBatch">
          Run a quick scan of the import directory. Deep scan (pHash dedup,
          archives) lands in FC-2d.
        </span>
        <span v-else>
          An active batch is in progress. Wait for it to finish, or click
          <em>Clear stuck</em> above if it has been wedged with no
          measurable progress.
        </span>
      </p>

      <v-btn
        color="primary" rounded="pill"
        :disabled="!!store.activeBatch"
        :loading="busy"
        @click="trigger"
      >
        <v-icon start>mdi-magnify-scan</v-icon>
        Quick scan
      </v-btn>

      <v-alert v-if="store.triggerError" type="error" variant="tonal" class="mt-3" closable>
        {{ store.triggerError }}
      </v-alert>
    </v-card-text>
  </v-card>
</template>

<script setup>
import { ref } from 'vue'
import { useImportStore } from '../../stores/import.js'

const store = useImportStore()
const busy = ref(false)
const clearing = ref(false)

async function trigger() {
  busy.value = true
  try { await store.triggerScan() } catch {} finally { busy.value = false }
}

async function onClearStuck() {
  clearing.value = true
  try {
    await store.clearStuck()
  } catch {
    // store surfaces error via triggerError if needed
  } finally {
    clearing.value = false
  }
}
</script>
