// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

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

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

model Organization {
  id           String             @id @default(uuid())
  name         String
  description  String?
  users        UserOrganization[]
  media        Media[]
  paymentId    String?
  createdAt    DateTime           @default(now())
  updatedAt    DateTime           @updatedAt
  github       GitHub[]
  subscription Subscription?
  Integration  Integration[]
  post         Post[] @relation("organization")
  submittedPost Post[] @relation("submittedForOrg")
  Comments     Comments[]
  notifications Notifications[]
  buyerOrganization MessagesGroup[]
  usedCodes    UsedCodes[]
  credits      Credits[]
}

model User {
  id            String             @id @default(uuid())
  email         String
  password      String?
  providerName  Provider
  name          String?
  lastName      String?
  isSuperAdmin  Boolean            @default(false)
  bio           String?
  audience      Int                @default(0)
  pictureId     String?
  picture       Media?             @relation(fields: [pictureId], references: [id])
  providerId    String?
  organizations UserOrganization[]
  timezone      Int
  comments      Comments[]
  createdAt     DateTime           @default(now())
  updatedAt     DateTime           @updatedAt
  lastReadNotifications DateTime   @default(now())
  inviteId      String?
  activated     Boolean            @default(true)
  items         ItemUser[]
  marketplace   Boolean            @default(true)
  account       String?
  connectedAccount  Boolean         @default(false)
  groupBuyer   MessagesGroup[]  @relation("groupBuyer")
  groupSeller  MessagesGroup[]  @relation("groupSeller")
  orderBuyer   Orders[]        @relation("orderBuyer")
  orderSeller  Orders[]        @relation("orderSeller")
  payoutProblems    PayoutProblems[]
  lastOnline   DateTime          @default(now())
  agencies     SocialMediaAgency[]

  @@unique([email, providerName])
  @@index([lastReadNotifications])
  @@index([inviteId])
  @@index([account])
  @@index([lastOnline])
  @@index([pictureId])
}

model UsedCodes {
  id        String   @id @default(uuid())
  code      String
  orgId     String
  organization Organization @relation(fields: [orgId], references: [id])
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  @@index([code])
}

model UserOrganization {
  id             String       @id @default(uuid())
  user           User         @relation(fields: [userId], references: [id])
  userId         String
  organization   Organization @relation(fields: [organizationId], references: [id])
  organizationId String
  disabled       Boolean      @default(false)
  role           Role         @default(USER)
  createdAt      DateTime     @default(now())
  updatedAt      DateTime     @updatedAt

  @@unique([userId, organizationId])
  @@index([disabled])
}

model GitHub {
  id             String       @id @default(uuid())
  login          String?
  name           String?
  token          String
  jobId          String?
  organization   Organization @relation(fields: [organizationId], references: [id])
  organizationId String
  createdAt      DateTime     @default(now())
  updatedAt      DateTime     @updatedAt

  @@index([login])
  @@index([organizationId])
}

model Trending {
  id           String   @id @default(uuid())
  trendingList String
  language     String?
  hash         String
  date         DateTime
  createdAt    DateTime @default(now())
  updatedAt    DateTime @updatedAt

  @@unique([language])
  @@index([hash])
}

model TrendingLog {
  id           String   @id @default(uuid())
  language     String?
  date         DateTime
}

model ItemUser {
  id           String   @id @default(uuid())
  user         User     @relation(fields: [userId], references: [id])
  userId       String
  key          String

  @@index([userId])
  @@index([key])
  @@unique([userId, key])
}

model Star {
  id         String   @id @default(uuid())
  stars      Int
  totalStars Int
  forks      Int
  totalForks Int
  login      String
  date       DateTime @default(now()) @db.Date
  createdAt  DateTime @default(now())
  updatedAt  DateTime @updatedAt

  @@unique([login, date])
}

model Media {
  id             String       @id @default(uuid())
  name           String
  path           String
  organization   Organization @relation(fields: [organizationId], references: [id])
  organizationId String
  createdAt      DateTime     @default(now())
  updatedAt      DateTime     @updatedAt
  userPicture    User[]
  agencies       SocialMediaAgency[]

  @@index([organizationId])
}

