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

datasource db {
  provider = "sqlite"
}

// ─── Auth ────────────────────────────────────────────────────────────────────

model User {
  id            String    @id @default(cuid())
  email         String    @unique
  emailVerified DateTime?
  name          String
  passwordHash  String
  plan          String    @default("FREE")  // FREE | PREMIUM
  stripeId      String?   @unique
  createdAt     DateTime  @default(now())
  updatedAt     DateTime  @updatedAt

  readings       Reading[]
  patient        Patient?
  dailyAnalysis  DailyAnalysis?
  verifyTokens   VerifyToken[]
  subscription   Subscription?
}

model VerifyToken {
  id        String   @id @default(cuid())
  token     String   @unique
  userId    String
  user      User     @relation(fields: [userId], references: [id], onDelete: Cascade)
  expiresAt DateTime
  createdAt DateTime @default(now())
}

// ─── Domain ──────────────────────────────────────────────────────────────────

model Reading {
  id         Int      @id @default(autoincrement())
  userId     String
  user       User     @relation(fields: [userId], references: [id], onDelete: Cascade)
  measuredAt DateTime
  moment     String
  value      Float
  notes      String?
  createdAt  DateTime @default(now())

  @@index([userId, measuredAt])
}

model Patient {
  id            String    @id @default(cuid())
  userId        String    @unique
  user          User      @relation(fields: [userId], references: [id], onDelete: Cascade)
  firstName     String
  lastName      String
  email         String?
  birthDate     DateTime?
  heightCm      Int?
  weightKg      Float?
  sex           String?
  diabetesType  String?
  treatment     String?
  updatedAt     DateTime  @updatedAt
}

model DailyAnalysis {
  id          String   @id @default(cuid())
  userId      String   @unique
  user        User     @relation(fields: [userId], references: [id], onDelete: Cascade)
  content     String
  generatedAt DateTime @default(now())
}

model Subscription {
  id                String   @id @default(cuid())
  userId            String   @unique
  user              User     @relation(fields: [userId], references: [id], onDelete: Cascade)
  stripeId          String   @unique
  stripePriceId     String
  stripeCustomerId  String
  status            String   // active | past_due | canceled | unpaid
  currentPeriodEnd  DateTime
  canceledAt        DateTime?
  createdAt         DateTime @default(now())
  updatedAt         DateTime  @updatedAt
}
