import { defineStore } from 'pinia'
import { ref } from 'vue'
import { useApi } from '../composables/useApi.js'

export const useCleanupStore = defineStore('cleanup', () => {
  const api = useApi()

  // Defaults sourced from ImportSettings on mount. Cards pre-fill from
  // these so the common case ("apply current import filters
  // retroactively") is one click; operator can override per-audit.
  const defaults = ref({
    min_width: 0,
    min_height: 0,
    transparency_threshold: 0.9,
    single_color_threshold: 0.95,
    single_color_tolerance: 30,
  })

  const recentRuns = ref([])

  async function loadDefaults() {
    const s = await api.get('/api/settings/import')
    defaults.value = {
      min_width: s.min_width ?? 0,
      min_height: s.min_height ?? 0,
      transparency_threshold: s.transparency_threshold ?? 0.9,
      single_color_threshold: s.single_color_threshold ?? 0.95,
      single_color_tolerance: s.single_color_tolerance ?? 30,
    }
  }

  async function previewMinDim(min_width, min_height) {
    return await api.post('/api/cleanup/min-dimension/preview', {
      body: { min_width, min_height },
    })
  }

  async function deleteMinDim(min_width, min_height, confirm) {
    return await api.post('/api/cleanup/min-dimension/delete', {
      body: { min_width, min_height, confirm },
    })
  }

  async function startAudit(rule, params) {
    return await api.post('/api/cleanup/audit', { body: { rule, params } })
  }

  async function getAudit(id) {
    return await api.get(`/api/cleanup/audit/${id}`)
  }

  async function loadHistory(limit = 20) {
    const body = await api.get(`/api/cleanup/audit?limit=${limit}`)
    recentRuns.value = body.runs
    return body.runs
  }

  // The most recent audit run for a given rule, or null. Cards call this on
  // mount to reconnect to a scan that's still running (or to show the last
  // completed result) after the user navigates away and back.
  async function latestAuditForRule(rule) {
    const body = await api.get('/api/cleanup/audit', { params: { rule, limit: 1 } })
    return (body.runs && body.runs[0]) || null
  }

  async function applyAudit(id, confirm) {
    return await api.post(`/api/cleanup/audit/${id}/apply`, { body: { confirm } })
  }

  async function cancelAudit(id) {
    return await api.post(`/api/cleanup/audit/${id}/cancel`)
  }

  return {
    defaults, recentRuns,
    loadDefaults,
    previewMinDim, deleteMinDim,
    startAudit, getAudit, loadHistory, latestAuditForRule, applyAudit, cancelAudit,
  }
})
