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

datasource db {
  provider = "sqlite"
}

model Project {
  id          String   @id @default(cuid())
  name        String
  description String   @default("")
  type        String   @default("technical") // technical | business | custom
  status      String   @default("backlog")   // backlog | active | completed | archived
  phase       String   @default("")
  phaseConfig String   @default("[]")        // JSON array of phase definitions
  health      String   @default("on-track")  // on-track | at-risk | blocked
  nextStep    String   @default("")
  sortOrder   Int      @default(0)
  metadata    String   @default("{}")         // JSON flexible fields
  leadId      String   @default("")
  teamId      String?

  team                 Team?               @relation(fields: [teamId], references: [id])
  kanbanItems          KanbanItem[]
  kanbanColumns        KanbanColumn[]
  kanbanLabels         KanbanLabel[]
  transcriptSummaries  TranscriptSummary[]
  metrics              Metric[]
  objectives           Objective[]
  teamAssignments      TeamAssignment[]

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  @@index([teamId])
}

model KanbanItem {
  id          String    @id @default(cuid())
  projectId   String
  title       String
  description String    @default("")
  column      String    @default("To Do")
  sortOrder   Int       @default(0)
  priority    String    @default("medium") // low | medium | high | critical
  assigneeId  String    @default("")
  dueDate     DateTime?
  source      String    @default("manual") // manual | transcript
  sourceId    String    @default("")
  isNew       Boolean   @default(false)
  approved    Boolean   @default(true)
  dismissed   Boolean   @default(false)
  metadata    String    @default("{}")

  project    Project          @relation(fields: [projectId], references: [id], onDelete: Cascade)
  labels     KanbanItemLabel[]
  subtasks   KanbanSubtask[]
  activities KanbanActivity[]

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  @@index([projectId])
}

model KanbanColumn {
  id        String @id @default(cuid())
  projectId String
  name      String
  sortOrder Int    @default(0)
  wipLimit  Int    @default(0) // 0 = no limit
  color     String @default("")

  project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  @@unique([projectId, name])
  @@index([projectId])
}

model KanbanLabel {
  id        String @id @default(cuid())
  projectId String
  name      String
  color     String @default("#3b82f6")

  project    Project          @relation(fields: [projectId], references: [id], onDelete: Cascade)
  itemLabels KanbanItemLabel[]

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  @@unique([projectId, name])
  @@index([projectId])
}

model KanbanItemLabel {
  id      String @id @default(cuid())
  itemId  String
  labelId String

  item  KanbanItem  @relation(fields: [itemId], references: [id], onDelete: Cascade)
  label KanbanLabel @relation(fields: [labelId], references: [id], onDelete: Cascade)

  @@unique([itemId, labelId])
  @@index([itemId])
  @@index([labelId])
}

model KanbanSubtask {
  id        String  @id @default(cuid())
  itemId    String
  title     String
  completed Boolean @default(false)
  sortOrder Int     @default(0)

  item KanbanItem @relation(fields: [itemId], references: [id], onDelete: Cascade)

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  @@index([itemId])
}

model KanbanActivity {
  id       String @id @default(cuid())
  itemId   String
  type     String // status_change | assignment | priority | comment | label | created
  actorId  String @default("")
  content  String @default("")
  metadata String @default("{}") // JSON: {from, to} for changes

  item KanbanItem @relation(fields: [itemId], references: [id], onDelete: Cascade)

  createdAt DateTime @default(now())

  @@index([itemId])
}

model TranscriptSummary {
  id           String  @id @default(cuid())
  projectId    String
  title        String
  date         DateTime
  summary      String  @default("")
  participants String  @default("[]") // JSON array
  actionItems  String  @default("[]") // JSON array
  sourceFile   String  @default("")
  processed    Boolean @default(false)

  project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  @@index([projectId])
}

model Metric {
  id        String @id @default(cuid())
  projectId String
  name      String
  value     Float  @default(0)
  unit      String @default("")
  category  String @default("")
  trend     String @default("stable") // up | down | stable
  history   String @default("[]") // JSON array of {date, value}

  project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  @@unique([projectId, name])
  @@index([projectId])
}

model Objective {
  id          String @id @default(cuid())
  projectId   String
  title       String
  description String @default("")
  status      String @default("not-started") // not-started | in-progress | completed
  sortOrder   Int    @default(0)

  project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  @@index([projectId])
}

model TeamAssignment {
  id        String @id @default(cuid())
  projectId String
  memberId  String
  role      String @default("member")

  project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  @@unique([projectId, memberId])
  @@index([projectId])
}

model Team {
  id          String    @id @default(cuid())
  slug        String    @unique
  name        String
  description String    @default("")
  sortOrder   Int       @default(0)
  projects    Project[]
  createdAt   DateTime  @default(now())
  updatedAt   DateTime  @updatedAt
}

model User {
  id                 String   @id @default(cuid())
  email              String   @unique
  passwordHash       String
  name               String   @default("")
  mustChangePassword Boolean  @default(true)
  createdAt          DateTime @default(now())
  updatedAt          DateTime @updatedAt
}

model PersonalIdea {
  id                  String   @id @default(cuid())
  ownerId             String
  title               String
  description         String   @default("")
  priority            String   @default("medium")
  status              String   @default("idea") // idea | ready | promoted | discarded
  promotedToProjectId String?
  sortOrder           Int      @default(0)
  createdAt           DateTime @default(now())
  updatedAt           DateTime @updatedAt

  @@index([ownerId])
}
