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

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

model User {
  id        String   @id @default(cuid())
  email     String   @unique
  name      String
  avatar    String?
  createdAt DateTime @default(now()) @map("created_at")
  updatedAt DateTime @updatedAt @map("updated_at")

  createdTasks   Task[]          @relation("TaskCreator")
  assignedTasks  Task[]          @relation("TaskAssignee")
  projectMembers ProjectMember[]
  requirements   Requirement[]
  watchedTasks   TaskWatcher[]

  @@map("users")
}

model Product {
  id          String   @id @default(cuid())
  name        String
  description String   @default("")
  createdAt   DateTime @default(now()) @map("created_at")
  updatedAt   DateTime @updatedAt @map("updated_at")

  projects     Project[]
  requirements Requirement[]
  versions     Version[]

  @@map("products")
}

model Project {
  id          String   @id @default(cuid())
  productId   String   @map("product_id")
  name        String
  description String   @default("")
  createdAt   DateTime @default(now()) @map("created_at")
  updatedAt   DateTime @updatedAt @map("updated_at")

  product Product         @relation(fields: [productId], references: [id])
  sprints Sprint[]
  tasks   Task[]
  members ProjectMember[]

  @@map("projects")
}

model Sprint {
  id        String   @id @default(cuid())
  projectId String   @map("project_id")
  name      String
  startDate DateTime @map("start_date")
  endDate   DateTime @map("end_date")
  createdAt DateTime @default(now()) @map("created_at")
  updatedAt DateTime @updatedAt @map("updated_at")

  project Project @relation(fields: [projectId], references: [id])
  tasks   Task[]

  @@map("sprints")
}

model Version {
  id          String    @id @default(cuid())
  productId   String    @map("product_id")
  name        String
  description String    @default("")
  releaseDate DateTime? @map("release_date")
  createdAt   DateTime  @default(now()) @map("created_at")
  updatedAt   DateTime  @updatedAt @map("updated_at")

  product Product @relation(fields: [productId], references: [id])
  tasks   Task[]

  @@map("versions")
}

model Requirement {
  id          String   @id @default(cuid())
  productId   String   @map("product_id")
  title       String
  description String   @default("")
  status      String   @default("draft")
  priority    Int      @default(0)
  creatorId   String   @map("creator_id")
  createdAt   DateTime @default(now()) @map("created_at")
  updatedAt   DateTime @updatedAt @map("updated_at")

  product Product @relation(fields: [productId], references: [id])
  creator User    @relation(fields: [creatorId], references: [id])

  @@map("requirements")
}

model Task {
  id          String    @id @default(cuid())
  projectId   String    @map("project_id")
  sprintId    String?   @map("sprint_id")
  versionId   String?   @map("version_id")
  parentId    String?   @map("parent_id")
  title       String
  description String    @default("")
  status      String    @default("todo")
  priority    Int       @default(0)
  assigneeId  String?   @map("assignee_id")
  creatorId   String    @map("creator_id")
  startDate   DateTime? @map("start_date")
  dueDate     DateTime? @map("due_date")
  createdAt   DateTime  @default(now()) @map("created_at")
  updatedAt   DateTime  @updatedAt @map("updated_at")

  project  Project      @relation(fields: [projectId], references: [id])
  sprint   Sprint?      @relation(fields: [sprintId], references: [id])
  version  Version?     @relation(fields: [versionId], references: [id])
  parent   Task?        @relation("TaskChildren", fields: [parentId], references: [id])
  children Task[]       @relation("TaskChildren")
  assignee User?        @relation("TaskAssignee", fields: [assigneeId], references: [id])
  creator  User         @relation("TaskCreator", fields: [creatorId], references: [id])
  watchers TaskWatcher[]
  comments Comment[]

  @@map("tasks")
}

model TaskWatcher {
  id     String @id @default(cuid())
  taskId String @map("task_id")
  userId String @map("user_id")

  task Task @relation(fields: [taskId], references: [id])
  user User @relation(fields: [userId], references: [id])

  @@unique([taskId, userId])
  @@map("task_watchers")
}

model Comment {
  id        String   @id @default(cuid())
  taskId    String   @map("task_id")
  authorId  String   @map("author_id")
  content   String
  createdAt DateTime @default(now()) @map("created_at")

  task Task @relation(fields: [taskId], references: [id])

  @@map("comments")
}

model ProjectMember {
  id        String @id @default(cuid())
  projectId String @map("project_id")
  userId    String @map("user_id")
  role      String @default("member")

  project Project @relation(fields: [projectId], references: [id])
  user    User    @relation(fields: [userId], references: [id])

  @@unique([projectId, userId])
  @@map("project_members")
}

model AppData {
  key       String   @id
  value     Json
  createdAt DateTime @default(now()) @map("created_at")
  updatedAt DateTime @updatedAt @map("updated_at")

  @@map("app_data")
}
