import { computed } from 'vue'
import { defineStore } from 'pinia'
import { api } from 'boot/axios'
import { useProjectsStore } from 'stores/projects'
import type { FileLink } from 'types/FileLink'

export const useFilesStore = defineStore('files', () => {
  const projectsStore = useProjectsStore()
  const currentProjectId = computed(() => projectsStore.currentProjectId)
  
  async function getFilesList (projectId: number) {
    const { data } = await api.get(`/project/${projectId}/file`)
    return data.data as FileLink[]
  }

  async function downloadParams (fileId: number) {
    const { data } = await api.get(`/project/${currentProjectId.value}/file/${fileId}/token`)
    return data.data 
      ? {
          url: `/api/download/${data.data.token}`,
          file_name: data.data.filename as string
        }
      : null
  }

  return {
    getFilesList,
    downloadParams
  }
})
