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

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

model User {
  id           String     @id @default(cuid())
  email        String     @unique
  name         String?
  passwordHash String
  role         String     @default("USER")
  status       String     @default("PENDING")
  emailVerified Boolean   @default(false)
  createdAt    DateTime   @default(now())
  events       Event[]    @relation("EventCreator")
  views        UserView[]
  accounts     Account[]
  sessions     Session[]
  resetTokens  PasswordResetToken[]
}

model Event {
  id          String      @id @default(cuid())
  title       String
  description String?
  location    String?
  locationPlaceId String?
  locationLat  Float?
  locationLng  Float?
  startAt     DateTime
  endAt       DateTime?
  status      String      @default("PENDING")
  createdAt   DateTime    @default(now())
  createdById String
  createdBy   User        @relation("EventCreator", fields: [createdById], references: [id])
  viewItems   UserViewItem[]
  categoryId  String?
  category    Category?   @relation(fields: [categoryId], references: [id])
  viewExclusions UserViewExclusion[]
}

model Category {
  id        String   @id @default(cuid())
  name      String   @unique
  createdAt DateTime @default(now())
  events    Event[]
  viewSubscriptions UserViewCategory[]
}

model UserView {
  id        String         @id @default(cuid())
  name      String
  token     String         @unique
  userId    String
  user      User           @relation(fields: [userId], references: [id])
  items     UserViewItem[]
  categories UserViewCategory[]
  exclusions UserViewExclusion[]
  createdAt DateTime       @default(now())
}

model UserViewItem {
  id      String   @id @default(cuid())
  viewId  String
  eventId String
  view    UserView @relation(fields: [viewId], references: [id])
  event   Event    @relation(fields: [eventId], references: [id])

  @@unique([viewId, eventId])
}

model UserViewCategory {
  id         String   @id @default(cuid())
  viewId     String
  categoryId String
  view       UserView @relation(fields: [viewId], references: [id])
  category   Category @relation(fields: [categoryId], references: [id])
  createdAt  DateTime @default(now())

  @@unique([viewId, categoryId])
}

model UserViewExclusion {
  id        String   @id @default(cuid())
  viewId    String
  eventId   String
  view      UserView @relation(fields: [viewId], references: [id])
  event     Event    @relation(fields: [eventId], references: [id])
  createdAt DateTime @default(now())

  @@unique([viewId, eventId])
}

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?

  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 VerificationToken {
  identifier String
  token      String   @unique
  expires    DateTime

  @@unique([identifier, token])
}

model PasswordResetToken {
  id        String   @id @default(cuid())
  userId    String
  token     String   @unique
  expiresAt DateTime
  createdAt DateTime @default(now())

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

model Setting {
  id        String   @id @default(cuid())
  key       String   @unique
  value     String
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

model LoginAttempt {
  id          String   @id @default(cuid())
  email       String
  ip          String
  attempts    Int      @default(0)
  lastAttempt DateTime @default(now())
  lockedUntil DateTime?
  createdAt   DateTime @default(now())
  updatedAt   DateTime @updatedAt

  @@unique([email, ip])
}
