
generator client {
  provider = "prisma-client"
  output   = "../src/generated/prisma"
}

datasource db {
  provider = "postgresql"
}

model User {
  id                          String            @id @default(uuid())
  name                        String
  email                       String
  emailVerified               Boolean           @default(false)
  image                       String?
  createdAt                   DateTime          @default(now()) @db.Timestamptz
  updatedAt                   DateTime          @updatedAt @db.Timestamptz
  sessions                    Session[]
  accounts                    Account[]
  audit                       Audit[]
  notification                Notification[]

  role                        String?
  banned                      Boolean?          @default(false)
  banReason                   String?
  banExpires                  DateTime?

  members                     Member[]
  invitations                 Invitation[]
  boxes                       Box[]
  items                        Item[]

  @@unique([email])
  @@map("user")
}

model Session {
  id                          String            @id @default(uuid())
  expiresAt                   DateTime          @db.Timestamptz
  token                       String
  createdAt                   DateTime          @default(now()) @db.Timestamptz
  updatedAt                   DateTime          @updatedAt @db.Timestamptz
  ipAddress                   String?
  userAgent                   String?
  userId                      String
  user                        User              @relation(fields: [userId], references: [id], onDelete: Cascade)

  impersonatedBy              String?

  activeOrganizationId        String?

  @@unique([token])
  @@index([userId])
  @@map("session")
}

model Account {
  id                          String            @id @default(uuid())
  accountId                   String
  providerId                  String
  userId                      String
  user                        User              @relation(fields: [userId], references: [id], onDelete: Cascade)
  accessToken                 String?
  refreshToken                String?
  idToken                     String?
  accessTokenExpiresAt        DateTime?
  refreshTokenExpiresAt       DateTime?
  scope                       String?
  password                    String?
  createdAt                   DateTime          @default(now()) @db.Timestamptz
  updatedAt                   DateTime          @updatedAt @db.Timestamptz

  @@index([userId])
  @@map("account")
}

model Verification {
  id                          String            @id @default(uuid())
  identifier                  String
  value                       String
  expiresAt                   DateTime
  createdAt                   DateTime          @default(now()) @db.Timestamptz
  updatedAt                   DateTime          @updatedAt @db.Timestamptz

  @@index([identifier])
  @@map("verification")
}

model House {
  id                          String            @id @default(uuid())
  name                        String
  slug                        String
  logo                        String?
  createdAt                   DateTime          @db.Timestamptz
  metadata                    String?
  color                       String?           @default("#000000")
  members                     Member[]
  invitations                 Invitation[]
  boxes                       Box[]

  @@unique([slug])
  @@map("house")
}

model Member {
  id                          String            @id @default(uuid())
  organizationId              String
  house                       House             @relation(fields: [organizationId], references: [id], onDelete: Cascade)
  userId                      String
  user                        User              @relation(fields: [userId], references: [id], onDelete: Cascade)
  role                        String            @default("member")
  createdAt                   DateTime          @db.Timestamptz

  @@index([organizationId])
  @@index([userId])
  @@map("member")
}

model Invitation {
  id                          String            @id @default(uuid())
  organizationId              String
  house                       House             @relation(fields: [organizationId], references: [id], onDelete: Cascade)
  email                       String
  role                        String?
  status                      String            @default("pending")
  expiresAt                   DateTime          @db.Timestamptz
  createdAt                   DateTime          @default(now()) @db.Timestamptz
  inviterId                   String
  user                        User              @relation(fields: [inviterId], references: [id], onDelete: Cascade)

  @@index([organizationId])
  @@index([email])
  @@map("invitation")
}

model Setting {
  id                          String            @id @default(uuid())
  key                         String            @unique
  value                       String
  description                 String
  relation                    String            @default("admin")

  createdAt                   DateTime          @default(now()) @db.Timestamptz
  updatedAt                   DateTime          @updatedAt @db.Timestamptz

  @@map("setting")
}

model Audit {
  id                          String            @id @default(uuid())
  userId                      String
  action                      String
  tableName                   String
  recordId                    String
  oldValue                    String?
  newValue                    String?
  createdAt                   DateTime          @default(now()) @db.Timestamptz
  user                        User              @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@map("audit")
}

model Notification {
  id                          String            @id @default(uuid())
  userId                      String

  title                       String
  message                     String
  type                        String            @default("system")

  link                        String?
  metadata                    String?

  createdAt                   DateTime          @default(now()) @db.Timestamptz
  readAt                      DateTime?         @db.Timestamptz

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

  @@index([userId, readAt])
  @@index([readAt])
  @@map("notification")
}

model Box {
  id                          String            @id @default(uuid())
  name                        String
  description                 String?
  tags                        String[]          @default([])
  color                       String?           @default("#000000")
  houseId                     String?
  createrId                   String

  createdAt                   DateTime          @default(now()) @db.Timestamptz
  updatedAt                   DateTime          @updatedAt @db.Timestamptz
  deletedAt                   DateTime?         @db.Timestamptz
  isPrivate                   Boolean           @default(false)

  items                       Item[]
  house                       House?            @relation(fields: [houseId], references: [id], onDelete: SetNull)
  user                        User              @relation(fields: [createrId], references: [id], onDelete: Cascade)

  @@index([houseId])
  @@index([createrId])
  @@index([isPrivate])
  @@index([tags])
  @@map("box")
}

model Item {
  id                          String            @id @default(uuid())
  createrId                   String
  boxId                       String?
  image                       String
  name                        String
  description                 String?
  price                       String?
  tags                        String[]          @default([])
  quantity                    Int
  expiresAt                   DateTime          @default(now()) @db.Timestamptz
  createdAt                   DateTime          @default(now()) @db.Timestamptz
  updatedAt                   DateTime          @updatedAt @db.Timestamptz
  deletedAt                   DateTime?         @db.Timestamptz

  user                        User              @relation(fields: [createrId], references: [id], onDelete: Cascade)
  box                         Box?              @relation(fields: [boxId], references: [id], onDelete: SetNull)

  @@index([createrId])
  @@index([boxId])
  @@index([tags])
  @@map("item")
}
