import { ref } from 'vue';
import axios from 'axios';

export function useActivity() {
    const activities = ref([]);
    const loading = ref(false);
    const error = ref(null);

    const fetchRecentActivities = async (limit = 10) => {
        loading.value = true;
        error.value = null;
        try {
            const response = await axios.get('/api/activity/recent', {
                params: { limit }
            });
            if (response.data.success) {
                activities.value = response.data.data;
            } else {
                error.value = 'Failed to fetch activities';
            }
        } catch (err) {
            error.value = err.message || 'Error connecting to activity service';
            console.error('Activity fetch error:', err);
        } finally {
            loading.value = false;
        }
    };

    const searchActivities = async (query, limit = 20) => {
        loading.value = true;
        error.value = null;
        try {
            const response = await axios.get('/api/activity/search', {
                params: { q: query, limit }
            });
            if (response.data.success) {
                activities.value = response.data.data;
            }
        } catch (err) {
            error.value = err.message;
        } finally {
            loading.value = false;
        }
    };

    return {
        activities,
        loading,
        error,
        fetchRecentActivities,
        searchActivities
    };
}
