import { api } from './api' import type { LoginResponse, Project, ProjectMember, MemberRole, UserListItem, MenuItem, Role, SaveRoleRequest, MasterDataItem, SaveMasterDataRequest, PagedResult, UserRoleDetail, User, ActionLog, LoginLog, ActionLogFilters, LoginLogFilters, } from '../types' export async function login(username: string, password: string): Promise { const { data } = await api.post('/api/auth/login', { username, password }) return data } export async function getMenu(): Promise { const { data } = await api.get('/api/settings/permission/menu') return data } export async function getRoles(page = 1, pageSize = 20): Promise> { const { data } = await api.get>('/api/settings/permission', { params: { page, pageSize } }) return data } export async function getRole(id: string): Promise { const { data } = await api.get(`/api/settings/permission/${id}`) return data } export async function createRole(payload: SaveRoleRequest): Promise { const { data } = await api.post('/api/settings/permission', payload) return data } export async function updateRole(id: string, payload: SaveRoleRequest): Promise { const { data } = await api.put(`/api/settings/permission/${id}`, payload) return data } export async function deleteRole(id: string): Promise { await api.delete(`/api/settings/permission/${id}`) } export async function getMasterDataList(params: { group?: string q?: string isActive?: boolean page?: number pageSize?: number } = {}): Promise> { const { data } = await api.get>('/api/masterdata', { params }) return data } export async function getMasterDataByGroup(group: string): Promise { const { data } = await api.get(`/api/masterdata/groups/${group}`) return data } export async function getMasterDataOptions(group: string): Promise<{ label: string; value: string }[]> { const items = await getMasterDataByGroup(group) return items.map((i) => ({ label: i.label, value: i.value })) } export async function createMasterData(payload: SaveMasterDataRequest): Promise { const { data } = await api.post('/api/masterdata', payload) return data } export async function updateMasterData(id: string, payload: SaveMasterDataRequest): Promise { const { data } = await api.put(`/api/masterdata/${id}`, payload) return data } export async function deleteMasterData(id: string): Promise { await api.delete(`/api/masterdata/${id}`) } export async function getProjects(params: { q?: string status?: string createdBy?: string updatedBy?: string page?: number pageSize?: number } = {}): Promise> { const { data } = await api.get>('/api/projects', { params }) return data } export async function searchProjects(q: string): Promise { const { data } = await api.get('/api/projects/search', { params: { q } }) return data } export async function getProject(id: string): Promise { const { data } = await api.get(`/api/projects/${id}`) return data } export async function createProject(name: string, description?: string): Promise { const { data } = await api.post('/api/projects', { name, description }) return data } export async function updateProject( id: string, payload: { name: string; description: string | null; status: string }, ): Promise { const { data } = await api.put(`/api/projects/${id}`, payload) return data } export async function deleteProject(id: string): Promise { await api.delete(`/api/projects/${id}`) } export async function getMembers( projectId: string, params: { q?: string; role?: string; page?: number; pageSize?: number } = {}, ): Promise> { const { data } = await api.get>(`/api/projects/${projectId}/members`, { params }) return data } export async function addMember( projectId: string, userId: string, role: MemberRole, ): Promise { const { data } = await api.post(`/api/projects/${projectId}/members`, { userId, role }) return data } export async function removeMember(projectId: string, userId: string): Promise { await api.delete(`/api/projects/${projectId}/members/${userId}`) } export async function updateMemberDocumentPermissions( projectId: string, userId: string, payload: { canViewDocuments: boolean canCreateDocuments: boolean canEditDocuments: boolean canDeleteDocuments: boolean }, ): Promise { const { data } = await api.put(`/api/projects/${projectId}/members/${userId}/document-permissions`, payload) return data } export async function getProfile(): Promise { const { data } = await api.get('/api/auth/me') return data } export async function getUsers(q?: string): Promise { const { data } = await api.get('/api/users/list', { params: { q } }) return data } export async function getUsersPaged(params: { q?: string isActive?: boolean page?: number pageSize?: number } = {}): Promise> { const { data } = await api.get>('/api/users', { params }) return data } export async function createUser(payload: { username: string displayName: string password: string roleId?: string }): Promise { const { data } = await api.post('/api/users', payload) return data } export async function updateUser( id: string, payload: { displayName: string; roleId?: string; isActive: boolean }, ): Promise { const { data } = await api.put(`/api/users/${id}`, payload) return data } export async function deleteUser(id: string): Promise { await api.delete(`/api/users/${id}`) } export async function resetUserPassword(id: string, newPassword: string): Promise { await api.post(`/api/users/${id}/reset-password`, { newPassword }) } export async function getUserRoles(userId: string): Promise { const { data } = await api.get(`/api/users/${userId}/roles`) return data } export async function assignUserRole(userId: string, roleId: string): Promise { await api.post(`/api/users/${userId}/roles`, { roleId }) } export async function unassignUserRole(userId: string, roleId: string): Promise { await api.delete(`/api/users/${userId}/roles/${roleId}`) } export async function getUserPermissions(userId: string): Promise { const { data } = await api.get(`/api/users/${userId}/permissions`) return data } export async function getActionLogs(params: ActionLogFilters = {}): Promise> { const { data } = await api.get>('/api/audit/actions', { params }) return data } export async function getLoginLogs(params: LoginLogFilters = {}): Promise> { const { data } = await api.get>('/api/audit/logins', { params }) return data }