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

generator jsonSchema {
  provider = "prisma-json-schema-generator"
  output   = "./json"
}

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

model user {
  userID                Int              @id @default(autoincrement())
  userSecretKey         String?          @db.VarChar(255)
  userUsername          String?          @db.VarChar(255)
  userPassword          String?          @db.VarChar(255)
  userFullName          String?          @db.VarChar(255)
  userEmail             String?          @db.VarChar(255)
  userPhone             String?          @db.VarChar(255)
  userStatus            String?          @db.VarChar(255)
  userCreatedDate       DateTime?        @db.DateTime(0)
  userModifiedDate      DateTime?        @db.DateTime(0)
  caseInstance          caseInstance[]
  caseTimeline          caseTimeline[]
  forms                 form[]           @relation("FormCreator")
  formHistoryEntries    formHistory[]
  processes             process[]        @relation("ProcessCreator")
  processHistoryEntries processHistory[]
  task                  task[]
  userrole              userrole[]
  startedCases          caseInstance[]   @relation("CaseStartedBy")
  assignedTasks         task[]           @relation("TaskAssignedTo")
  caseTimelineEntries   caseTimeline[]
}

model role {
  roleID           Int        @id @default(autoincrement())
  roleName         String?    @db.VarChar(255)
  roleDescription  String?    @db.VarChar(255)
  roleStatus       String?    @db.VarChar(255)
  roleCreatedDate  DateTime?  @db.DateTime(0)
  roleModifiedDate DateTime?  @db.DateTime(0)
  userrole         userrole[]
}

model userrole {
  userRoleID          Int      @id @default(autoincrement())
  userRoleUserID      Int      @default(0)
  userRoleRoleID      Int      @default(0)
  userRoleCreatedDate DateTime @db.DateTime(0)
  role                role     @relation(fields: [userRoleRoleID], references: [roleID], onDelete: NoAction, onUpdate: NoAction, map: "FK_userrole_role")
  user                user     @relation(fields: [userRoleUserID], references: [userID], onDelete: NoAction, onUpdate: NoAction, map: "FK_userrole_user")

  @@index([userRoleRoleID], map: "FK_userrole_role")
  @@index([userRoleUserID], map: "FK_userrole_user")
}

model form {
  formID           Int           @id @default(autoincrement())
  formUUID         String        @unique @db.VarChar(36)
  formName         String        @db.VarChar(255)
  formDescription  String?       @db.Text
  formComponents   Json
  formStatus       String        @default("active") @db.VarChar(50)
  formCreatedBy    Int?
  formCreatedDate  DateTime      @default(now()) @db.DateTime(0)
  formModifiedDate DateTime?     @updatedAt @db.DateTime(0)
  customCSS        String?       @db.Text
  customScript     String?       @db.LongText
  formEvents       Json?
  scriptMode       String?       @default("safe") @db.VarChar(20)
  creator          user?         @relation("FormCreator", fields: [formCreatedBy], references: [userID])
  history          formHistory[] @relation("FormHistoryEntries")
  tasks            task[]

  @@index([formCreatedBy], map: "FK_form_creator")
}

model formHistory {
  historyID         Int      @id @default(autoincrement())
  formID            Int
  formUUID          String   @db.VarChar(36)
  formName          String   @db.VarChar(255)
  formDescription   String?  @db.Text
  formComponents    Json
  formStatus        String   @db.VarChar(50)
  customCSS         String?  @db.Text
  customScript      String?  @db.LongText
  formEvents        Json?
  scriptMode        String?  @db.VarChar(20)
  versionNumber     Int
  changeDescription String?  @db.Text
  savedBy           Int?
  savedDate         DateTime @default(now()) @db.DateTime(0)
  form              form     @relation("FormHistoryEntries", fields: [formID], references: [formID], onDelete: Cascade)
  savedByUser       user?    @relation(fields: [savedBy], references: [userID])

  @@index([formID], map: "FK_formHistory_form")
  @@index([savedBy], map: "FK_formHistory_savedBy")
  @@index([formUUID], map: "IDX_formHistory_uuid")
  @@index([savedDate], map: "IDX_formHistory_date")
}

