import { api } from "src/boot/axios";
import type { BackendResponse } from "src/modules/shared/models/backend-response.models";
import type { Expense } from "src/modules/timesheets/models/expense.models";

export const ExpenseService = {
    createExpense: async (expense: Expense): Promise<{ success: boolean, data: Expense, error?: unknown }> => {
        const response = await api.post('expense/create', expense);
        return response.data;
    },

    updateExpense: async (expense: Expense, email?: string): Promise<{ success: boolean, data: Expense, error?: unknown }> => {
        const response = await api.patch(`expense/update${email ? '?employee_email=' + email : ''}`, expense);
        return response.data;
    },

    deleteExpenseById: async (expense_id: number): Promise<{ success: boolean, data: number, error?: unknown }> => {
        const response = await api.delete(`expense/delete/${expense_id}`);
        return response.data;
    },

    getPresignedUploadURL: async (file: File, checksum_crc32: string): Promise<BackendResponse<string>> => {
        const [file_name, file_type] = file.name.split('.');
        const response = await api.post(`attachments/s3/upload?file-name=${file_name}&file-type=${file_type}&checksumCRC32=${checksum_crc32}`);
        return response.data;
    },

    uploadAttachmentWithPresignedUrl: async (file: File, url: string) => {
        const response = await api.put(url, file, { headers: { 'Content-Type': file.type, }, withCredentials: false });
        console.log('response to upload: ', response);
    }
};