import { api } from '@/lib/api'
import type { User, PaginatedResponse } from '@/types/api'

export const userService = {
    getAll: async (page = 1, limit = 10) => {
        const response = await api.get<PaginatedResponse<User>>('/v1/admin/users', {
            params: { page, limit },
        })
        return response.data
    },

    search: async (query: string, page = 1, limit = 10) => {
        const response = await api.get<PaginatedResponse<User>>('/v1/admin/users/search', {
            params: { q: query, page, limit },
        })
        return response.data
    },

    getById: async (id: string) => {
        const response = await api.get<User>(`/v1/admin/users/${id}`)
        return response.data
    },

    create: async (data: Partial<User> & { password?: string; roles: string[] }) => {
        const response = await api.post<User>('/v1/admin/users', data)
        return response.data
    },

    update: async (id: string, data: Partial<User>) => {
        const response = await api.put<User>(`/v1/admin/users/${id}`, data)
        return response.data
    },

    delete: async (id: string) => {
        await api.delete(`/v1/admin/users/${id}`)
    },

    getDeleted: async (page = 1, limit = 10) => {
        const response = await api.get<PaginatedResponse<User>>('/v1/admin/users/deleted', {
            params: { page, limit },
        })
        return response.data
    },

    restore: async (id: string) => {
        await api.post(`/v1/admin/users/${id}/restore`)
    },

    hardDelete: async (id: string) => {
        await api.delete(`/v1/admin/users/${id}?hard=true`)
    },

    assignRoles: async (id: string, roles: string[]) => {
        await api.post(`/v1/admin/users/${id}/roles`, { roles })
    },

    removeRole: async (id: string, role: string) => {
        await api.delete(`/v1/admin/users/${id}/roles/${role}`)
    }
}
