import { defineStore } from 'pinia';
import { ref } from 'vue';
import { FileUploadService } from '../service/FileUploadService';

export const FileUploadStore = defineStore('file_upload_store', () => {
    const uploadedFiles = ref([]);
    const folderName = ref('');
    const numberPrFiles = ref(0);

    // Funzione per eliminare un file
    async function deleteFile(fileName, folderName) {
        try {
            const response = await FileUploadService.deleteFile(fileName, folderName);
            return response;
        } catch (error) {
            console.error('Error deleting file:', error);
            throw error;
        }
    }

    // Funzione per generare un ID univoco per la cartella
    function generateUniqueFolderId() {
        const timestamp = Date.now();
        const randomNumber = Math.floor(Math.random() * 1000);
        const newFolderName = `${timestamp}_${randomNumber}`;
        folderName.value = newFolderName;
        return newFolderName;
    }

    // Funzione per resettare lo store
    function resetStore() {
        uploadedFiles.value = [];
        folderName.value = '';
        numberPrFiles.value = 0;
    }

    // Funzione per aggiornare il numero di file PR
    function updatePrFilesCount(increment = true) {
        if (increment) {
            numberPrFiles.value += 1;
        } else {
            numberPrFiles.value -= 1;
        }
    }

    return {
        uploadedFiles,
        folderName,
        numberPrFiles,
        deleteFile,
        generateUniqueFolderId,
        resetStore,
        updatePrFilesCount
    };
});
