// prisma/schema.prisma

datasource db {
  provider = "sqlite"
  url      = "file:./lcp.db"
  //url      = env("DATABASE_URL_SQLITE")
}

generator client {
  provider        = "prisma-client-js"
  previewFeatures = []
  output          = "../../node_modules/@prisma/sqlite/client"
}

model Status {
  id              Int     @id @default(autoincrement())
  name            String
  slug            String
  description     String?
  status_id       Int?    @default(1)
  transfer_status Int?    @default(1)

  users                        User[]
  permissions                  Permissions[]
  roles                        Role[]
  languages                    Language[]
  language_contents            LanguageContent[]
  menus                        Menus[]
  menuitems                    MenuItems[]
  master_entities              MasterEntity[]
  master_entity_line_items     MasterEntityLineItem[]
  field_types                  FieldType[]
  webservice_logs              WebserviceLog[]
  email_template_processes     EmailTemplateProcess[]
  email_template_tags          EmailTemplateTag[]
  email_template_receiver_tags EmailTemplateRecipientTag[]
  email_templates              EmailTemplate[]
  Designation                  Designation[]
  Department                   Department[]
  MenuType                     menuType[]

  @@unique([id])
  @@unique([slug])
  @@map("statuses")
}

model FieldType {
  id                   Int                    @id @default(autoincrement())
  field_type           String
  company_id           Int
  status_id            Int                    @default(1)
  created_at           String                 @default(dbgenerated("(datetime('now'))")) @map("created_at")
  status               Status                 @relation(fields: [status_id], references: [id], onDelete: Cascade)
  master_grid_lineitem MasterEntityLineItem[]
  transfer_status      Int?                   @default(1)

  @@unique([company_id, id])
  @@map("field_types")
}

model Role {
  id              Int       @id @default(autoincrement())
  uuid            String    @default(uuid())
  name            String
  description     String?
  status_id       Int       @default(1)
  status          Status    @relation(fields: [status_id], references: [id], onDelete: Cascade)
  company_id      Int
  created_by      Int?
  updated_by      Int?
  deleted_by      Int?
  created_at      String    @default(dbgenerated("(datetime('now'))")) @map("created_at")
  updated_at      DateTime? @updatedAt
  deleted_at      DateTime?
  transfer_status Int?      @default(1)

  permissions RolePermissions[]
  users       UserRoles[]
  created     User?             @relation("CreatedRole", fields: [company_id, created_by], references: [company_id, id], onDelete: Cascade)
  updated     User?             @relation("UpdatedRole", fields: [company_id, updated_by], references: [company_id, id], onDelete: Cascade)
  deleted     User?             @relation("DeletedRole", fields: [company_id, deleted_by], references: [company_id, id], onDelete: Cascade)

  @@unique([company_id, id])
  @@unique([company_id, uuid])
  @@map("roles")
}

model Department {
  id                 Int       @id @default(autoincrement())
  uuid               String    @default(uuid())
  code               String
  name               String
  description        String?
  status_id          Int       @default(1)
  status             Status    @relation(fields: [status_id], references: [id], onDelete: Cascade)
  department_head_id Int?
  company_id         Int
  created_at         String    @default(dbgenerated("(datetime('now'))")) @map("created_at")
  created_by         Int
  updated_at         DateTime? @updatedAt
  updated_by         Int?
  deleted_at         DateTime?
  deleted_by         Int?
  transfer_status    Int?      @default(1)

  // relationships
  users           UserDetail[] @relation("UserDepartment")
  department_head User?        @relation("DepartmentHead", fields: [company_id, department_head_id], references: [company_id, id], onDelete: Cascade)
  created         User?        @relation("CreatedDepartment", fields: [company_id, created_by], references: [company_id, id], onDelete: Cascade)
  updated         User?        @relation("UpdatedDepartment", fields: [company_id, updated_by], references: [company_id, id], onDelete: Cascade)
  deleted         User?        @relation("DeletedDepartment", fields: [company_id, deleted_by], references: [company_id, id], onDelete: Cascade)

  @@unique([company_id, id])
  @@unique([company_id, uuid])
  @@map("departments")
}

