import api from './api';

const userService = {
    /**
     * Get all users
     */
    getAll: async () => {
        const response = await api.get('/users');
        return response.data.data;
    },

    /**
     * Get user by ID
     */
    getById: async (id) => {
        const response = await api.get(`/users/${id}`);
        return response.data.data;
    },

    /**
     * Create new user
     */
    create: async (userData) => {
        const response = await api.post('/users', userData);
        return response.data.data;
    },

    /**
     * Update user
     */
    update: async (id, userData) => {
        const response = await api.put(`/users/${id}`, userData);
        return response.data.data;
    },

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

    /**
     * Assign role to user
     */
    assignRole: async (id, roleId) => {
        const response = await api.put(`/users/${id}/role`, { role_id: roleId });
        return response.data.data;
    },

    /**
     * Activate/Deactivate user
     */
    toggleStatus: async (id, isActive) => {
        const response = await api.put(`/users/${id}/activate`, { is_active: isActive });
        return response.data.data;
    },

    /**
     * Get all roles
     */
    getRoles: async () => {
        const response = await api.get('/users/roles');
        return response.data.data;
    },

    /**
     * Get available Azure AD groups for import
     */
    getAzureGroups: async () => {
        const response = await api.get('/users/import/azure/groups');
        return response.data.data;
    },

    /**
     * Import users from an Azure AD group
     * Returns { imported, skipped, errors }
     */
    importFromAzure: async (groupId, roleId) => {
        const response = await api.post('/users/import/azure', { group_id: groupId, role_id: roleId });
        return response.data.data;
    },

    /**
     * Get audit logs
     */
    getAuditLogs: async (limit = 100, offset = 0) => {
        const response = await api.get(`/users/audit-logs?limit=${limit}&offset=${offset}`);
        return response.data.data;
    },
};

export default userService;
