# ===============================================
# USERS
# ===============================================

extend type Query {
  users: UserQuery
}

extend type Mutation {
  users: UserMutation
}

# -----------------------------------------------
# QUERIES
# -----------------------------------------------

type UserQuery {
  list(
    filter: String
    orderBy: String
  ): [UserMinimal] @auth(requires: ["write:users", "manage:users", "manage:system"])

  search(
    query: String!
  ): [UserMinimal] @auth(requires: ["write:groups", "manage:groups", "write:users", "manage:users", "manage:system"])

  single(
    id: Int!
  ): User @auth(requires: ["manage:users", "manage:system"])

  profile: UserProfile

  lastLogins: [UserLastLogin] @auth(requires: ["write:groups", "manage:groups", "write:users", "manage:users", "manage:system"])
}

# -----------------------------------------------
# MUTATIONS
# -----------------------------------------------

type UserMutation {
  create(
    email: String!
    name: String!
    passwordRaw: String
    providerKey: String!
    groups: [Int]!
    mustChangePassword: Boolean
    sendWelcomeEmail: Boolean
  ): UserResponse @auth(requires: ["write:users", "manage:users", "manage:system"])

  update(
    id: Int!
    email: String
    name: String
    newPassword: String
    groups: [Int]
    location: String
    jobTitle: String
    timezone: String
    dateFormat: String
    appearance: String
  ): DefaultResponse @auth(requires: ["manage:users", "manage:system"])

  delete(
    id: Int!
    replaceId: Int!
  ): DefaultResponse @auth(requires: ["manage:users", "manage:system"])

  verify(
    id: Int!
  ): DefaultResponse @auth(requires: ["manage:users", "manage:system"])

  activate(
    id: Int!
  ): DefaultResponse @auth(requires: ["manage:users", "manage:system"])

  deactivate(
    id: Int!
  ): DefaultResponse @auth(requires: ["manage:users", "manage:system"])

  resetPassword(
    id: Int!
  ): DefaultResponse

  updateProfile(
    name: String!
    location: String!
    jobTitle: String!
    timezone: String!
    dateFormat: String!
    appearance: String!
  ): UserTokenResponse

  changePassword(
    current: String!
    new: String!
  ): UserTokenResponse
}

# -----------------------------------------------
# TYPES
# -----------------------------------------------

type UserResponse {
  responseResult: ResponseStatus!
  user: User
}

type UserLastLogin {
  id: Int!
  name: String!
  lastLoginAt: Date!
}

type UserMinimal {
  id: Int!
  name: String!
  email: String!
  providerKey: String!
  isSystem: Boolean!
  isActive: Boolean!
  createdAt: Date!
  lastLoginAt: Date
}

type User {
  id: Int!
  name: String!
  email: String!
  providerKey: String!
  providerId: String
  isSystem: Boolean!
  isActive: Boolean!
  isVerified: Boolean!
  location: String!
  jobTitle: String!
  timezone: String!
  dateFormat: String!
  appearance: String!
  createdAt: Date!
  updatedAt: Date!
  lastLoginAt: Date
  groups: [Group]!
}

type UserProfile {
  id: Int!
  name: String!
  email: String!
  providerKey: String
  providerName: String
  isSystem: Boolean!
  isVerified: Boolean!
  location: String!
  jobTitle: String!
  timezone: String!
  dateFormat: String!
  appearance: String!
  createdAt: Date!
  updatedAt: Date!
  lastLoginAt: Date
  groups: [String]!
  pagesTotal: Int!
}

type UserTokenResponse {
  responseResult: ResponseStatus!
  jwt: String
}