model SocialMediaAgency {
  id                  String   @id @default(uuid())
  user                User @relation(fields: [userId], references: [id])
  userId              String @unique()
  name                String
  logoId              String?
  logo                Media? @relation(fields: [logoId], references: [id])
  website             String?
  slug                String?

  facebook            String?
  instagram           String?
  twitter             String?
  linkedIn            String?
  youtube             String?
  tiktok              String?
  otherSocialMedia    String?

  shortDescription    String
  description         String
  niches              SocialMediaAgencyNiche[]
  approved            Boolean @default(false)

  createdAt           DateTime  @default(now())
  updatedAt           DateTime  @updatedAt
  deletedAt           DateTime?

  @@index([userId])
  @@index([deletedAt])
  @@index([id])
}

model SocialMediaAgencyNiche {
  agencyId            String
  agency              SocialMediaAgency @relation(fields: [agencyId], references: [id])
  niche               String

  @@id([agencyId, niche])
}

model Credits {
  id             String   @id @default(uuid())
  organization   Organization @relation(fields: [organizationId], references: [id])
  organizationId String
  credits        Int
  createdAt      DateTime @default(now())
  updatedAt      DateTime @updatedAt

  @@index([organizationId])
  @@index([createdAt])
}

model Subscription {
  id                String            @id @default(cuid())
  organizationId    String            @unique
  organization      Organization      @relation(fields: [organizationId], references: [id])
  subscriptionTier  SubscriptionTier
  identifier        String?
  cancelAt          DateTime?
  period            Period
  totalChannels     Int
  isLifetime        Boolean           @default(false)
  createdAt         DateTime          @default(now())
  updatedAt         DateTime          @updatedAt
  deletedAt         DateTime?

  @@index([organizationId])
  @@index([deletedAt])
}

model Integration {
  id                    String          @id @default(cuid())
  internalId            String
  organizationId        String
  name                  String
  organization          Organization    @relation(fields: [organizationId], references: [id])
  picture               String?
  providerIdentifier    String
  type                  String
  token                 String
  disabled              Boolean         @default(false)
  tokenExpiration       DateTime?
  refreshToken          String?
  posts                 Post[]
  profile               String?
  deletedAt             DateTime?
  createdAt             DateTime       @default(now())
  updatedAt             DateTime?      @updatedAt
  orderItems            OrderItems[]
  inBetweenSteps        Boolean         @default(false)
  refreshNeeded         Boolean         @default(false)
  postingTimes          String          @default("[{\"time\":120}, {\"time\":400}, {\"time\":700}]")
  customInstanceDetails String?

  @@index([updatedAt])
  @@index([deletedAt])
  @@unique([organizationId, internalId])
}

model Comments {
  id                String     @id @default(uuid())
  content           String
  organizationId    String
  organization      Organization @relation(fields: [organizationId], references: [id])
  userId            String
  user              User       @relation(fields: [userId], references: [id])
  date              DateTime
  parentCommentId   String?
  parentComment     Comments?  @relation("parentCommentId", fields: [parentCommentId], references: [id])
  childrenComment   Comments[] @relation("parentCommentId")
  createdAt         DateTime   @default(now())
  updatedAt         DateTime   @updatedAt
  deletedAt         DateTime?

  @@index([createdAt])
  @@index([organizationId])
  @@index([date])
  @@index([userId])
  @@index([deletedAt])
}

model Post {
  id                  String       @id @default(cuid())
  state               State        @default(QUEUE)
  publishDate         DateTime
  organizationId      String
  integrationId       String
  content             String
  group               String
  organization        Organization @relation("organization", fields: [organizationId], references: [id])
  integration         Integration  @relation(fields: [integrationId], references: [id])
  title               String?
  description         String?
  parentPostId        String?
  releaseId           String?
  releaseURL          String?
  settings            String?
  parentPost          Post?       @relation("parentPostId", fields: [parentPostId], references: [id])
  childrenPost        Post[]      @relation("parentPostId")
  image               String?
  submittedForOrderId String?
  submittedForOrder   Orders? @relation(fields: [submittedForOrderId], references: [id])
  submittedForOrganizationId String?
  submittedForOrganization   Organization? @relation("submittedForOrg", fields: [submittedForOrganizationId], references: [id])
  approvedSubmitForOrder APPROVED_SUBMIT_FOR_ORDER @default(NO)
  lastMessageId       String?
  lastMessage         Messages? @relation(fields: [lastMessageId], references: [id])
  payoutProblems      PayoutProblems[]
  createdAt           DateTime     @default(now())
  updatedAt           DateTime     @updatedAt
  deletedAt           DateTime?

  @@index([group])
  @@index([deletedAt])
  @@index([publishDate])
  @@index([state])
  @@index([organizationId])
  @@index([parentPostId])
  @@index([submittedForOrderId])
  @@index([approvedSubmitForOrder])
  @@index([lastMessageId])
  @@index([createdAt])
  @@index([updatedAt])
  @@index([releaseURL])
  @@index([integrationId])
}

