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

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

model User {
  id          String       @id @default(uuid())
  username    String       @unique
  phone       String       @unique
  createdAt   DateTime     @default(now())
  updatedAt   DateTime     @updatedAt
  collections Collection[]
  redemptions Redemption[]
}

model Stamp {
  id          String       @id @default(uuid())
  name        String
  note        String?
  imageColor  String
  imageGrey   String
  sortOrder   Int          @default(0)
  enabled     Boolean      @default(true)
  createdAt   DateTime     @default(now())
  updatedAt   DateTime     @updatedAt
  collections Collection[]
}

model Collection {
  id          String   @id @default(uuid())
  userId      String
  stampId     String
  collectedAt DateTime @default(now())
  user        User     @relation(fields: [userId], references: [id], onDelete: Cascade)
  stamp       Stamp    @relation(fields: [stampId], references: [id], onDelete: Cascade)

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

model RedemptionRule {
  id          String       @id @default(uuid())
  name        String
  description String?
  threshold   Int
  enabled     Boolean      @default(true)
  sortOrder   Int          @default(0)
  createdAt   DateTime     @default(now())
  updatedAt   DateTime     @updatedAt
  redemptions Redemption[]
}

model Redemption {
  id         String         @id @default(uuid())
  userId     String
  ruleId     String
  stampCount Int
  redeemedAt DateTime       @default(now())
  user       User           @relation(fields: [userId], references: [id], onDelete: Cascade)
  rule       RedemptionRule @relation(fields: [ruleId], references: [id])

  @@index([userId])
}