model process {
  processID           Int              @id @default(autoincrement())
  processUUID         String           @unique @db.VarChar(36)
  processName         String           @db.VarChar(255)
  processDescription  String?          @db.Text
  processDefinition   Json
  processVersion      Int              @default(1)
  processStatus       String           @default("draft") @db.VarChar(50)
  processCreatedBy    Int?
  processCreatedDate  DateTime         @default(now()) @db.DateTime(0)
  processModifiedDate DateTime?        @updatedAt @db.DateTime(0)
  isTemplate          Boolean          @default(false)
  processCategory     String?          @db.VarChar(100)
  processOwner        String?          @db.VarChar(255)
  processPermissions  Json?
  processPriority     String?          @default("normal") @db.VarChar(50)
  processSettings     Json?
  processVariables    Json?
  templateCategory    String?          @db.VarChar(100)
  processDeletedDate  DateTime?        @db.DateTime(0)
  caseInstance        caseInstance[]
  creator             user?            @relation("ProcessCreator", fields: [processCreatedBy], references: [userID])
  history             processHistory[] @relation("ProcessHistoryEntries")
  cases               caseInstance[]

  @@index([processCreatedBy], map: "FK_process_creator")
  @@index([processStatus], map: "IDX_process_status")
  @@index([processCategory], map: "IDX_process_category")
  @@index([isTemplate], map: "IDX_process_template")
}

model processHistory {
  historyID          Int      @id @default(autoincrement())
  processID          Int
  processUUID        String   @db.VarChar(36)
  processName        String   @db.VarChar(255)
  processDescription String?  @db.Text
  processDefinition  Json
  processVersion     Int
  processStatus      String   @db.VarChar(50)
  processCategory    String?  @db.VarChar(100)
  processOwner       String?  @db.VarChar(255)
  processPermissions Json?
  processPriority    String?  @db.VarChar(50)
  processSettings    Json?
  processVariables   Json?
  templateCategory   String?  @db.VarChar(100)
  versionNumber      Int
  changeDescription  String?  @db.Text
  savedBy            Int?
  savedDate          DateTime @default(now()) @db.DateTime(0)
  process            process  @relation("ProcessHistoryEntries", fields: [processID], references: [processID], onDelete: Cascade)
  savedByUser        user?    @relation(fields: [savedBy], references: [userID])

  @@index([processID], map: "FK_processHistory_process")
  @@index([savedBy], map: "FK_processHistory_savedBy")
  @@index([processUUID], map: "IDX_processHistory_uuid")
  @@index([savedDate], map: "IDX_processHistory_date")
}

model caseInstance {
  caseID           Int           @id @default(autoincrement())
  caseUUID         String        @unique @db.VarChar(36)
  processID        Int
  caseName         String        @db.VarChar(255)
  caseStatus       String        @default("active") @db.VarChar(50)
  caseStartedBy    Int?
  caseVariables    Json?
  caseSettings     Json?
  caseDefinition   Json?
  caseCreatedDate  DateTime      @default(now()) @db.DateTime(0)
  caseModifiedDate DateTime?     @updatedAt @db.DateTime(0)
  caseCompletedDate DateTime?    @db.DateTime(0)
  process          process       @relation(fields: [processID], references: [processID])
  startedBy        user?        @relation("CaseStartedBy", fields: [caseStartedBy], references: [userID])
  tasks            task[]
  timeline         caseTimeline[]

  @@index([processID], map: "FK_case_process")
  @@index([caseStartedBy], map: "FK_case_startedBy")
  @@index([caseStatus], map: "IDX_case_status")
}

model task {
  taskID           Int           @id @default(autoincrement())
  taskUUID         String        @unique @db.VarChar(36)
  caseID           Int
  taskName         String        @db.VarChar(255)
  taskType         String        @db.VarChar(50)
  taskStatus       String        @default("pending") @db.VarChar(50)
  taskAssignedTo   Int?
  taskFormID       Int?
  taskData         Json?
  taskCreatedDate  DateTime      @default(now()) @db.DateTime(0)
  taskModifiedDate DateTime?     @updatedAt @db.DateTime(0)
  taskCompletedDate DateTime?    @db.DateTime(0)
  case             caseInstance  @relation(fields: [caseID], references: [caseID])
  assignedTo       user?        @relation("TaskAssignedTo", fields: [taskAssignedTo], references: [userID])
  form             form?        @relation(fields: [taskFormID], references: [formID])

  @@index([caseID], map: "FK_task_case")
  @@index([taskAssignedTo], map: "FK_task_assignedTo")
  @@index([taskFormID], map: "FK_task_form")
  @@index([taskStatus], map: "IDX_task_status")
}

model caseTimeline {
  timelineID          Int      @id @default(autoincrement())
  caseID              Int
  timelineType        String   @db.VarChar(50)
  timelineDescription String?  @db.Text
  timelineDate        DateTime @default(now()) @db.DateTime(0)
  timelineCreatedBy   Int?
  case                caseInstance @relation(fields: [caseID], references: [caseID])
  createdBy           user?    @relation(fields: [timelineCreatedBy], references: [userID])

  @@index([caseID], map: "FK_caseTimeline_case")
  @@index([timelineCreatedBy], map: "FK_caseTimeline_createdBy")
  @@index([timelineDate], map: "IDX_caseTimeline_date")
}
