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

/**
 * Composable for managing global announcements
 */
export function useAnnouncements() {
    const announcements = ref([]);
    const loading = ref(false);
    const error = ref(null);

    /**
     * Fetch the latest active announcements for users
     */
    const fetchLatest = async () => {
        loading.value = true;
        error.value = null;
        try {
            const response = await axios.get('/Announcements/Latest');
            announcements.value = response.data || [];
        } catch (err) {
            console.error('Error fetching announcements:', err);
            error.value = 'Failed to load announcements.';
        } finally {
            loading.value = false;
        }
    };

    /**
     * List all announcements (for admin management)
     */
    const fetchAllAdmin = async () => {
        loading.value = true;
        error.value = null;
        try {
            const response = await axios.post('/Admin/Announcements/List');
            return response.data || [];
        } catch (err) {
            console.error('Error fetching admin announcements:', err);
            error.value = 'Failed to fetch admin announcements.';
            return [];
        } finally {
            loading.value = false;
        }
    };

    /**
     * Store a new announcement
     */
    const storeAnnouncement = async (data) => {
        try {
            const response = await axios.post('/Admin/Announcement/Store', data);
            return response.data;
        } catch (err) {
            console.error('Error storing announcement:', err);
            throw err;
        }
    };

    /**
     * Update an announcement
     */
    const updateAnnouncement = async (data) => {
        try {
            const response = await axios.post('/Admin/Announcement/Update', data);
            return response.data;
        } catch (err) {
            console.error('Error updating announcement:', err);
            throw err;
        }
    };

    /**
     * Delete an announcement
     */
    const deleteAnnouncement = async (target) => {
        try {
            const response = await axios.post('/Admin/Announcement/Delete', { target });
            return response.data;
        } catch (err) {
            console.error('Error deleting announcement:', err);
            throw err;
        }
    };

    /**
     * Toggle active status of an announcement
     */
    const toggleStatus = async (target) => {
        try {
            const response = await axios.post('/Admin/Announcement/ToggleStatus', { target });
            return response.data;
        } catch (err) {
            console.error('Error toggling status:', err);
            throw err;
        }
    };

    /**
     * Upload an announcement photo
     */
    const uploadPhoto = async (file) => {
        const formData = new FormData();
        formData.append('file', file);
        try {
            const response = await axios.post('/File/Upload/announcement', formData, {
                headers: {
                    'Content-Type': 'multipart/form-data'
                }
            });
            return response.data;
        } catch (err) {
            console.error('Error uploading photo:', err);
            throw err;
        }
    };

    return {
        announcements,
        loading,
        error,
        fetchLatest,
        fetchAllAdmin,
        storeAnnouncement,
        updateAnnouncement,
        deleteAnnouncement,
        toggleStatus,
        uploadPhoto
    };
}
