# ===============================================
# PAGES
# ===============================================

extend type Query {
  pages: PageQuery
}

extend type Mutation {
  pages: PageMutation
}

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

type PageQuery {
  history(
    id: Int!
    offsetPage: Int
    offsetSize: Int
  ): PageHistoryResult @auth(requires: ["manage:system", "read:history"])

  version(
    pageId: Int!
    versionId: Int!
  ): PageVersion @auth(requires: ["manage:system", "read:history"])

  search(
    query: String!
    path: String
    locale: String
  ): PageSearchResponse! @auth(requires: ["manage:system", "read:pages"])

  list(
    limit: Int
    orderBy: PageOrderBy
    orderByDirection: PageOrderByDirection
    tags: [String!]
    locale: String
    creatorId: Int
    authorId: Int
  ): [PageListItem!]! @auth(requires: ["manage:system", "read:pages"])

  single(
    id: Int!
  ): Page @auth(requires: ["read:pages", "manage:system"])

  tags: [PageTag]! @auth(requires: ["manage:system", "read:pages"])

  searchTags(
    query: String!
  ): [String]! @auth(requires: ["manage:system", "read:pages"])

  tree(
    path: String
    parent: Int
    mode: PageTreeMode!
    locale: String!
    includeAncestors: Boolean
  ): [PageTreeItem] @auth(requires: ["manage:system", "read:pages"])

  links(
    locale: String!
  ): [PageLinkItem] @auth(requires: ["manage:system", "read:pages"])

  checkConflicts(
    id: Int!
    checkoutDate: Date!
  ): Boolean! @auth(requires: ["write:pages", "manage:pages", "manage:system"])

  conflictLatest(
    id: Int!
  ): PageConflictLatest! @auth(requires: ["write:pages", "manage:pages", "manage:system"])
}

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

type PageMutation {
  create(
    content: String!
    description: String!
    editor: String!
    isPublished: Boolean!
    isPrivate: Boolean!
    locale: String!
    path: String!
    publishEndDate: Date
    publishStartDate: Date
    scriptCss: String
    scriptJs: String
    tags: [String]!
    title: String!
  ): PageResponse @auth(requires: ["write:pages", "manage:pages", "manage:system"])

  update(
    id: Int!
    content: String
    description: String
    editor: String
    isPrivate: Boolean
    isPublished: Boolean
    locale: String
    path: String
    publishEndDate: Date
    publishStartDate: Date
    scriptCss: String
    scriptJs: String
    tags: [String]
    title: String
  ): PageResponse @auth(requires: ["write:pages", "manage:pages", "manage:system"])

  move(
    id: Int!
    destinationPath: String!
    destinationLocale: String!
  ): DefaultResponse @auth(requires: ["manage:pages", "manage:system"])

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

  deleteTag(
    id: Int!
  ): DefaultResponse @auth(requires: ["manage:system"])

  updateTag(
    id: Int!
    tag: String!
    title: String!
  ): DefaultResponse @auth(requires: ["manage:system"])

  flushCache: DefaultResponse @auth(requires: ["manage:system"])

  migrateToLocale(
    sourceLocale: String!
    targetLocale: String!
  ): PageMigrationResponse @auth(requires: ["manage:system"])

  rebuildTree: DefaultResponse @auth(requires: ["manage:system"])

  render(
    id: Int!
  ): DefaultResponse @auth(requires: ["manage:system"])

  restore(
    pageId: Int!
    versionId: Int!
  ): DefaultResponse @auth(requires: ["write:pages", "manage:pages", "manage:system"])

  purgeHistory (
    olderThan: String!
  ): DefaultResponse @auth(requires: ["manage:system"])
}

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

type PageResponse {
  responseResult: ResponseStatus!
  page: Page
}

type PageMigrationResponse {
  responseResult: ResponseStatus!
  count: Int
}

type Page {
  id: Int!
  path: String!
  hash: String!
  title: String!
  description: String!
  isPrivate: Boolean! @auth(requires: ["write:pages", "manage:system"])
  isPublished: Boolean! @auth(requires: ["write:pages", "manage:system"])
  privateNS: String @auth(requires: ["write:pages", "manage:system"])
  publishStartDate: Date! @auth(requires: ["write:pages", "manage:system"])
  publishEndDate: Date! @auth(requires: ["write:pages", "manage:system"])
  tags: [PageTag]!
  content: String! @auth(requires: ["read:source", "write:pages", "manage:system"])
  render: String
  toc: String
  contentType: String!
  createdAt: Date!
  updatedAt: Date!
  editor: String! @auth(requires: ["write:pages", "manage:system"])
  locale: String!
  scriptCss: String
  scriptJs: String
  authorId: Int! @auth(requires: ["write:pages", "manage:system"])
  authorName: String! @auth(requires: ["write:pages", "manage:system"])
  authorEmail: String! @auth(requires: ["write:pages", "manage:system"])
  creatorId: Int! @auth(requires: ["write:pages", "manage:system"])
  creatorName: String! @auth(requires: ["write:pages", "manage:system"])
  creatorEmail: String! @auth(requires: ["write:pages", "manage:system"])
}

type PageTag {
  id: Int!
  tag: String!
  title: String
  createdAt: Date!
  updatedAt: Date!
}

type PageHistory {
  versionId: Int!
  versionDate: Date!
  authorId: Int!
  authorName: String!
  actionType: String!
  valueBefore: String
  valueAfter: String
}

type PageVersion {
  action: String!
  authorId: String!
  authorName: String!
  content: String!
  contentType: String!
  createdAt: Date!
  versionDate: Date!
  description: String!
  editor: String!
  isPrivate: Boolean!
  isPublished: Boolean!
  locale: String!
  pageId: Int!
  path: String!
  publishEndDate: Date!
  publishStartDate: Date!
  tags: [String]!
  title: String!
  versionId: Int!
}

type PageHistoryResult {
  trail: [PageHistory]
  total: Int!
}

type PageSearchResponse {
  results: [PageSearchResult]!
  suggestions: [String]!
  totalHits: Int!
}

type PageSearchResult {
  id: String!
  title: String!
  description: String!
  path: String!
  locale: String!
}

type PageListItem {
  id: Int!
  path: String!
  locale: String!
  title: String
  description: String
  contentType: String!
  isPublished: Boolean!
  isPrivate: Boolean!
  privateNS: String
  createdAt: Date!
  updatedAt: Date!
  tags: [String]
}

type PageTreeItem {
  id: Int!
  path: String!
  depth: Int!
  title: String!
  isPrivate: Boolean!
  isFolder: Boolean!
  privateNS: String
  parent: Int
  pageId: Int
  locale: String!
}

type PageLinkItem {
  id: Int!
  path: String!
  title: String!
  links: [String]!
}

type PageConflictLatest {
  id: Int!
  authorId: String!
  authorName: String!
  content: String!
  createdAt: Date!
  description: String!
  isPublished: Boolean!
  locale: String!
  path: String!
  tags: [String]
  title: String!
  updatedAt: Date!
}

enum PageOrderBy {
  CREATED
  ID
  PATH
  TITLE
  UPDATED
}

enum PageOrderByDirection {
  ASC
  DESC
}

enum PageTreeMode {
  FOLDERS
  PAGES
  ALL
}