model Designation {
  id              Int       @id @default(autoincrement())
  uuid            String    @default(uuid())
  code            String
  name            String
  description     String?
  status_id       Int       @default(1)
  status          Status    @relation(fields: [status_id], references: [id], onDelete: Cascade)
  company_id      Int
  created_at      String    @default(dbgenerated("(datetime('now'))")) @map("created_at")
  created_by      Int
  updated_at      DateTime? @updatedAt
  updated_by      Int?
  deleted_at      DateTime?
  deleted_by      Int?
  transfer_status Int?      @default(1)

  // relationships
  users   UserDetail[] @relation("UserDesignation")
  created User?        @relation("CreatedDesignation", fields: [company_id, created_by], references: [company_id, id], onDelete: Cascade)
  updated User?        @relation("UpdatedDesignation", fields: [company_id, updated_by], references: [company_id, id], onDelete: Cascade)
  deleted User?        @relation("DeletedDesignation", fields: [company_id, deleted_by], references: [company_id, id], onDelete: Cascade)

  @@unique([company_id, id])
  @@unique([company_id, uuid])
  @@map("designations")
}

model User {
  id                Int       @id @default(autoincrement())
  uuid              String    @default(uuid())
  username          String
  email             String
  email_verified    Boolean   @default(false)
  password          String?
  status_id         Int       @default(1)
  role              String    @default("user")
  company_id        Int
  created_by        Int?
  updated_by        Int?
  deleted_by        Int?
  created_at        String    @default(dbgenerated("(datetime('now'))")) @map("created_at")
  updated_at        DateTime? @updatedAt
  deleted_at        DateTime?
  last_login        DateTime?
  last_login_device String?
  transfer_status   Int?      @default(1)

  status Status @relation(fields: [status_id], references: [id], onDelete: Cascade)

  permissions UserPermissions[]
  roles       UserRoles[]
  user_detail UserDetail?
  otps        Otp[]

  created User? @relation("CreatedUser", fields: [company_id, created_by], references: [company_id, id], onDelete: Cascade)
  updated User? @relation("UpdatedUser", fields: [company_id, updated_by], references: [company_id, id], onDelete: Cascade)
  deleted User? @relation("DeletedUser", fields: [company_id, deleted_by], references: [company_id, id], onDelete: Cascade)

  created_users User[] @relation("CreatedUser")
  updated_users User[] @relation("UpdatedUser")
  deleted_users User[] @relation("DeletedUser")

  created_user_details UserDetail[] @relation("CreatedUserDetail")
  updated_user_details UserDetail[] @relation("UpdatedUserDetail")
  deleted_user_details UserDetail[] @relation("DeletedUserDetail")
  created_roles        Role[]       @relation("CreatedRole")
  updated_roles        Role[]       @relation("UpdatedRole")
  deleted_roles        Role[]       @relation("DeletedRole")

  created_menus Menus[] @relation("CreatedMenus")
  updated_menus Menus[] @relation("UpdatedMenus")
  deleted_menus Menus[] @relation("DeletedMenus")

  created_menu_items MenuItems[] @relation("CreatedMenuItems")
  updated_menu_items MenuItems[] @relation("UpdatedMenuItems")
  deleted_menu_items MenuItems[] @relation("DeletedMenuItems")

  created_email_templates            EmailTemplate[]           @relation("CreatedEmailTemplate")
  updated_email_templates            EmailTemplate[]           @relation("UpdatedEmailTemplate")
  deleted_email_templates            EmailTemplate[]           @relation("DeletedEmailTemplate")
  created_email_template_assignments EmailTemplateAssignment[] @relation("CreatedEmailTemplateAssignment")

  created_languages Language[]      @relation("CreatedLanguage")
  updated_languages Language[]      @relation("UpdatedLanguage")
  deleted_languages Language[]      @relation("DeletedLanguage")
  web_service_logs  WebserviceLog[] @relation("WebServiceSalesMan")

  created_category_types CategoryTypes[] @relation("CreatedCategoryTypes")
  updated_category_types CategoryTypes[] @relation("UpdatedCategoryTypes")
  deleted_category_types CategoryTypes[] @relation("DeletedCategoryTypes")
  created_categories     Categories[]    @relation("CreatedCategories")
  updated_categories     Categories[]    @relation("UpdatedCategories")
  deleted_categories     Categories[]    @relation("DeletedCategories")

  created_configurations Configurations[] @relation("CreatedConfigurations")
  updated_configurations Configurations[] @relation("UpdatedConfigurations")
  deleted_configurations Configurations[] @relation("DeletedConfigurations")
  created_departments    Department[]     @relation("CreatedDepartment")
  head_departments       Department[]     @relation("DepartmentHead")
  updated_departments    Department[]     @relation("UpdatedDepartment")
  deleted_departments    Department[]     @relation("DeletedDepartment")
  created_designations   Designation[]    @relation("CreatedDesignation")
  updated_designations   Designation[]    @relation("UpdatedDesignation")
  deleted_designations   Designation[]    @relation("DeletedDesignation")

  @@unique([company_id, uuid])
  @@unique([company_id, email, role])
  @@unique([company_id, username])
  @@unique([company_id, id])
  @@map("users")
}

