// 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"
}

datasource db {
  provider = "sqlite"
}

model User {
  id           String   @id @default(uuid())
  email        String   @unique
  password     String
  role         String   @default("USER") // USER, ADMIN
  isFirstLogin Boolean  @default(true)
  isBlocked    Boolean  @default(false)
  createdAt    DateTime @default(now())
  updatedAt    DateTime @updatedAt

  profile      UserProfile?
  sessions     WorkoutSession[]
  exercises    Exercise[]
  plans        WorkoutPlan[]
  weightRecords BodyWeightRecord[]
  savedMessages SavedMessage[]
}

model SavedMessage {
  id        String   @id @default(uuid())
  userId    String
  user      User     @relation(fields: [userId], references: [id], onDelete: Cascade)
  content   String
  role      String   @default("model")
  createdAt DateTime @default(now())
}

model BodyWeightRecord {
  id        String   @id @default(uuid())
  userId    String
  user      User     @relation(fields: [userId], references: [id], onDelete: Cascade)
  weight    Float
  date      DateTime @default(now())
  dateStr   String   // YYYY-MM-DD for unique constraint

  @@unique([userId, dateStr])
}

model UserProfile {
  id     String @id @default(uuid())
  userId String @unique
  user   User   @relation(fields: [userId], references: [id], onDelete: Cascade)
  weight    Float?
  height    Float?
  gender    String?
  birthDate DateTime?
  language  String?   @default("en")
  restTimerDefault Int? @default(120) // Default rest timer in seconds
}

model Exercise {
  id                   String   @id @default(uuid())
  userId               String?  // Null means system default
  user                 User?    @relation(fields: [userId], references: [id], onDelete: Cascade)
  name                 String
  type                 String   // STRENGTH, CARDIO, BODYWEIGHT, STATIC
  bodyWeightPercentage Float?   @default(0)
  isArchived           Boolean  @default(false)
  isUnilateral         Boolean  @default(false)
  
  sets                 WorkoutSet[]
  planExercises        PlanExercise[]
}

model WorkoutSession {
  id             String   @id @default(uuid())
  userId         String
  user           User     @relation(fields: [userId], references: [id], onDelete: Cascade)
  startTime      DateTime
  endTime        DateTime?
  userBodyWeight Float?
  note           String?
  planId         String?
  planName       String?
  type           String   @default("STANDARD") // STANDARD, QUICK_LOG
  
  sets           WorkoutSet[]
}

model WorkoutSet {
  id             String         @id @default(uuid())
  sessionId      String
  session        WorkoutSession @relation(fields: [sessionId], references: [id], onDelete: Cascade)
  exerciseId     String
  exercise       Exercise       @relation(fields: [exerciseId], references: [id])
  
  order          Int
  weight         Float?
  reps           Int?
  distanceMeters Float?
  durationSeconds Int?
  height         Float?
  bodyWeightPercentage Float?
  completed      Boolean        @default(true)
  side           String?        // LEFT, RIGHT, or null for bilateral
  timestamp      DateTime       @default(now())
}

model WorkoutPlan {
  id          String   @id @default(uuid())
  userId      String
  user        User     @relation(fields: [userId], references: [id], onDelete: Cascade)
  name        String
  description String?
  exercises   String?  // JSON string of exercise IDs (Deprecated, to be removed)
  planExercises PlanExercise[]
  createdAt   DateTime @default(now())
  updatedAt   DateTime @updatedAt
}

model PlanExercise {
  id           String      @id @default(uuid())
  planId       String
  plan         WorkoutPlan @relation(fields: [planId], references: [id], onDelete: Cascade)
  exerciseId   String
  exercise     Exercise    @relation(fields: [exerciseId], references: [id])
  order        Int
  isWeighted   Boolean     @default(false)
  restTime     Int?        // Optional rest time target in seconds
}

