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

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

model User {
    id         String     @id @default(uuid())
    pubkey     String     @unique
    username   String?    @unique
    avatar     String?
    purchased  Purchase[]
    courses    Course[]   // Relation field added for courses created by the user
    resources  Resource[] // Relation field added for resources created by the user
    drafts     Draft[]    // Relation field added for drafts created by the user
    role       Role?      @relation(fields: [roleId], references: [id])
    roleId     String?
    createdAt  DateTime   @default(now())
    updatedAt  DateTime   @updatedAt
}

model Role {
    id          String    @id @default(uuid())
    subscribed  Boolean   @default(false)
    users       User[]
}

model Purchase {
    id          String    @id @default(uuid())
    user        User      @relation(fields: [userId], references: [id])
    userId      String
    course      Course?   @relation(fields: [courseId], references: [id])
    courseId    String?
    resource    Resource? @relation(fields: [resourceId], references: [id])
    resourceId  String?
    amountPaid  Int       // in satoshis
    createdAt   DateTime  @default(now())
    updatedAt   DateTime  @updatedAt
}

model Course {
    id          String    @id // Client generates UUID
    userId      String    
    user        User      @relation(fields: [userId], references: [id])
    price       Int       @default(0)
    resources   Resource[]
    purchases   Purchase[]
    noteId      String?   @unique
    createdAt   DateTime  @default(now())
    updatedAt   DateTime  @updatedAt
}

model Resource {
    id        String    @id // Client generates UUID
    userId    String    
    user      User      @relation(fields: [userId], references: [id])
    course    Course?   @relation(fields: [courseId], references: [id])
    courseId  String?
    price     Int       @default(0)
    purchases Purchase[]
    noteId    String?   @unique
    createdAt DateTime  @default(now())
    updatedAt DateTime  @updatedAt
}

model Draft {
    id        String    @id @default(uuid())
    userId    String
    user      User      @relation(fields: [userId], references: [id])
    type      String
    title     String
    summary   String
    content   String
    image     String?
    price     Int?      @default(0)
    topics    String[]
    createdAt DateTime  @default(now())
    updatedAt DateTime  @updatedAt
}

