export type AdminUserStatus = "active" | "disabled" | "invited"

export type AdminUserRole = "admin" | "user" | "guest" | "suspended"

export type AdminUserGroupRef = {
  id: string
  name: string
}

export type AdminUser = {
  id: string
  external_id: string
  email: string
  name: string
  status: AdminUserStatus
  platform_admin: boolean
  role: AdminUserRole
  groups?: AdminUserGroupRef[]
  storage?: AdminUserStorage
  invited_at?: string | null
  disabled_at?: string | null
  created_at: string
  updated_at: string
}

export type AdminServiceQuota = {
  count?: number
  used_storage_bytes: number
  max_storage_bytes: number
}

export type AdminUserQuota = {
  mail: AdminServiceQuota
  drive: AdminServiceQuota
  photos: AdminServiceQuota
}

export type AdminUserDetail = AdminUser & {
  quota?: AdminUserQuota
}

export type AdminPagination = {
  page: number
  page_size: number
  total: number
}

export type AdminUsersListResponse = {
  users: AdminUser[]
  pagination: AdminPagination
}

export type AdminUserGroup = {
  id: string
  name: string
  description: string
  member_count: number
  created_at: string
  updated_at: string
}

export type AdminUserGroupsListResponse = {
  groups: AdminUserGroup[]
  pagination: AdminPagination
}

export type AdminCreateUserGroupRequest = {
  name: string
  description?: string
}

export type AdminUpdateUserGroupRequest = {
  name?: string
  description?: string
}

export type AdminSetUserGroupMembersRequest = {
  user_ids: string[]
}

export type AdminBulkUsersAction =
  | "disable"
  | "reactivate"
  | "delete"
  | "set_role"
  | "add_to_group"
  | "remove_from_group"

export type AdminBulkUsersRequest = {
  user_ids: string[]
  action: AdminBulkUsersAction
  role?: AdminUserRole
  group_id?: string
}

export type AdminBulkUsersFailure = {
  user_id: string
  message: string
}

export type AdminBulkUsersResponse = {
  success_count: number
  failed?: AdminBulkUsersFailure[]
}

export type AdminAuditLog = {
  id: string
  actor: string
  action: string
  details: unknown
  created_at: string
}

export type AdminAuditListResponse = {
  logs: AdminAuditLog[]
  pagination: AdminPagination
}

export type AdminStatsTopActor = {
  actor: string
  count: number
}

export type AdminStorageServiceStats = {
  used_bytes: number
  allocated_bytes: number
  tracked?: boolean
}

export type AdminStatsResponse = {
  users: {
    total: number
    active: number
    disabled: number
    invited: number
  }
  services: {
    mail_accounts_total: number
    messages_total: number
    audit_events_24h: number
  }
  quotas: {
    users_near_mail_quota_90pct: number
  }
  storage?: {
    mail: AdminStorageServiceStats
    drive: AdminStorageServiceStats
    photos: AdminStorageServiceStats
  }
  audit: {
    top_actors_7d: AdminStatsTopActor[]
  }
}

export type AdminUserStorage = {
  mail_used_bytes: number
  drive_used_bytes: number
}

export type AdminSetQuotaRequest = {
  mail_max_storage_bytes?: number
  drive_max_storage_bytes?: number
  photos_max_storage_bytes?: number
}

export type AdminCreateUserRequest = {
  external_id: string
  email: string
  name?: string
}

export type AdminInviteUserRequest = {
  email: string
  name?: string
}

export type AdminUpdateUserRequest = {
  email?: string
  name?: string
}

export type AdminSetUserRoleRequest = {
  role: AdminUserRole
}

export type AdminPublicShare = {
  id: string
  token: string
  path: string
  item_type: string
  access_mode: string
  share_type: number
  permissions: number
  url: string
  expires_at?: string
  created_at?: string
  owner_nc_user_id: string
  owner_email: string
  owner_display_name?: string
  share_with?: string
  share_with_display_name?: string
  has_password?: boolean
  label?: string
  last_access_at?: string | null
  access_count: number
}

export type AdminPublicSharesListResponse = {
  shares: AdminPublicShare[]
  pagination: AdminPagination
}
