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
  passwordHash  String
  avatarColor   String    @default("#4B7BF5")
  createdAt     DateTime  @default(now())
  updatedAt     DateTime  @updatedAt
  lists         List[]
  tasks         Task[]
  tags          Tag[]
  sessions      Session[]
}

model Session {
  id        String   @id @default(cuid())
  userId    String
  token     String   @unique
  expiresAt DateTime
  createdAt DateTime @default(now())
  user User @relation(fields: [userId], references: [id], onDelete: Cascade)
}

model List {
  id        String   @id @default(cuid())
  userId    String
  name      String
  color     String   @default("#4B7BF5")
  icon      String   @default("list")
  sortOrder Int      @default(0)
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  user  User   @relation(fields: [userId], references: [id], onDelete: Cascade)
  tasks Task[]
  @@index([userId])
}

model Task {
  id          String    @id @default(cuid())
  userId      String
  listId      String
  parentId    String?
  title       String
  note        String?
  completed   Boolean   @default(false)
  completedAt DateTime?
  dueDate     DateTime?
  priority    Int       @default(0)
  sortOrder   Int       @default(0)
  createdAt   DateTime  @default(now())
  updatedAt   DateTime  @updatedAt
  user     User   @relation(fields: [userId], references: [id], onDelete: Cascade)
  list     List   @relation(fields: [listId], references: [id], onDelete: Cascade)
  parent   Task?  @relation("TaskChildren", fields: [parentId], references: [id], onDelete: Cascade)
  children Task[] @relation("TaskChildren")
  tags     TaskTag[]
  @@index([userId])
  @@index([listId])
  @@index([parentId])
}

model Tag {
  id     String @id @default(cuid())
  userId String
  name   String
  color  String @default("#6B6B6B")
  user  User      @relation(fields: [userId], references: [id], onDelete: Cascade)
  tasks TaskTag[]
  @@unique([userId, name])
  @@index([userId])
}

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