generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model User {
  id                   Int                    @id @default(autoincrement())
  discordId            String                 @unique
  discordName          String
  discordDiscriminator String
  discordAvatar        String?
  discordRefreshToken  String
  twitch               String?
  twitter              String?
  youtubeId            String?
  youtubeName          String?
  createdAt            DateTime               @default(now())
  updatedAt            DateTime               @updatedAt
  trustedUsers         TrustRelationships[]   @relation("trustGiver")
  trustingUsers        TrustRelationships[]   @relation("trustReceiver")
  ownedOrganization    Organization?
  tournamentTeams      TournamentTeamMember[]
}

model Organization {
  id            Int          @id @default(autoincrement())
  name          String
  /// Name in lower case to show in URL
  nameForUrl    String       @unique
  ownerId       Int          @unique
  owner         User         @relation(fields: [ownerId], references: [id])
  discordInvite String
  twitter       String?
  tournaments   Tournament[]
}

model Tournament {
  id                Int              @id @default(autoincrement())
  name              String
  /// Name in lower case to show in URL
  nameForUrl        String
  description       String?
  startTime         DateTime
  checkInTime       DateTime
  /// CSS for tournament banner's background value
  bannerBackground  String
  /// CSS for tournament banner's color value
  bannerTextHSLArgs String
  mapPool           Stage[]
  organizerId       Int
  organizer         Organization     @relation(fields: [organizerId], references: [id])
  teams             TournamentTeam[]

  // There might be duplicate nameForUrl's but inside an organization they're unique
  @@unique([nameForUrl, organizerId])
}

enum Mode {
  TW
  SZ
  TC
  RM
  CB
}

model Stage {
  id                 Int          @id @default(autoincrement())
  name               String
  mode               Mode
  tournamentMapPools Tournament[]
}

model TournamentTeam {
  id           Int                    @id @default(autoincrement())
  name         String
  checkedIn    Boolean                @default(false)
  tournamentId Int
  tournament   Tournament             @relation(fields: [tournamentId], references: [id])
  inviteCode   String                 @default(uuid())
  members      TournamentTeamMember[]

  @@unique([name, tournamentId])
}

model TournamentTeamMember {
  memberId     Int
  teamId       Int
  tournamentId Int
  captain      Boolean        @default(false)
  team         TournamentTeam @relation(fields: [teamId], references: [id])
  member       User           @relation(fields: [memberId], references: [id])

  @@unique([memberId, tournamentId])
}

// Trust relationship is defined as user allowing another user
// to add them to a tournament team
// / LFG group without them having to click a link.
model TrustRelationships {
  trustGiverId    Int
  trustReceiverId Int
  trustGiver      User     @relation("trustGiver", fields: [trustGiverId], references: [id])
  trustReceiver   User     @relation("trustReceiver", fields: [trustReceiverId], references: [id])
  createdAt       DateTime @default(now())

  @@unique([trustGiverId, trustReceiverId])
}
