// filepath: src/stores/analysis.ts
import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useAnalysisStore = defineStore('analysis', () => {
  const url = ref('')
  const loading = ref(false)
  const error = ref('')
  const metadata = ref<any>(null)
  
  // New state for mix detection
  const isMix = ref(false)
  const scanMix = ref(false)
  
  // Input mode state
  const isBatchMode = ref(false)

  const options = ref({
    is_audio_only: false,
    quality: 'best',
    output_path: '',
    output_format: 'original',
    cookies_path: ''
  })

  function toggleEntry(id: string) {
      if (metadata.value && metadata.value.entries) {
          const entry = metadata.value.entries.find((e: any) => e.id === id)
          if (entry) {
              entry.selected = !entry.selected
          }
      }
  }

  function setAllEntries(selected: boolean) {
      if (metadata.value && metadata.value.entries) {
          metadata.value.entries = metadata.value.entries.map((e: any) => ({
              ...e,
              selected
          }))
      }
  }

  function invertSelection() {
      if (metadata.value && metadata.value.entries) {
          metadata.value.entries = metadata.value.entries.map((e: any) => ({
              ...e,
              selected: !e.selected
          }))
      }
  }

  function reset() {
      url.value = ''
      loading.value = false
      error.value = ''
      metadata.value = null
      isMix.value = false
      scanMix.value = false
  }

  return { url, loading, error, metadata, options, isMix, scanMix, isBatchMode, toggleEntry, setAllEntries, invertSelection, reset }
})