model UserDetail {
  id              Int       @id @default(autoincrement())
  user_id         Int
  code            String
  first_name      String
  last_name       String?
  designation_id  Int?
  department_id   Int?
  profile_pic     String?
  dob             DateTime?
  country_code    String?
  phone_number    String?
  gender          String?   @default("male")
  user_time_zone  String?
  address         String?
  culture         String?
  company_id      Int
  created_by      Int
  updated_by      Int?
  deleted_by      Int?
  created_at      String    @default(dbgenerated("(datetime('now'))")) @map("created_at")
  updated_at      DateTime? @updatedAt
  deleted_at      DateTime?
  transfer_status Int?      @default(1)

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

  designation Designation? @relation("UserDesignation", fields: [company_id, designation_id], references: [company_id, id], onDelete: Cascade)
  department  Department?  @relation("UserDepartment", fields: [company_id, department_id], references: [company_id, id], onDelete: Cascade)

  created User? @relation("CreatedUserDetail", fields: [company_id, created_by], references: [company_id, id], onDelete: Cascade)
  updated User? @relation("UpdatedUserDetail", fields: [company_id, updated_by], references: [company_id, id], onDelete: Cascade)
  deleted User? @relation("DeletedUserDetail", fields: [company_id, deleted_by], references: [company_id, id], onDelete: Cascade)

  @@unique([company_id, user_id])
  @@map("user_details")
}

model UserRoles {
  user            User @relation(fields: [company_id, user_id], references: [company_id, id], onDelete: Cascade)
  user_id         Int
  role            Role @relation(fields: [company_id, role_id], references: [company_id, id], onDelete: Cascade)
  role_id         Int
  company_id      Int
  transfer_status Int? @default(1)

  @@id([company_id, user_id, role_id])
  @@map("user_roles")
}

model CategoryTypes {
  id               Int     @id @default(autoincrement())
  uuid             String  @default(uuid())
  category_type_id String //@unique
  name             String
  description      String?
  transfer_status  Int?    @default(1)

  company_id Int
  created_at String    @default(dbgenerated("(datetime('now'))")) @map("created_at")
  created_by Int
  updated_at DateTime? @updatedAt
  updated_by Int?
  deleted_at DateTime?
  deleted_by Int?

  // relationships
  created_category_types User? @relation("CreatedCategoryTypes", fields: [company_id, created_by], references: [company_id, id], onDelete: Cascade)
  updated_category_types User? @relation("UpdatedCategoryTypes", fields: [company_id, updated_by], references: [company_id, id], onDelete: Cascade)
  deleted_category_types User? @relation("DeletedCategoryTypes", fields: [company_id, deleted_by], references: [company_id, id], onDelete: Cascade)

  categories Categories[]

  @@unique([company_id, category_type_id])
  @@unique([company_id, id])
  @@unique([company_id, name])
  @@map("app_category_types")
}

model Categories {
  id               Int     @id @default(autoincrement())
  uuid             String  @default(uuid())
  category_type_id String
  category_id      String  @unique
  parent_id        String?
  name             String
  description      String?

  company_id      Int
  created_at      String    @default(dbgenerated("(datetime('now'))")) @map("created_at")
  created_by      Int
  updated_at      DateTime? @updatedAt
  updated_by      Int?
  deleted_at      DateTime?
  deleted_by      Int?
  transfer_status Int?      @default(1)

  // Self-referencing relation using cat_id
  parent        Categories?  @relation("SubCategories", fields: [parent_id], references: [category_id])
  subcategories Categories[] @relation("SubCategories")

  // relationships
  category_type      CategoryTypes @relation(fields: [category_type_id, company_id], references: [category_type_id, company_id])
  created_categories User?         @relation("CreatedCategories", fields: [company_id, created_by], references: [company_id, id], onDelete: Cascade)
  updated_categories User?         @relation("UpdatedCategories", fields: [company_id, updated_by], references: [company_id, id], onDelete: Cascade)
  deleted_categories User?         @relation("DeletedCategories", fields: [company_id, deleted_by], references: [company_id, id], onDelete: Cascade)

  configurations Configurations[]

  @@unique([company_id, category_id])
  @@unique([company_id, id])
  @@unique([company_id, name])
  @@map("app_categories")
}

