// src/stores/downloadstHeadStore.js
import { defineStore } from 'pinia'
import { download } from 'src/services/api'

export const useDownloadstHeadStore = defineStore('downloadstHead', {
  actions: {
    // 📄 Statement Header PDF indir / aç
    async handlestHeadDownload (
      accountCode,
      startDate,
      endDate,
      parislemler
    ) {
      try {
        // ✅ Params (axios paramsSerializer array=repeat destekliyor)
        const params = {
          accountcode: accountCode,
          startdate: startDate,
          enddate: endDate
        }

        if (Array.isArray(parislemler) && parislemler.length > 0) {
          params.parislemler = parislemler.filter(
            p => p !== undefined && p !== null && p !== ''
          )
        }

        // 🔥 API CALL (TOKEN + BLOB + ERROR HANDLING OTOMATİK)
        const blob = await download(
          '/exportstamentheaderreport-pdf',
          params
        )

        const pdfUrl = window.URL.createObjectURL(blob)
        window.open(pdfUrl, '_blank')

        return { ok: true, message: '📄 PDF hazırlandı' }

      } catch (err) {
        console.error('❌ PDF açma hatası:', err)
        return {
          ok: false,
          message:
            err?.message ||
            '❌ PDF açma hatası'
        }
      }
    }
  }
})
