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

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

model Account {
  id                       String  @id @default(cuid())
  userId                   String
  type                     String
  provider                 String
  providerAccountId        String
  refresh_token            String?
  access_token             String?
  expires_at               Int?
  token_type               String?
  scope                    String?
  id_token                 String?
  session_state            String?
  oauth_token_secret       String?
  oauth_token              String?
  refresh_token_expires_in Int?

  user User @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@unique([provider, providerAccountId])
}

model Session {
  id           String   @id @default(cuid())
  sessionToken String   @unique
  userId       String
  expires      DateTime
  user         User     @relation(fields: [userId], references: [id], onDelete: Cascade)
}

model User {
  id                      String                    @id @default(cuid())
  createdAt               DateTime                  @default(now())
  updatedAt               DateTime                  @default(now()) @updatedAt
  lastActivityAt          DateTime                  @default(now())
  name                    String?
  email                   String?                   @unique
  emailVerified           DateTime?
  image                   String?
  accounts                Account[]
  sessions                Session[]
  typebots                Typebot[]
  folders                 DashboardFolder[]
  plan                    Plan                      @default(FREE)
  stripeId                String?                   @unique
  credentials             Credentials[]
  customDomains           CustomDomain[]
  apiToken                String?
  CollaboratorsOnTypebots CollaboratorsOnTypebots[]
  company String?
  onboardingCategories String[]
}

model CustomDomain {
  name      String   @id
  createdAt DateTime @default(now())
  ownerId   String
  owner     User     @relation(fields: [ownerId], references: [id], onDelete: Cascade)
}

model Credentials {
  id        String   @id @default(cuid())
  createdAt DateTime @default(now())
  ownerId   String
  owner     User     @relation(fields: [ownerId], references: [id], onDelete: Cascade)
  data      String // Encrypted data
  name      String
  type      String
  iv        String

  @@unique([name, type, ownerId])
}

enum Plan {
  FREE
  PRO
  LIFETIME
  OFFERED
}

model VerificationToken {
  identifier String
  token      String   @unique
  expires    DateTime

  @@unique([identifier, token])
}

model DashboardFolder {
  id             String            @id @default(cuid())
  createdAt      DateTime          @default(now())
  updatedAt      DateTime          @default(now()) @updatedAt
  name           String
  owner          User              @relation(fields: [ownerId], references: [id], onDelete: Cascade)
  ownerId        String
  parentFolderId String?
  parentFolder   DashboardFolder?  @relation("ParentChild", fields: [parentFolderId], references: [id])
  childrenFolder DashboardFolder[] @relation("ParentChild")
  typebots       Typebot[]

  @@unique([id, ownerId])
}

model Typebot {
  id                 String                    @id @default(cuid())
  createdAt          DateTime                  @default(now())
  updatedAt          DateTime                  @default(now()) @updatedAt
  name               String
  ownerId            String
  owner              User                      @relation(fields: [ownerId], references: [id], onDelete: Cascade)
  publishedTypebotId String?
  publishedTypebot   PublicTypebot?
  results            Result[]
  folderId           String?
  folder             DashboardFolder?          @relation(fields: [folderId], references: [id])
  blocks             Json[]
  variables          Json[]
  edges              Json[]
  theme              Json
  settings           Json
  publicId           String?                   @unique
  customDomain       String?                   @unique
  collaborators      CollaboratorsOnTypebots[]
  invitations        Invitation[]
  webhooks           Webhook[]

  @@unique([id, ownerId])
}

model Invitation {
  createdAt DateTime          @default(now())
  email     String
  typebotId String
  typebot   Typebot           @relation(fields: [typebotId], references: [id], onDelete: Cascade)
  type      CollaborationType

  @@unique([email, typebotId])
}

model CollaboratorsOnTypebots {
  userId    String
  user      User              @relation(fields: [userId], references: [id], onDelete: Cascade)
  typebotId String
  typebot   Typebot           @relation(fields: [typebotId], references: [id], onDelete: Cascade)
  type      CollaborationType

  @@unique([userId, typebotId])
}

enum CollaborationType {
  READ
  WRITE
}

model PublicTypebot {
  id           String   @id @default(cuid())
  createdAt    DateTime @default(now())
  updatedAt    DateTime @default(now()) @updatedAt
  typebotId    String   @unique
  typebot      Typebot  @relation(fields: [typebotId], references: [id], onDelete: Cascade)
  name         String
  blocks       Json[]
  variables    Json[]
  edges        Json[]
  theme        Json
  settings     Json
  publicId     String?  @unique
  customDomain String?  @unique
}

model Result {
  id                 String   @id @default(cuid())
  createdAt          DateTime @default(now())
  updatedAt          DateTime @default(now()) @updatedAt
  typebotId          String
  typebot            Typebot  @relation(fields: [typebotId], references: [id], onDelete: Cascade)
  answers            Answer[]
  prefilledVariables Json[]
  isCompleted        Boolean
  logs               Log[]
}

model Log {
  id          String    @id @default(cuid())
  createdAt   DateTime  @default(now())
  resultId    String
  result      Result    @relation(fields: [resultId], references: [id], onDelete: Cascade)
  status      String
  description String
  details     String?
}

model Answer {
  createdAt  DateTime @default(now())
  resultId   String
  result     Result   @relation(fields: [resultId], references: [id], onDelete: Cascade)
  stepId     String
  blockId    String
  variableId String?
  content    String

  @@unique([resultId, blockId, stepId])
}

model Coupon {
  userPropertiesToUpdate Json
  code                   String    @id @unique
  dateRedeemed           DateTime?
}

model Webhook {
  id          String  @id @default(cuid())
  url         String?
  method      String
  queryParams Json[]
  headers     Json[]
  body        String?
  typebotId   String
  typebot     Typebot @relation(fields: [typebotId], references: [id], onDelete: Cascade)
}