model Notifications {
  id                String   @id @default(uuid())
  organizationId    String
  organization      Organization @relation(fields: [organizationId], references: [id])
  content           String
  link              String?
  createdAt         DateTime @default(now())
  updatedAt         DateTime @updatedAt
  deletedAt         DateTime?

  @@index([createdAt])
  @@index([organizationId])
  @@index([deletedAt])
}

model MessagesGroup {
  id                  String   @id @default(uuid())
  buyerOrganizationId String
  buyerOrganization   Organization @relation(fields: [buyerOrganizationId], references: [id])
  buyerId             String
  buyer               User     @relation("groupBuyer", fields: [buyerId], references: [id])
  sellerId            String
  seller              User     @relation("groupSeller", fields: [sellerId], references: [id])
  messages            Messages[]
  orders              Orders[]
  createdAt           DateTime @default(now())
  updatedAt           DateTime @updatedAt

  @@unique([buyerId, sellerId])
  @@index([createdAt])
  @@index([updatedAt])
  @@index([buyerOrganizationId])
}

model PayoutProblems {
  id                String   @id @default(uuid())
  status            String
  orderId           String
  order             Orders   @relation(fields: [orderId], references: [id])
  userId            String
  user              User     @relation(fields: [userId], references: [id])
  postId            String?
  post              Post?     @relation(fields: [postId], references: [id])
  amount            Int
  createdAt         DateTime @default(now())
  updatedAt         DateTime @updatedAt
}

model Orders {
  id                String   @id @default(uuid())
  buyerId           String
  sellerId          String
  posts             Post[]
  buyer             User     @relation("orderBuyer", fields: [buyerId], references: [id])
  seller            User     @relation("orderSeller", fields: [sellerId], references: [id])
  status            OrderStatus
  ordersItems       OrderItems[]
  messageGroupId    String
  messageGroup      MessagesGroup @relation(fields: [messageGroupId], references: [id])
  captureId         String?
  payoutProblems    PayoutProblems[]
  createdAt         DateTime @default(now())
  updatedAt         DateTime @updatedAt

  @@index([buyerId])
  @@index([sellerId])
  @@index([updatedAt])
  @@index([createdAt])
  @@index([messageGroupId])
}

model OrderItems {
  id                String   @id @default(uuid())
  orderId           String
  order             Orders   @relation(fields: [orderId], references: [id])
  integrationId     String
  integration       Integration @relation(fields: [integrationId], references: [id])
  quantity          Int
  price             Int

  @@index([orderId])
  @@index([integrationId])
}

model Messages {
  id                String   @id @default(uuid())
  from              From
  content           String?
  groupId           String
  group             MessagesGroup @relation(fields: [groupId], references: [id])
  special           String?
  posts             Post[]
  createdAt         DateTime @default(now())
  updatedAt         DateTime @updatedAt
  deletedAt         DateTime?

  @@index([groupId])
  @@index([createdAt])
  @@index([deletedAt])
}

enum OrderStatus {
  PENDING
  ACCEPTED
  CANCELED
  COMPLETED
}

enum From {
  BUYER
  SELLER
}

enum State {
  QUEUE
  PUBLISHED
  ERROR
  DRAFT
}

enum SubscriptionTier {
  STANDARD
  PRO
  TEAM
  ULTIMATE
}

enum Period {
  MONTHLY
  YEARLY
}

enum Provider {
  LOCAL
  GITHUB
  GOOGLE
}

enum Role {
  SUPERADMIN
  ADMIN
  USER
}

enum APPROVED_SUBMIT_FOR_ORDER {
  NO
  WAITING_CONFIRMATION
  YES
}