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

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

enum UserRole {
  USER
  ADMIN
  CREATOR
  MANAGER
}

enum UserStatus {
  ACTIVE
  BANNED
  FLAGGED
}

enum MaterialType {
  CODE
  ASSET_ZIP
  VIDEO
}

model User {
  id        String   @id @default(uuid())
  username  String   @unique @db.VarChar(50)
  password  String?  @db.VarChar(255) // Made optional for guest/demo purposes if needed
  avatarUrl String?  @db.VarChar(500)
  role      UserRole @default(CREATOR)
  status    UserStatus @default(ACTIVE)
  createdAt DateTime @default(now())
  lastLogin DateTime @updatedAt

  materials Material[]
  comments  Comment[]
  favorites Favorite[]

  @@index([username])
}

model Material {
  id          String       @id @default(uuid())
  title       String       @db.VarChar(100)
  description String       @db.Text
  type        MaterialType
  contentUrl  String?      @db.VarChar(500)
  codeSnippet String?      @db.LongText
  language    String?      @db.VarChar(20)
  
  views       Int          @default(0)
  downloads   Int          @default(0)

  authorId    String
  author      User         @relation(fields: [authorId], references: [id], onDelete: Cascade)

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

  comments    Comment[]
  favorites   Favorite[]
  tags        Tag[]

  @@index([type])
  @@index([authorId])
}

model Comment {
  id         String   @id @default(uuid())
  content    String   @db.VarChar(1000)
  createdAt  DateTime @default(now())
  
  authorId   String
  author     User     @relation(fields: [authorId], references: [id])
  
  materialId String
  material   Material @relation(fields: [materialId], references: [id], onDelete: Cascade)

  @@index([materialId])
}

model Favorite {
  userId     String
  materialId String
  user       User     @relation(fields: [userId], references: [id])
  material   Material @relation(fields: [materialId], references: [id])

  @@id([userId, materialId])
}

model Tag {
  id        Int        @id @default(autoincrement())
  name      String     @unique
  materials Material[]
}
