import api from './api';

const ticketService = {
    getAll: async (filters = {}) => {
        const params = {};
        if (filters.status) params.status = filters.status;
        if (filters.priority) params.priority = filters.priority;
        if (filters.category) params.category = filters.category;
        if (filters.assigned_to) params.assigned_to = filters.assigned_to;
        if (filters.search) params.search = filters.search;
        const response = await api.get('/tickets', { params });
        return response.data.data;
    },

    getById: async (id) => {
        const response = await api.get(`/tickets/${id}`);
        return response.data.data;
    },

    getStats: async () => {
        const response = await api.get('/tickets/stats');
        return response.data.data;
    },

    getMetrics: async (days = 30) => {
        const response = await api.get('/tickets/metrics', { params: { days } });
        return response.data.data;
    },

    create: async (data) => {
        const response = await api.post('/tickets', data);
        return response.data.data;
    },

    update: async (id, data) => {
        const response = await api.put(`/tickets/${id}`, data);
        return response.data.data;
    },

    bulkUpdate: async (ids, data) => {
        const response = await api.put('/tickets/bulk', { ids, data });
        return response.data;
    },

    delete: async (id) => {
        const response = await api.delete(`/tickets/${id}`);
        return response.data;
    },

    addComment: async (id, comment, isInternal = false) => {
        const response = await api.post(`/tickets/${id}/comments`, {
            comment,
            is_internal: isInternal,
        });
        return response.data.data;
    },

    aiReply: async (id, message, history = []) => {
        const response = await api.post(`/tickets/${id}/ai-reply`, { message, history });
        return response.data.data;
    },

    deleteComment: async (ticketId, commentId) => {
        const response = await api.delete(`/tickets/${ticketId}/comments/${commentId}`);
        return response.data;
    },

    getHistory: async (id) => {
        const response = await api.get(`/tickets/${id}/history`);
        return response.data.data;
    },

    getPdfUrl: (id) => `/api/tickets/${id}/pdf`,

    getLinks: async (id) => {
        const response = await api.get(`/tickets/${id}/links`);
        return response.data.data;
    },

    addLink: async (id, linked_ticket_number, link_type = 'related') => {
        const response = await api.post(`/tickets/${id}/links`, { linked_ticket_number, link_type });
        return response.data.data;
    },

    removeLink: async (ticketId, linkId) => {
        const response = await api.delete(`/tickets/${ticketId}/links/${linkId}`);
        return response.data;
    },

    snooze: async (id, snoozed_until) => {
        const response = await api.put(`/tickets/${id}/snooze`, { snoozed_until });
        return response.data.data;
    },

    addAssignee: async (ticketId, userId) => {
        const response = await api.post(`/tickets/${ticketId}/assignees`, { user_id: userId });
        return response.data.data;
    },

    removeAssignee: async (ticketId, userId) => {
        const response = await api.delete(`/tickets/${ticketId}/assignees/${userId}`);
        return response.data.data;
    },
};

export default ticketService;