model Configurations {
  id                Int     @id @default(autoincrement())
  uuid              String  @default(uuid())
  order_no          Decimal @default(0)
  config_key        String
  category_id       String
  config_value      String?
  config_file_value String?
  config_value_type String
  config_field_type String
  display_config    Boolean @default(true)

  company_id      Int
  created_at      String    @default(dbgenerated("(datetime('now'))")) @map("created_at")
  created_by      Int
  updated_at      DateTime? @updatedAt
  updated_by      Int?
  deleted_at      DateTime?
  deleted_by      Int?
  transfer_status Int?      @default(1)

  // relationships
  category Categories @relation(fields: [category_id, company_id], references: [category_id, company_id])

  created_configurations User? @relation("CreatedConfigurations", fields: [company_id, created_by], references: [company_id, id], onDelete: Cascade)
  updated_configurations User? @relation("UpdatedConfigurations", fields: [company_id, updated_by], references: [company_id, id], onDelete: Cascade)
  deleted_configurations User? @relation("DeletedConfigurations", fields: [company_id, deleted_by], references: [company_id, id], onDelete: Cascade)

  @@unique([company_id, id])
  @@unique([company_id, config_key])
  @@map("app_configurations")
}

model menuType {
  id              Int     @id @default(autoincrement())
  name            String?
  status_id       Int     @default(1)
  status          Status  @relation(fields: [status_id], references: [id], onDelete: Cascade)
  company_id      Int
  created_at      String  @default(dbgenerated("(datetime('now'))")) @map("created_at")
  transfer_status Int?    @default(1)

  @@unique([company_id, id])
  @@map("menu_types")
}

model Menus {
  id              Int       @id @default(autoincrement())
  uuid            String    @default(uuid())
  name            String
  slug            String?
  description     String?
  menu_type       Int
  status_id       Int       @default(1)
  status          Status    @relation(fields: [status_id], references: [id], onDelete: Cascade)
  company_id      Int
  created_by      Int
  updated_by      Int?
  deleted_by      Int?
  created_at      String    @default(dbgenerated("(datetime('now'))")) @map("created_at")
  updated_at      DateTime? @updatedAt
  deleted_at      DateTime?
  transfer_status Int?      @default(1)

  //menuitems MenuItems[]

  created User? @relation("CreatedMenus", fields: [company_id, created_by], references: [company_id, id])
  updated User? @relation("UpdatedMenus", fields: [company_id, updated_by], references: [company_id, id])
  deleted User? @relation("DeletedMenus", fields: [company_id, deleted_by], references: [company_id, id])

  @@unique([company_id, id])
  @@unique([company_id, uuid])
  @@map("menu")
}

model WizardGroup {
  id              Int     @id @default(autoincrement())
  uuid            String  @default(uuid())
  name            String
  slug            String
  description     String?
  status_id       Int     @default(1)
  created_at      String  @default(dbgenerated("(datetime('now'))")) @map("created_at")
  company_id      Int
  transfer_status Int?    @default(1)

  @@unique([company_id, id])
  @@unique([company_id, slug])
  @@map("wizard_group")
}

model WizardType {
  id              Int    @id @default(autoincrement())
  uuid            String @default(uuid())
  name            String
  description     String
  company_id      Int
  transfer_status Int?   @default(1)

  @@unique([company_id, id])
  @@unique([company_id, name])
  @@map("wizard_types")
}

