import { defineStore } from 'pinia'

import { useApi } from '../composables/useApi.js'

// Heads (#114): the per-concept classifiers that LEARN from your tags and power
// suggestions (replacing Camie + centroid). Training runs as a background task;
// the card rehydrates status from GET /api/heads on mount so it survives
// navigation (the run lives in head_training_run server-side).
export const useHeadsStore = defineStore('heads', () => {
  const api = useApi()

  // Summary: head_count, graduated_count, last_trained_at, running_id, the
  // per-concept head table, and recent training runs.
  async function status() {
    return await api.get('/api/heads')
  }

  // (Re)train all eligible heads. One run at a time (409 if already running).
  async function train(params = {}) {
    return await api.post('/api/heads/train', { body: { params } })
  }

  // Earned auto-apply: trigger a sweep. dry_run previews (writes nothing);
  // a real sweep needs head_auto_apply_enabled on (else 400).
  async function autoApply(dryRun = false) {
    return await api.post('/api/heads/auto-apply', { body: { dry_run: dryRun } })
  }

  // Recent sweeps + per-concept report (volume / projected per head).
  async function autoApplyStatus() {
    return await api.get('/api/heads/auto-apply')
  }

  // Observability: per-concept counts (volume, misfires, under-fires, realized
  // misfire rate, head quality) + the daily time-series, to tune from.
  async function metrics() {
    return await api.get('/api/heads/metrics')
  }

  return { status, train, autoApply, autoApplyStatus, metrics }
})
