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

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

model User {
  id              String           @id @default(cuid())
  email           String           @unique
  name            String?
  firstName       String?
  lastName        String?
  avatar          String?          // URL de l'avatar
  role            String           @default("user") // user, admin, etc.
  isActive        Boolean          @default(true)
  lastLoginAt     DateTime?
  password        String           // Hashé avec bcrypt
  createdAt       DateTime         @default(now())
  updatedAt       DateTime         @updatedAt
  preferences     UserPreferences?
  notes           Note[]
  dailyCheckboxes DailyCheckbox[]
  tasks           Task[]           @relation("TaskOwner")
  tags            Tag[]            @relation("TagOwner")
  folders         Folder[]         @relation("FolderOwner")

  @@map("users")
}

model Task {
  id               String          @id @default(cuid())
  title            String
  description      String?
  status           String          @default("todo")
  priority         String          @default("medium")
  source           String
  sourceId         String?
  dueDate          DateTime?
  completedAt      DateTime?
  createdAt        DateTime        @default(now())
  updatedAt        DateTime        @updatedAt
  jiraProject      String?
  jiraKey          String?
  assignee         String?         // Legacy field - keep for Jira/TFS compatibility
  ownerId          String          // Required - chaque tâche appartient à un user
  owner            User            @relation("TaskOwner", fields: [ownerId], references: [id], onDelete: Cascade)
  jiraType         String?
  tfsProject       String?
  tfsPullRequestId Int?
  tfsRepository    String?
  tfsSourceBranch  String?
  tfsTargetBranch  String?
  primaryTagId     String?
  primaryTag       Tag?            @relation("PrimaryTag", fields: [primaryTagId], references: [id])
  dailyCheckboxes  DailyCheckbox[]
  taskTags         TaskTag[]
  notes            Note[]          // Notes associées à cette tâche

  @@unique([source, sourceId])
  @@map("tasks")
}

model Tag {
  id           String    @id @default(cuid())
  name         String
  color        String    @default("#6b7280")
  isPinned     Boolean   @default(false)
  ownerId      String    // Chaque tag appartient à un utilisateur
  owner        User      @relation("TagOwner", fields: [ownerId], references: [id], onDelete: Cascade)
  taskTags     TaskTag[]
  primaryTasks Task[]    @relation("PrimaryTag")
  noteTags     NoteTag[]
  folders      Folder[]

  @@unique([name, ownerId]) // Un nom de tag unique par utilisateur
  @@map("tags")
}

model TaskTag {
  taskId String
  tagId  String
  tag    Tag    @relation(fields: [tagId], references: [id], onDelete: Cascade)
  task   Task   @relation(fields: [taskId], references: [id], onDelete: Cascade)

  @@id([taskId, tagId])
  @@map("task_tags")
}

model SyncLog {
  id        String   @id @default(cuid())
  source    String
  status    String
  message   String?
  tasksSync Int      @default(0)
  createdAt DateTime @default(now())

  @@map("sync_logs")
}

model DailyCheckbox {
  id        String   @id @default(cuid())
  date      DateTime
  text      String
  isChecked Boolean  @default(false)
  type      String   @default("task")
  order     Int      @default(0)
  taskId    String?
  userId    String
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  task      Task?    @relation(fields: [taskId], references: [id])
  user      User     @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@index([date])
  @@index([userId])
  @@map("daily_checkboxes")
}

model UserPreferences {
  id               String   @id @default(cuid())
  userId           String   @unique
  kanbanFilters    Json?
  viewPreferences  Json?
  columnVisibility Json?
  jiraConfig       Json?
  jiraAutoSync     Boolean  @default(false)
  jiraSyncInterval String   @default("daily")
  tfsConfig        Json?
  tfsAutoSync      Boolean  @default(false)
  tfsSyncInterval  String   @default("daily")
  createdAt        DateTime @default(now())
  updatedAt        DateTime @updatedAt
  user             User     @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@map("user_preferences")
}

model Note {
  id        String   @id @default(cuid())
  title     String
  content   String   // Markdown content
  userId    String
  taskId    String?  // Tâche associée à la note
  folderId  String?  // Dossier contenant la note
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  user      User     @relation(fields: [userId], references: [id], onDelete: Cascade)
  task      Task?    @relation(fields: [taskId], references: [id])
  folder    Folder?  @relation(fields: [folderId], references: [id])
  noteTags  NoteTag[]
}

model Folder {
  id        String   @id @default(cuid())
  name      String
  userId    String
  tagId     String?  // Tag associé au dossier
  parentId  String?  // Dossier parent pour sous-dossiers
  order     Int      @default(0)
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  user      User     @relation("FolderOwner", fields: [userId], references: [id], onDelete: Cascade)
  tag       Tag?     @relation(fields: [tagId], references: [id])
  parent    Folder?  @relation("FolderHierarchy", fields: [parentId], references: [id], onDelete: Cascade)
  children  Folder[] @relation("FolderHierarchy")
  notes     Note[]

  @@map("folders")
}

model NoteTag {
  noteId String
  tagId  String
  tag    Tag    @relation(fields: [tagId], references: [id], onDelete: Cascade)
  note   Note   @relation(fields: [noteId], references: [id], onDelete: Cascade)

  @@id([noteId, tagId])
  @@map("note_tags")
}