model MasterEntity {
  id                        Int       @id @default(autoincrement())
  uuid                      String    @default(uuid())
  name                      String    @unique
  entity_name               String
  entity_type               String
  primary_table             String?
  associated_tables         String?
  query_information         String?
  form_information          String?
  add_query_information     String?
  edit_query_information    String?
  preset_query_information  String?
  static_page_content       String?
  dashboard_wizard_group_id Int?
  dashboard_wizard_type     String?
  dashboard_wizard_rows     Int?
  dashboard_wizard_columns  Int?
  dashboard_wizard_order_no Decimal?  @default(0)
  dashboard_wizard_options  String?
  status_id                 Int       @default(1)
  company_id                Int
  created_at                String    @default(dbgenerated("(datetime('now'))")) @map("created_at")
  deleted_at                DateTime?
  transfer_status           Int?      @default(1)

  status      Status                 @relation(fields: [status_id], references: [id], onDelete: Cascade)
  permissions Permissions[]
  line_items  MasterEntityLineItem[]

  //MenuItems MenuItems[]

  @@unique([company_id, id])
  @@unique([company_id, entity_name])
  @@map("master_entities")
}

model MasterEntityLineItem {
  id              Int     @id @default(autoincrement())
  master_grid_id  Int
  field_name      String
  display_name    String
  width           Int?    @default(0)
  format          String?
  is_default      Boolean @default(true)
  order_no        Decimal @default(0)
  is_grid_column  Boolean @default(true)
  is_searchable   Boolean @default(true)
  is_sortable     Boolean @default(true)
  field_type_id   Int
  status_id       Int     @default(1)
  company_id      Int
  created_at      String  @default(dbgenerated("(datetime('now'))")) @map("created_at")
  transfer_status Int?    @default(1)

  status      Status       @relation(fields: [status_id], references: [id], onDelete: Cascade)
  master_grid MasterEntity @relation(fields: [company_id, master_grid_id], references: [company_id, id], onDelete: Cascade)
  field_type  FieldType    @relation(fields: [company_id, field_type_id], references: [company_id, id], onDelete: Cascade)

  @@unique([company_id, id])
  @@unique([company_id, master_grid_id, field_name, display_name])
  @@map("master_entity_line_items")
}

model Permissions {
  id              Int     @id @default(autoincrement())
  name            String
  slug            String
  entity_id       Int?
  order_no        Decimal @default(0)
  status_id       Int     @default(1)
  company_id      Int
  created_at      String  @default(dbgenerated("(datetime('now'))")) @map("created_at")
  transfer_status Int?    @default(1)

  status Status            @relation(fields: [status_id], references: [id], onDelete: Cascade)
  entity MasterEntity?     @relation(fields: [company_id, entity_id], references: [company_id, id])
  roles  RolePermissions[]
  users  UserPermissions[]
  //menus           MenuItems[]

  @@unique([company_id, slug])
  @@unique([company_id, id])
  @@index([company_id, order_no])
  @@map("permissions")
}

model MenuItems {
  id             Int     @id @default(autoincrement())
  menu_id        Int
  name           String
  target         String?
  parent_id      Int?
  permission_id  Int?
  entity_id      Int?
  link_type      Int?
  order_no       Decimal @default(0)
  menu_img       String?
  short_cut_keys String?
  status_id      Int     @default(1)
  status         Status  @relation(fields: [status_id], references: [id], onDelete: Cascade)
  company_id     Int
  created_by     Int?
  updated_by     Int?
  deleted_by     Int?

  created_at      String?   @default(dbgenerated("(datetime('now'))")) @map("created_at")
  updated_at      DateTime? @updatedAt
  deleted_at      DateTime?
  transfer_status Int?      @default(1)

  // relationships
  //menus      Menus?        @relation(fields: [company_id, menu_id], references: [company_id, id])
  //entity     MasterEntity? @relation(fields: [company_id, entity_id], references: [company_id, id])
  //permission Permissions?  @relation(fields: [company_id, permission_id], references: [company_id, id], onDelete: Cascade)
  //parent     MenuItems?    @relation("ChildMenuItemss", fields: [company_id, parent_id], references: [company_id, id])
  //children   MenuItems[]   @relation("ChildMenuItemss")
  created User? @relation("CreatedMenuItems", fields: [company_id, created_by], references: [company_id, id])
  updated User? @relation("UpdatedMenuItems", fields: [company_id, updated_by], references: [company_id, id])
  deleted User? @relation("DeletedMenuItems", fields: [company_id, deleted_by], references: [company_id, id])

  @@unique([company_id, id])
  @@map("menu_items")
}

