// 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")
}

enum ProjectType {
  website
  app
  backend
}

model Project {
  id               String        @id @default(dbgenerated("gen_random_uuid()"))
  name             String
  organizationSlug String
  eventsCount      Int           @default(0)
  types            ProjectType[] @default([])

  events     Event[]
  profiles   Profile[]
  clients    Client[]
  reports    Report[]
  dashboards Dashboard[]
  share      ShareOverview?
  meta       EventMeta[]
  references Reference[]
  access     ProjectAccess[]

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

  @@map("projects")
}

enum AccessLevel {
  read
  write
  admin
}

model ProjectAccess {
  id               String      @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  projectId        String
  project          Project     @relation(fields: [projectId], references: [id])
  organizationSlug String
  userId           String
  level            AccessLevel
  createdAt        DateTime    @default(now())
  updatedAt        DateTime    @default(now()) @updatedAt

  @@map("project_access")
}

model Event {
  id         String  @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  name       String
  properties Json
  projectId  String
  project    Project @relation(fields: [projectId], references: [id])

  profileId String?

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

  @@map("events")
}

model Salt {
  salt      String   @id
  createdAt DateTime @default(now())
  updatedAt DateTime @default(now()) @updatedAt

  @@map("salts")
}

model Profile {
  id         String   @id
  externalId String?
  firstName  String?
  lastName   String?
  email      String?
  avatar     String?
  properties Json
  projectId  String
  project    Project  @relation(fields: [projectId], references: [id])
  createdAt  DateTime @default(now())
  updatedAt  DateTime @default(now()) @updatedAt

  @@map("profiles")
}

enum ClientType {
  read
  write
  root
}

model Client {
  id               String     @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  name             String
  secret           String?
  type             ClientType @default(write)
  projectId        String?
  project          Project?   @relation(fields: [projectId], references: [id])
  organizationSlug String
  cors             String?

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

  @@map("clients")
}

enum Interval {
  hour
  day
  month
  minute
}

enum ChartType {
  linear
  bar
  histogram
  pie
  metric
  area
  map
  funnel
}

model Dashboard {
  id               String   @id @default(dbgenerated("gen_random_uuid()"))
  name             String
  organizationSlug String
  projectId        String
  project          Project  @relation(fields: [projectId], references: [id])
  reports          Report[]

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

  @@map("dashboards")
}

enum Metric {
  sum
  average
  min
  max
}

model Report {
  id         String    @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  name       String
  interval   Interval
  range      String    @default("1m")
  chartType  ChartType
  lineType   String    @default("monotone")
  breakdowns Json
  events     Json
  formula    String?
  unit       String?
  metric     Metric    @default(sum)
  projectId  String
  project    Project   @relation(fields: [projectId], references: [id])
  previous   Boolean   @default(false)

  dashboardId String
  dashboard   Dashboard @relation(fields: [dashboardId], references: [id])

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

  @@map("reports")
}

model Waitlist {
  id        String   @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  email     String   @unique
  createdAt DateTime @default(now())
  updatedAt DateTime @default(now()) @updatedAt
  accepted  Boolean  @default(false)

  @@map("waitlist")
}

model ShareOverview {
  id               String   @unique
  projectId        String   @unique
  project          Project  @relation(fields: [projectId], references: [id])
  organizationSlug String
  public           Boolean  @default(false)
  password         String?
  createdAt        DateTime @default(now())
  updatedAt        DateTime @default(now()) @updatedAt

  @@map("shares")
}

model EventMeta {
  id         String   @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  name       String
  conversion Boolean?
  color      String?
  icon       String?
  projectId  String
  project    Project  @relation(fields: [projectId], references: [id])

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

  @@unique([name, projectId])
  @@map("event_meta")
}

model Reference {
  id          String   @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  title       String
  description String?
  date        DateTime @default(now())
  projectId   String
  project     Project  @relation(fields: [projectId], references: [id])

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

  @@map("references")
}
