enum Network {
  BABYLON
  ALEXANDRIA
  ROME
}

type Block @entity {
  "Block number as a string"
  id: ID!
  block: Int!
  timestamp: BigInt!
  network: Network!
}

"Stored information about a registered user"
type Member @entity {
  "MemberId: runtime identifier for a user"
  id: ID!

  "The unique handle chosen by member"
  handle: String @unique @fulltext(query: "membersByHandle")

  "A Url to member's Avatar image"
  avatarUri: String

  "Short text chosen by member to share information about themselves"
  about: String

  "Blocknumber when member was registered"
  registeredAtBlock: Int!

  "Member's controller account id"
  controllerAccount: Bytes!

  "Member's root account id"
  rootAccount: Bytes!

  happenedIn: Block!
}

"""
This type is to keep which entity belongs to which class. This type will be used
by EntityCreated event. When a new schema support added to an Entity we will get the
class name from this table.
We need this because we can't create a database row (Channel, Video etc) without
with empty fields.
"""
type ClassEntity @entity {
  "Runtime entity identifier (EntityId)"
  id: ID!

  "The class id of this entity"
  classId: Int!

  happenedIn: Block!
}

"Keep track of the next entity id"
type NextEntityId @entity {
  "Constant field is set to '1'"
  id: ID!

  nextId: Int!
}

#### High Level Derivative Entities ####

type Language @entity {
  "Runtime entity identifier (EntityId)"
  id: ID!

  name: String!
  code: String!

  happenedIn: Block!
}

type Channel @entity {
  "Runtime entity identifier (EntityId)"
  id: ID!

  # "Owner of the channel" Commenting out this field: 'owner' can be curator_group, lead
  # or a member. We are not handling events related to curator group so we will not set this field
  # owner: Member!

  "The title of the Channel"
  handle: String! @fulltext(query: "search")

  "The description of a Channel"
  description: String!

  "Url for Channel's cover (background) photo. Recommended ratio: 16:9."
  coverPhotoUrl: String

  "Channel's avatar photo."
  avatarPhotoUrl: String

  "Flag signaling whether a channel is public."
  isPublic: Boolean!

  "Flag signaling whether a channel is curated/verified."
  isCurated: Boolean!

  "The primary langauge of the channel's content"
  language: Language

  videos: [Video!] @derivedFrom(field: "channel")

  happenedIn: Block!
}

type Category @entity {
  "Runtime entity identifier (EntityId)"
  id: ID!

  "The name of the category"
  name: String! @unique @fulltext(query: "categoriesByName")

  "The description of the category"
  description: String

  videos: [Video!] @derivedFrom(field: "category")

  happenedIn: Block!
}

"Encoding and containers"
type VideoMediaEncoding @entity {
  "Runtime entity identifier (EntityId)"
  id: ID!

  name: String!

  happenedIn: Block!
}

type KnownLicenseEntity @entity {
  "Runtime entity identifier (EntityId)"
  id: ID!

  "Short, commonly recognized code of the licence (ie. CC_BY_SA)"
  code: String! @unique

  "Full, descriptive name of the license (ie. Creative Commons - Attribution-NonCommercial-NoDerivs)"
  name: String

  "Short description of the license conditions"
  description: String

  "An url pointing to full license content"
  url: String

  happenedIn: Block!
}

type UserDefinedLicenseEntity @entity {
  "Runtime entity identifier (EntityId)"
  id: ID!

  "Custom license content"
  content: String!

  happenedIn: Block!
}

type MediaLocationEntity @entity {
  "Runtime entity identifier (EntityId)"
  id: ID!

  # One of the following field will be non-null

  "A reference to HttpMediaLocation"
  httpMediaLocation: HttpMediaLocationEntity

  "A reference to JoystreamMediaLocation"
  joystreamMediaLocation: JoystreamMediaLocationEntity

  videoMedia: VideoMedia @derivedFrom(field: "locationEntity")

  happenedIn: Block!
}

type JoystreamMediaLocationEntity @entity {
  "Runtime entity identifier (EntityId)"
  id: ID!

  "Id of the data object in the Joystream runtime dataDirectory module"
  dataObjectId: String!

  happenedIn: Block!
}

type HttpMediaLocationEntity @entity {
  "Runtime entity identifier (EntityId)"
  id: ID!

  "The http url pointing to the media"
  url: String!

  "The port to use when connecting to the http url (defaults to 80)"
  port: Int

  happenedIn: Block!
}

type VideoMedia @entity {
  "Runtime entity identifier (EntityId)"
  id: ID!

  "Encoding of the video media object"
  encoding: VideoMediaEncoding!

  "Video media width in pixels"
  pixelWidth: Int!

  "Video media height in pixels"
  pixelHeight: Int!

  "Video media size in bytes"
  size: Int

  video: Video @derivedFrom(field: "media")

  "Location of the video media object"
  location: MediaLocation!

  locationEntity: MediaLocationEntity

  happenedIn: Block!
}

type Video @entity {
  "Runtime entity identifier (EntityId)"
  id: ID!

  "Reference to member's channel"
  channel: Channel!

  "Reference to a video category"
  category: Category!

  "The title of the video"
  title: String! @fulltext(query: "search")

  "The description of the Video"
  description: String!

  "Video duration in seconds"
  duration: Int!

  "Video's skippable intro duration in seconds"
  skippableIntroDuration: Int

  "Video thumbnail url (recommended ratio: 16:9)"
  thumbnailUrl: String!

  "Video's main langauge"
  language: Language

  "Reference to VideoMedia"
  media: VideoMedia

  "Whether or not Video contains marketing"
  hasMarketing: Boolean

  "If the Video was published on other platform before beeing published on Joystream - the original publication date"
  publishedBeforeJoystream: Int

  "Whether the Video is supposed to be publically displayed"
  isPublic: Boolean!

  "Video curation status set by the Curator"
  isCurated: Boolean!

  "Whether the Video contains explicit material."
  isExplicit: Boolean!

  license: LicenseEntity!

  happenedIn: Block!

  "Is video featured or not"
  isFeatured: Boolean!

  featured: FeaturedVideo @derivedFrom(field: "video")
}

type JoystreamMediaLocation @variant {
  "Id of the data object in the Joystream runtime dataDirectory module"
  dataObjectId: String!
}

type HttpMediaLocation @variant {
  "The http url pointing to the media"
  url: String!

  "The port to use when connecting to the http url (defaults to 80)"
  port: Int
}

union MediaLocation = HttpMediaLocation | JoystreamMediaLocation

type KnownLicense @variant {
  "Short, commonly recognized code of the licence (ie. CC_BY_SA)"
  code: String!

  "Full, descriptive name of the license (ie. Creative Commons - Attribution-NonCommercial-NoDerivs)"
  name: String

  "Short description of the license conditions"
  description: String

  "An url pointing to full license content"
  url: String
}

type UserDefinedLicense @variant {
  "Custom license content"
  content: String!
}

union License = KnownLicense | UserDefinedLicense

type LicenseEntity @entity {
  "Runtime entity identifier (EntityId)"
  id: ID!

  type: License!

  "Attribution (if required by the license)"
  attribution: String

  happenedIn: Block!
}

type FeaturedVideo @entity {
  "Runtime entity identifier (EntityId)"
  id: ID!

  "Reference to a video"
  video: Video!
}