model UserPermissions {
  user            User        @relation(fields: [company_id, user_id], references: [company_id, id], onDelete: Cascade)
  user_id         Int
  permission      Permissions @relation(fields: [company_id, permission_id], references: [company_id, id], onDelete: Cascade)
  permission_id   Int
  company_id      Int
  transfer_status Int?        @default(1)

  @@id([company_id, user_id, permission_id])
  @@map("user_permissions")
}

model ActionType {
  id              Int    @id @default(autoincrement())
  name            String
  description     String
  company_id      Int
  transfer_status Int?   @default(1)

  @@unique([company_id, id])
  @@map("action_types")
}

model EntityType {
  id              Int    @id @default(autoincrement())
  name            String
  slug            String
  description     String
  company_id      Int
  transfer_status Int?   @default(1)

  @@unique([company_id, id])
  @@map("entity_types")
}

model Otp {
  id              Int    @id @default(autoincrement())
  user_id         Int
  otp             String
  purpose         String
  company_id      Int
  created_at      String @default(dbgenerated("(datetime('now'))")) @map("created_at")
  transfer_status Int?   @default(1)

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

  @@index([company_id, user_id, purpose])
  @@map("otps")
}

model Language {
  id              Int       @id @default(autoincrement())
  uuid            String    @default(uuid())
  name            String
  name_in_english String?
  flag            String?
  code            String?
  rtl             Boolean   @default(false)
  description     String
  status_id       Int       @default(1)
  status          Status    @relation(fields: [status_id], references: [id], onDelete: Cascade)
  company_id      Int
  created_by      Int
  updated_by      Int?
  deleted_by      Int?
  created_at      String    @default(dbgenerated("(datetime('now'))")) @map("created_at")
  updated_at      DateTime? @updatedAt
  deleted_at      DateTime?
  transfer_status Int?      @default(1)

  // relationships
  translations LanguageContent[] @relation("LanguageTrans")

  created User? @relation("CreatedLanguage", fields: [company_id, created_by], references: [company_id, id], onDelete: Cascade)
  updated User? @relation("UpdatedLanguage", fields: [company_id, updated_by], references: [company_id, id], onDelete: Cascade)
  deleted User? @relation("DeletedLanguage", fields: [company_id, deleted_by], references: [company_id, id], onDelete: Cascade)

  @@unique([company_id, id])
  @@unique([company_id, uuid])
  @@unique([company_id, code])
  @@map("languages")
}

model LanguageContent {
  id              Int    @id @default(autoincrement())
  language_id     Int    @default(1)
  key_content     String
  values          String
  status_id       Int    @default(1)
  status          Status @relation(fields: [status_id], references: [id], onDelete: Cascade)
  company_id      Int
  created_at      String @default(dbgenerated("(datetime('now'))")) @map("created_at")
  transfer_status Int?   @default(1)

  // relationships
  language Language? @relation("LanguageTrans", fields: [company_id, language_id], references: [company_id, id], onDelete: Cascade)

  @@unique([company_id, id])
  @@map("language_contents")
}

model WebserviceLog {
  id          BigInt @id @default(autoincrement())
  salesman_id Int?

  req_method String?

  service_type           String?
  status_id              Int     @default(1)
  status                 Status  @relation(fields: [status_id], references: [id], onDelete: Cascade)
  app_version            String?
  parameters             String?
  time_taken_to_complete Int?
  response               String?
  device_imei            String?
  company_id             Int
  created_at             String  @default(dbgenerated("(datetime('now'))")) @map("created_at")
  transfer_status        Int?    @default(1)

  salesman User? @relation("WebServiceSalesMan", fields: [company_id, salesman_id], references: [company_id, id], onDelete: Cascade)

  @@unique([company_id, id])
  @@map("webservice_logs")
}

model RolePermissions {
  role            Role        @relation(fields: [company_id, role_id], references: [company_id, id], onDelete: Cascade)
  role_id         Int
  permission      Permissions @relation(fields: [company_id, permission_id], references: [company_id, id], onDelete: Cascade)
  permission_id   Int
  company_id      Int
  transfer_status Int?        @default(1)

  @@id([company_id, role_id, permission_id])
  @@map("role_permissions")
}

