// 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"
  previewFeatures = ["driverAdapters"]
}

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


model UserPreferences {
  id                String   @id @default(cuid())
  userId            String   @unique
  substance         String   @default("nicotine")
  trackingStartDate String?
  hasCompletedSetup Boolean  @default(false)
  dailyGoal         Int?
  userName          String?
  userAge           Int?
  religion          String?
  lastNicotineUsageTime String?
  lastWeedUsageTime     String?
  createdAt         DateTime @default(now())
  updatedAt         DateTime @updatedAt

  // Quit plan fields stored as JSON string
  quitPlanJson String?

  usageEntries     UsageEntry[]
  achievements     Achievement[]
  reminderSettings ReminderSettings?
  savingsConfig    SavingsConfig?
  moodEntries      MoodEntry[]
}

model UsageEntry {
  id        String   @id @default(cuid())
  userId    String
  date      String
  count     Int
  substance String
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  userPreferences UserPreferences? @relation(fields: [userId], references: [userId])

  @@unique([userId, date, substance])
}

model Achievement {
  id         String   @id @default(cuid())
  userId     String
  badgeId    String
  unlockedAt DateTime @default(now())
  substance  String

  userPreferences UserPreferences? @relation(fields: [userId], references: [userId])

  @@unique([userId, badgeId, substance])
}

model ReminderSettings {
  id               String   @id @default(cuid())
  userId           String   @unique
  enabled          Boolean  @default(false)
  reminderTime     String   @default("09:00")
  lastNotifiedDate String?
  createdAt        DateTime @default(now())
  updatedAt        DateTime @updatedAt

  userPreferences UserPreferences? @relation(fields: [userId], references: [userId])
}

model SavingsConfig {
  id          String   @id @default(cuid())
  userId      String   @unique
  costPerUnit Float
  unitsPerDay Float
  savingsGoal Float?
  goalName    String?
  currency    String   @default("USD")
  substance   String
  createdAt   DateTime @default(now())
  updatedAt   DateTime @updatedAt

  userPreferences UserPreferences? @relation(fields: [userId], references: [userId])
}

model MoodEntry {
  id        String   @id @default(cuid())
  userId    String
  mood      String   // "good", "neutral", "bad" (kept for backward compat or categorical)
  score     Int      @default(50) // 0-100
  date      String   // YYYY-MM-DD
  comment   String?
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  userPreferences UserPreferences? @relation(fields: [userId], references: [userId])

  @@index([userId, date])
}
