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

datasource db {
  provider = "postgresql"
}

enum Role {
  ADMIN
  PARTNER_USER
}

enum DocumentType {
  NDA
  MSA
}

model Organization {
  id           String        @id @default(uuid())
  name         String
  status       String        @default("ACTIVE")
  createdAt    DateTime      @default(now())
  updatedAt    DateTime      @updatedAt
  users        User[]
  sharedAssets SharedAsset[]
}

model User {
  id               String            @id @default(uuid())
  email            String            @unique
  passwordHash     String
  role             Role              @default(PARTNER_USER)
  mfaEnabled       Boolean           @default(true)
  mfaSecret        String?
  inviteToken      String?           @unique
  inviteTokenExp   DateTime?
  organizationId   String?
  onboardingStatus String            @default("PENDING_ONBOARDING")
  partnerGroup     String?
  assignedNdaId    String?
  assignedMsaId    String?
  website          String?
  sector           String?
  companySize      String?
  defaultTheme     String?           @default("dark")
  companyName      String?
  showEcosystemTab Boolean           @default(true)
  assignedNda      LegalDocument?    @relation("AssignedNda", fields: [assignedNdaId], references: [id], onDelete: SetNull)
  assignedMsa      LegalDocument?    @relation("AssignedMsa", fields: [assignedMsaId], references: [id], onDelete: SetNull)
  organization     Organization?     @relation(fields: [organizationId], references: [id])
  createdAt        DateTime          @default(now())
  updatedAt        DateTime          @updatedAt
  acceptances      LegalAcceptance[]
  auditLogs        AuditLog[]
  sharedAssets     SharedAsset[]
  downloadRequests DownloadRequest[]
}

model Asset {
  id             String            @id @default(uuid())
  title          String
  type           String
  size           Int
  url            String
  version        Int               @default(1)
  uploadedBy     String
  description    String?           @db.Text
  categoryId     String?
  subcategory    String?
  tags           String[]
  downloadsCount Int               @default(0)
  githubUrl      String?
  status         String            @default("published")
  isDownloadable Boolean           @default(true)
  thumbnailUrl   String?
  problemStatement String?           @db.Text
  solution       String?           @db.Text
  folderId       String?
  folder         Folder?           @relation(fields: [folderId], references: [id], onDelete: SetNull)
  sharedWith     SharedAsset[]
  downloadRequests DownloadRequest[]
  assetGroups    AssetGroup[]
  createdAt      DateTime          @default(now())
  updatedAt      DateTime          @updatedAt
}

model SharedAsset {
  id             String       @id @default(uuid())
  assetId        String
  organizationId String
  userId         String?
  createdAt      DateTime     @default(now())
  asset          Asset        @relation(fields: [assetId], references: [id], onDelete: Cascade)
  organization   Organization @relation(fields: [organizationId], references: [id], onDelete: Cascade)
  user           User?        @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@unique([assetId, organizationId, userId])
}

model DownloadRequest {
  id        String   @id @default(uuid())
  assetId   String
  userId    String
  status    String   @default("PENDING") // PENDING, APPROVED, REJECTED
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  asset     Asset    @relation(fields: [assetId], references: [id], onDelete: Cascade)
  user      User     @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@unique([assetId, userId])
}

model Folder {
  id        String   @id @default(uuid())
  name      String
  parentId  String?
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  assets    Asset[]
}

model LegalDocument {
  id               String            @id @default(uuid())
  type             DocumentType
  version          String
  content          String
  isActive         Boolean           @default(false)
  pdfUrl           String?
  createdAt        DateTime          @default(now())
  acceptances      LegalAcceptance[]
  assignedNdaUsers User[]            @relation("AssignedNda")
  assignedMsaUsers User[]            @relation("AssignedMsa")
}

model LegalAcceptance {
  id              String        @id @default(uuid())
  docId           String
  userId          String
  ipAddress       String
  signatureHash   String?
  documentUrl     String?
  signatureBase64 String?
  acceptedAt      DateTime      @default(now())
  document        LegalDocument @relation(fields: [docId], references: [id])
  user            User          @relation(fields: [userId], references: [id])
}

model AuditLog {
  id        String   @id @default(uuid())
  actorId   String
  action    String
  resource  String
  metadata  Json?
  createdAt DateTime @default(now())
  actor     User     @relation(fields: [actorId], references: [id])
}

model AssetGroup {
  id          String   @id @default(uuid())
  name        String   @unique
  description String?
  assets      Asset[]
  createdAt   DateTime @default(now())
  updatedAt   DateTime @updatedAt
}

model EcosystemOffering {
  id           String   @id @default(uuid())
  name         String   @unique
  type         String   // "PRODUCT" | "SERVICE"
  tagline      String
  description  String   @db.Text
  benefits     String[]
  websiteUrl   String
  ctaText      String   @default("Visit Website")
  logoIcon     String   @default("Globe")
  logoUrl      String?
  mediaUrl     String?
  mediaType    String?  // "IMAGE" | "VIDEO" | "GIF"
  orderIndex   Int      @default(0)
  isActive     Boolean  @default(true)
  createdAt    DateTime @default(now())
  updatedAt    DateTime @updatedAt
}

model ContentShowcase {
  id            String   @id @default(uuid())
  title         String
  description   String?  @db.Text
  youtubeUrl    String
  thumbnailUrl  String?
  redirectUrl   String?
  redirectLabel String?  @default("Learn More")
  orderIndex    Int      @default(0)
  isActive      Boolean  @default(true)
  createdAt     DateTime @default(now())
  updatedAt     DateTime @updatedAt
}