model EmailTemplate {
  id                         Int                       @id @default(autoincrement())
  uuid                       String                    @default(uuid())
  slug                       String
  name                       String
  subject                    String
  template_body              String
  email_template_process_id  Int
  status_id                  Int                       @default(1)
  status                     Status                    @relation(fields: [status_id], references: [id], onDelete: Cascade)
  created_at                 String                    @default(dbgenerated("(datetime('now'))")) @map("created_at")
  created_by                 Int
  updated_at                 DateTime?                 @updatedAt
  updated_by                 Int?
  deleted_at                 DateTime?
  deleted_by                 Int?
  company_id                 Int
  transfer_status            Int?                      @default(1)
  email_template_process     EmailTemplateProcess      @relation(fields: [email_template_process_id, company_id], references: [id, company_id])
  email_template_assignments EmailTemplateAssignment[] @relation("EmailTemplateAssignment")

  created User  @relation("CreatedEmailTemplate", fields: [company_id, created_by], references: [company_id, id], onDelete: Cascade)
  updated User? @relation("UpdatedEmailTemplate", fields: [company_id, updated_by], references: [company_id, id], onDelete: Cascade)
  deleted User? @relation("DeletedEmailTemplate", fields: [company_id, deleted_by], references: [company_id, id], onDelete: Cascade)

  @@unique([company_id, id])
  @@unique([company_id, slug])
  @@map("email_templates")
}

model EmailTemplateTag {
  id                    Int                               @id @default(autoincrement())
  uuid                  String                            @default(uuid())
  slug                  String
  name                  String?
  description           String?
  status_id             Int                               @default(1)
  status                Status                            @relation(fields: [status_id], references: [id], onDelete: Cascade)
  company_id            Int
  email_template_groups EmailTemplateProcessTagsMapping[]
  transfer_status       Int?                              @default(1)

  @@unique([company_id, id])
  @@unique([company_id, slug])
  @@map("email_template_tags")
}

model EmailTemplateProcess {
  id                         Int                               @id @default(autoincrement())
  uuid                       String                            @default(uuid())
  name                       String
  slug                       String
  status_id                  Int                               @default(1)
  status                     Status                            @relation(fields: [status_id], references: [id], onDelete: Cascade)
  company_id                 Int
  email_template_groups      EmailTemplateProcessTagsMapping[]
  email_templates            EmailTemplate[]
  email_template_assignments EmailTemplateAssignment[]
  transfer_status            Int?                              @default(1)

  @@unique([company_id, id])
  @@unique([company_id, slug])
  @@map("email_template_process")
}

model EmailTemplateProcessTagsMapping {
  email_template_process_id Int
  email_template_tag_id     Int
  company_id                Int
  email_template_process    EmailTemplateProcess @relation(fields: [email_template_process_id, company_id], references: [id, company_id])
  email_template_tag        EmailTemplateTag     @relation(fields: [email_template_tag_id, company_id], references: [id, company_id])
  transfer_status           Int?                 @default(1)

  @@id([email_template_process_id, email_template_tag_id, company_id])
  @@map("email_template_process_tags_mapping")
}

model EmailTemplateAssignment {
  id                        Int     @id @default(autoincrement())
  uuid                      String  @default(uuid())
  email_template_process_id Int
  template_id               Int
  recipient_type            String  @default("tag")
  email_to                  String?
  company_id                Int
  created_at                String  @default(dbgenerated("(datetime('now'))")) @map("created_at")
  created_by                Int
  transfer_status           Int?    @default(1)

  email_template_process EmailTemplateProcess @relation(fields: [email_template_process_id, company_id], references: [id, company_id])
  email_template         EmailTemplate        @relation("EmailTemplateAssignment", fields: [template_id, company_id], references: [id, company_id])
  created                User                 @relation("CreatedEmailTemplateAssignment", fields: [company_id, created_by], references: [company_id, id], onDelete: Cascade)

  @@map("email_template_assignment")
}

model EmailTemplateRecipientTag {
  id              Int     @id @default(autoincrement())
  uuid            String  @default(uuid())
  slug            String
  name            String?
  description     String?
  status_id       Int     @default(1)
  status          Status  @relation(fields: [status_id], references: [id], onDelete: Cascade)
  company_id      Int
  transfer_status Int?    @default(1)

  @@unique([company_id, id])
  @@unique([company_id, slug])
  @@map("email_template_recipient_tags")
}
