import apiClient from './client'

export interface User {
  id: string
  username: string
  email: string
  full_name: string
  is_active: boolean
  is_system: boolean
  created_at: string
  updated_at: string
  last_login_at: string | null
  roles?: string[]
  permissions?: string[]
  groups?: string[]
}

export interface Group {
  id: string
  name: string
  description?: string
  is_system: boolean
  user_count: number
  role_count: number
  created_at: string
  updated_at: string
  users?: string[]
  roles?: string[]
}

export interface CreateGroupRequest {
  name: string
  description?: string
}

export interface UpdateGroupRequest {
  name?: string
  description?: string
}

export interface AddUserToGroupRequest {
  user_id: string
}

export interface CreateUserRequest {
  username: string
  email: string
  password: string
  full_name?: string
}

export interface UpdateUserRequest {
  email?: string
  full_name?: string
  is_active?: boolean
  roles?: string[]
  groups?: string[]
}

export const iamApi = {
  listUsers: async (): Promise<User[]> => {
    const response = await apiClient.get<{ users: User[] }>('/iam/users')
    return response.data.users || []
  },

  getUser: async (id: string): Promise<User> => {
    const response = await apiClient.get<{
      id: string
      username: string
      email: string
      full_name: string
      is_active: boolean
      is_system: boolean
      roles: string[]
      permissions: string[]
      groups: string[]
      created_at: string
      updated_at: string
      last_login_at: string | null
    }>(`/iam/users/${id}`)
    return response.data
  },

  createUser: async (data: CreateUserRequest): Promise<{ id: string; username: string }> => {
    const response = await apiClient.post<{ id: string; username: string }>('/iam/users', data)
    return response.data
  },

  updateUser: async (id: string, data: UpdateUserRequest): Promise<void> => {
    await apiClient.put(`/iam/users/${id}`, data)
  },

  deleteUser: async (id: string): Promise<void> => {
    await apiClient.delete(`/iam/users/${id}`)
  },

  // Groups API
  listGroups: async (): Promise<Group[]> => {
    const response = await apiClient.get<{ groups: Group[] }>('/iam/groups')
    return response.data.groups || []
  },

  getGroup: async (id: string): Promise<Group> => {
    const response = await apiClient.get<Group>(`/iam/groups/${id}`)
    return response.data
  },

  createGroup: async (data: CreateGroupRequest): Promise<{ id: string; name: string }> => {
    const response = await apiClient.post<{ id: string; name: string }>('/iam/groups', data)
    return response.data
  },

  updateGroup: async (id: string, data: UpdateGroupRequest): Promise<void> => {
    await apiClient.put(`/iam/groups/${id}`, data)
  },

  deleteGroup: async (id: string): Promise<void> => {
    await apiClient.delete(`/iam/groups/${id}`)
  },

  addUserToGroup: async (groupId: string, userId: string): Promise<void> => {
    await apiClient.post(`/iam/groups/${groupId}/users`, { user_id: userId })
  },

  removeUserFromGroup: async (groupId: string, userId: string): Promise<void> => {
    await apiClient.delete(`/iam/groups/${groupId}/users/${userId}`)
  },

  // User role assignment
  assignRoleToUser: async (userId: string, roleName: string): Promise<void> => {
    await apiClient.post(`/iam/users/${userId}/roles`, { role_name: roleName })
  },

  removeRoleFromUser: async (userId: string, roleName: string): Promise<void> => {
    await apiClient.delete(`/iam/users/${userId}/roles?role_name=${encodeURIComponent(roleName)}`)
  },

  // User group assignment
  assignGroupToUser: async (userId: string, groupName: string): Promise<void> => {
    await apiClient.post(`/iam/users/${userId}/groups`, { group_name: groupName })
  },

  removeGroupFromUser: async (userId: string, groupName: string): Promise<void> => {
    await apiClient.delete(`/iam/users/${userId}/groups?group_name=${encodeURIComponent(groupName)}`)
  },

  // List all available roles
  listRoles: async (): Promise<Array<{ id: string; name: string; description?: string; is_system: boolean; user_count?: number; created_at?: string; updated_at?: string }>> => {
    const response = await apiClient.get<{ roles: Array<{ id: string; name: string; description?: string; is_system: boolean; user_count?: number; created_at?: string; updated_at?: string }> }>('/iam/roles')
    return response.data.roles
  },

  getRole: async (id: string): Promise<{ id: string; name: string; description?: string; is_system: boolean; user_count?: number; created_at?: string; updated_at?: string }> => {
    const response = await apiClient.get<{ id: string; name: string; description?: string; is_system: boolean; user_count?: number; created_at?: string; updated_at?: string }>(`/iam/roles/${id}`)
    return response.data
  },

  createRole: async (data: { name: string; description?: string }): Promise<{ id: string; name: string }> => {
    const response = await apiClient.post<{ id: string; name: string }>('/iam/roles', data)
    return response.data
  },

  updateRole: async (id: string, data: { name?: string; description?: string }): Promise<void> => {
    await apiClient.put(`/iam/roles/${id}`, data)
  },

  deleteRole: async (id: string): Promise<void> => {
    await apiClient.delete(`/iam/roles/${id}`)
  },

  // Role permissions
  getRolePermissions: async (roleId: string): Promise<string[]> => {
    const response = await apiClient.get<{ permissions: string[] }>(`/iam/roles/${roleId}/permissions`)
    return response.data.permissions
  },

  assignPermissionToRole: async (roleId: string, permissionName: string): Promise<void> => {
    await apiClient.post(`/iam/roles/${roleId}/permissions`, { permission_name: permissionName })
  },

  removePermissionFromRole: async (roleId: string, permissionName: string): Promise<void> => {
    await apiClient.delete(`/iam/roles/${roleId}/permissions?permission_name=${encodeURIComponent(permissionName)}`)
  },

  // Permissions
  listPermissions: async (): Promise<Array<{ id: string; name: string; resource: string; action: string; description?: string }>> => {
    const response = await apiClient.get<{ permissions: Array<{ id: string; name: string; resource: string; action: string; description?: string }> }>('/iam/permissions')
    return response.data.permissions
  },
}

