import { ref } from 'vue';
import {
    fetchDeal as apiFetchDeal,
    createDeal as apiCreateDeal
} from '@/shared/api/deal';

export default function useDeal() {
    const deal = ref(null);
    const isLoading = ref(false);
    const error = ref(null);

    const loadDeal = async (dealId) => {
        try {
            isLoading.value = true;
            const response = await apiFetchDeal(dealId);
            deal.value = response.data;
        } catch (err) {
            error.value = err.message;
        } finally {
            isLoading.value = false;
        }
    };

    const saveDeal = async (dealData) => {
        try {
            isLoading.value = true;
            const response = await apiCreateDeal(dealData);
            deal.value = response.data;
            return response;
        } catch (err) {
            error.value = err.message;
            throw err;
        } finally {
            isLoading.value = false;
        }
    };

    return {
        deal,
        isLoading,
        error,
        loadDeal,
        saveDeal
    };
}