datasource db {
  provider = "postgresql"
}

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

// 1. Define the possible roles
enum Role {
  USER
  ADMIN
}

model User {
  id            String    @id @default(uuid())
  name          String?   
  email         String    @unique
  role          Role      @default(USER) // 2. Add this line (Defaults to USER)
  emailVerified DateTime?
  image         String?
  azureAdUserId String?   @unique
  
  createdAt     DateTime  @default(now())
  updatedAt     DateTime  @updatedAt

  nodes         FileNode[]
  accounts      Account[]
  sessions      Session[]
}

model Account {
  id                 String  @id @default(uuid())
  userId             String
  type               String
  provider           String
  providerAccountId  String
  refresh_token      String?  @db.Text
  access_token       String?  @db.Text
  expires_at         Int?
  token_type         String?
  scope              String?
  id_token           String?  @db.Text
  session_state      String?

  user User @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@unique([provider, providerAccountId])
}

model Session {
  id           String   @id @default(uuid())
  sessionToken String   @unique
  userId       String
  expires      DateTime
  user         User     @relation(fields: [userId], references: [id], onDelete: Cascade)
}

model FileNode {
  id           String    @id
  name         String
  size         BigInt?   // Preserved your BigInt size column
  isFolder     Boolean   @default(false)
  oneDriveId   String?   @unique
  path         String
  orderIndex   Int       @default(0)
  metadata     Json      @default("{}")
  description  String?

  ownerId      String
  owner        User      @relation(fields: [ownerId], references: [id])

  parentId     String?
  // Added onDelete: Cascade here to allow deleting folders and their children automatically
  parent       FileNode?  @relation("TreeHierarchy", fields: [parentId], references: [id], onDelete: Cascade)
  children     FileNode[] @relation("TreeHierarchy")

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

  @@unique([ownerId, path])
  @@index([parentId])
  @@index([orderIndex])
}