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

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

model User {
  id        String   @id @db.VarChar(255)
  username  String   @db.VarChar(255)
  fullName  String?  @map("full_name") @db.VarChar(255)
  email     String   @db.VarChar(255)
  createdAt DateTime @default(now()) @map("created_at")
  updatedAt DateTime @updatedAt @map("updated_at")

  clients       Client[]
  projects      Project[]
  timeEntries   TimeEntry[]
  ongoingTimer  OngoingTimer?
  clientTargets ClientTarget[]
  apiKeys       ApiKey[]

  @@map("users")
}

model Client {
  id          String    @id @default(uuid())
  name        String    @db.VarChar(255)
  description String?   @db.Text
  createdAt   DateTime  @default(now()) @map("created_at")
  updatedAt   DateTime  @updatedAt @map("updated_at")
  deletedAt   DateTime? @map("deleted_at")

  userId        String         @map("user_id") @db.VarChar(255)
  user          User           @relation(fields: [userId], references: [id], onDelete: Cascade)
  projects      Project[]
  clientTargets ClientTarget[]

  @@index([userId])
  @@map("clients")
}

model Project {
  id          String    @id @default(uuid())
  name        String    @db.VarChar(255)
  description String?   @db.Text
  color       String?   @db.VarChar(7) // Hex color code
  createdAt   DateTime  @default(now()) @map("created_at")
  updatedAt   DateTime  @updatedAt @map("updated_at")
  deletedAt   DateTime? @map("deleted_at")

  userId   String @map("user_id") @db.VarChar(255)
  user     User   @relation(fields: [userId], references: [id], onDelete: Cascade)
  clientId String @map("client_id")
  client   Client @relation(fields: [clientId], references: [id], onDelete: Cascade)

  timeEntries   TimeEntry[]
  ongoingTimers OngoingTimer[]

  @@index([userId])
  @@index([clientId])
  @@map("projects")
}

model TimeEntry {
  id           String    @id @default(uuid())
  startTime    DateTime  @map("start_time") @db.Timestamptz()
  endTime      DateTime  @map("end_time") @db.Timestamptz()
  breakMinutes Int       @default(0) @map("break_minutes")
  description  String?   @db.Text
  createdAt    DateTime  @default(now()) @map("created_at")
  updatedAt    DateTime  @updatedAt @map("updated_at")
  deletedAt    DateTime? @map("deleted_at")

  userId    String  @map("user_id") @db.VarChar(255)
  user      User    @relation(fields: [userId], references: [id], onDelete: Cascade)
  projectId String  @map("project_id")
  project   Project @relation(fields: [projectId], references: [id], onDelete: Cascade)

  @@index([userId])
  @@index([userId, startTime])
  @@index([projectId])
  @@map("time_entries")
}

model OngoingTimer {
  id        String   @id @default(uuid())
  startTime DateTime @map("start_time") @db.Timestamptz()
  createdAt DateTime @default(now()) @map("created_at")
  updatedAt DateTime @updatedAt @map("updated_at")

  userId    String   @unique @map("user_id") @db.VarChar(255)
  user      User     @relation(fields: [userId], references: [id], onDelete: Cascade)
  projectId String?  @map("project_id")
  project   Project? @relation(fields: [projectId], references: [id], onDelete: SetNull)

  @@index([userId])
  @@map("ongoing_timers")
}

enum PeriodType {
  WEEKLY
  MONTHLY
}

model ClientTarget {
  id          String     @id @default(uuid())
  targetHours Float      @map("target_hours")
  periodType  PeriodType @default(WEEKLY) @map("period_type")
  workingDays String[]   @map("working_days") // e.g. ["MON","WED","FRI"]
  startDate   DateTime   @map("start_date") @db.Date
  createdAt   DateTime   @default(now()) @map("created_at")
  updatedAt   DateTime   @updatedAt @map("updated_at")
  deletedAt   DateTime?  @map("deleted_at")

  userId   String @map("user_id") @db.VarChar(255)
  user     User   @relation(fields: [userId], references: [id], onDelete: Cascade)
  clientId String @map("client_id")
  client   Client @relation(fields: [clientId], references: [id], onDelete: Cascade)

  corrections BalanceCorrection[]

  @@unique([userId, clientId])
  @@index([userId])
  @@index([clientId])
  @@map("client_targets")
}

model BalanceCorrection {
  id          String    @id @default(uuid())
  date        DateTime  @map("date") @db.Date
  hours       Float
  description String?   @db.VarChar(255)
  createdAt   DateTime  @default(now()) @map("created_at")
  updatedAt   DateTime  @updatedAt @map("updated_at")
  deletedAt   DateTime? @map("deleted_at")

  clientTargetId String       @map("client_target_id")
  clientTarget   ClientTarget @relation(fields: [clientTargetId], references: [id], onDelete: Cascade)

  @@index([clientTargetId])
  @@map("balance_corrections")
}

model Session {
  id        String   @id
  sid       String   @unique
  data      String   @db.Text
  expiresAt DateTime @map("expires_at")

  @@map("sessions")
}

model ApiKey {
  id         String    @id @default(uuid())
  name       String    @db.VarChar(255)
  keyHash    String    @unique @map("key_hash") @db.VarChar(64) // SHA-256 hex
  prefix     String    @db.VarChar(16) // first chars of raw key for display
  lastUsedAt DateTime? @map("last_used_at")
  createdAt  DateTime  @default(now()) @map("created_at")

  userId String @map("user_id") @db.VarChar(255)
  user   User   @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@index([userId])
  @@map("api_keys")
}
