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

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

model Attendance {
  id           String    @id @default(uuid())
  userId       String
  date         DateTime  @default(now())
  checkInTime  DateTime  @default(now())
  checkOutTime DateTime?
  checkInLat   Float?
  checkInLng   Float?
  checkInLoc   String?
  checkOutLat  Float?
  checkOutLng  Float?
  checkOutLoc  String?
  user         User      @relation(fields: [userId], references: [id], map: "Attendance_userId_fkey")

  @@index([userId], map: "Attendance_userId_fkey")
  @@map("attendance")
}

model Client {
  id                       String        @id @default(uuid())
  name                     String
  phone                    String
  email                    String?
  address                  String?
  lat                      Float?
  lng                      Float?
  landmark                 String?
  status                   client_status @default(LEAD)
  assignedTo               String?
  createdAt                DateTime      @default(now())
  updatedAt                DateTime      @updatedAt
  companyName              String?
  contactName              String?
  closingProbability       Int?          @default(0)
  expectedClosingTimeframe String?
  isDemoDone               Boolean       @default(false)
  user                     User?         @relation(fields: [assignedTo], references: [id], map: "Client_assignedTo_fkey")
  files                    ClientFile[]
  enquiries                Enquiry[]
  followups                Followup[]
  meetings                 Meeting[]
  opportunities            Opportunity[]

  @@index([assignedTo], map: "Client_assignedTo_fkey")
  @@map("client")
}

model ClientFile {
  id        String   @id @default(uuid())
  clientId  String
  name      String
  url       String
  size      Int?
  type      String?
  createdAt DateTime @default(now())
  client    Client   @relation(fields: [clientId], references: [id], onDelete: Cascade)

  @@index([clientId])
  @@map("client_file")
}

model Enquiry {
  id           String     @id @default(uuid())
  clientId     String
  userId       String
  conversation String?    @db.Text
  status       String     @default("OPEN")
  createdAt    DateTime   @default(now())
  updatedAt    DateTime   @updatedAt
  client       Client     @relation(fields: [clientId], references: [id], map: "Enquiry_clientId_fkey")
  user         User       @relation(fields: [userId], references: [id], map: "Enquiry_userId_fkey")
  followups    Followup[]
  quotes       Quote[]
  products     Product[]  @relation("enquiry_products")

  @@index([clientId], map: "Enquiry_clientId_fkey")
  @@index([userId], map: "Enquiry_userId_fkey")
  @@map("enquiry")
}

model Expense {
  id          String         @id @default(uuid())
  userId      String
  amount      Float
  description String
  date        DateTime       @default(now())
  imageUrl    String?
  status      expense_status @default(PENDING)
  createdAt   DateTime       @default(now())
  updatedAt   DateTime       @updatedAt
  user        User           @relation(fields: [userId], references: [id], map: "Expense_userId_fkey")

  @@index([userId], map: "Expense_userId_fkey")
  @@map("expense")
}

model Followup {
  id                 String         @id @default(uuid())
  clientId           String
  enquiryId          String?
  userId             String
  date               DateTime
  notes              String?
  status             String         @default("PENDING")
  createdAt          DateTime       @default(now())
  updatedAt          DateTime       @updatedAt
  contentType        String?
  stage              followup_stage @default(LEAD)
  competitorMention  String?
  demoContactDetails String?
  demoPersonName     String?
  keyQueries         String?
  objections         String?
  opportunityId      String?
  type               activity_type  @default(FOLLOWUP)
  client             Client         @relation(fields: [clientId], references: [id], map: "Followup_clientId_fkey")
  enquiry            Enquiry?       @relation(fields: [enquiryId], references: [id], map: "Followup_enquiryId_fkey")
  opportunity        Opportunity?   @relation(fields: [opportunityId], references: [id], map: "Followup_opportunityId_fkey")
  user               User           @relation(fields: [userId], references: [id], map: "Followup_userId_fkey")

  @@index([clientId], map: "Followup_clientId_fkey")
  @@index([enquiryId], map: "Followup_enquiryId_fkey")
  @@index([opportunityId], map: "Followup_opportunityId_fkey")
  @@index([userId], map: "Followup_userId_fkey")
  @@map("followup")
}

model Incentive {
  id             String         @id @default(uuid())
  userId         String
  targetAmount   Float
  achievedAmount Float          @default(0)
  rewardAmount   Float?
  type           incentive_type
  startDate      DateTime
  endDate        DateTime
  createdAt      DateTime       @default(now())
  updatedAt      DateTime       @updatedAt
  user           User           @relation(fields: [userId], references: [id], map: "Incentive_userId_fkey")

  @@index([userId], map: "Incentive_userId_fkey")
  @@map("incentive")
}

model Location {
  id        String   @id @default(uuid())
  userId    String
  lat       Float
  lng       Float
  timestamp DateTime @default(now())
  user      User     @relation(fields: [userId], references: [id], map: "Location_userId_fkey")

  @@index([userId], map: "Location_userId_fkey")
  @@map("location")
}

model Meeting {
  id          String   @id @default(uuid())
  title       String
  description String?
  date        DateTime
  clientId    String
  createdBy   String
  createdAt   DateTime @default(now())
  client      Client   @relation(fields: [clientId], references: [id], map: "Meeting_clientId_fkey")
  user        User     @relation(fields: [createdBy], references: [id], map: "Meeting_createdBy_fkey")

  @@index([clientId], map: "Meeting_clientId_fkey")
  @@index([createdBy], map: "Meeting_createdBy_fkey")
  @@map("meeting")
}

model Opportunity {
  id                 String            @id @default(uuid())
  title              String
  value              Float
  stage              opportunity_stage @default(LEAD)
  priority           String?
  clientId           String
  assignedTo         String
  expectedCloseDate  DateTime?
  createdAt          DateTime          @default(now())
  updatedAt          DateTime          @updatedAt
  competitorMention  String?
  demoContactDetails String?
  demoPersonName     String?
  freeOffers         String?
  keyQueries         String?
  negotiationRemarks String?
  objections         String?
  paymentMode        String?
  specialRate        Float?
  creatorId          String?
  closingOwnerId     String?
  demoOwnerId        String?
  isDemoDone         Boolean           @default(false)
  activities         Followup[]
  user               User              @relation("opportunity_assignedToTouser", fields: [assignedTo], references: [id], map: "Opportunity_assignedTo_fkey")
  client             Client            @relation(fields: [clientId], references: [id], map: "Opportunity_clientId_fkey")
  closingOwner       User?             @relation("opportunity_closingOwnerIdTouser", fields: [closingOwnerId], references: [id], map: "Opportunity_closingOwnerId_fkey")
  creator            User?             @relation("opportunity_creatorIdTouser", fields: [creatorId], references: [id], map: "Opportunity_creatorId_fkey")
  demoOwner          User?             @relation("opportunity_demoOwnerIdTouser", fields: [demoOwnerId], references: [id], map: "Opportunity_demoOwnerId_fkey")
  quotes             Quote[]
  workOrders         WorkOrder[]

  @@index([assignedTo], map: "Opportunity_assignedTo_fkey")
  @@index([clientId], map: "Opportunity_clientId_fkey")
  @@index([creatorId], map: "Opportunity_creatorId_fkey")
  @@index([demoOwnerId], map: "Opportunity_demoOwnerId_fkey")
  @@index([closingOwnerId], map: "Opportunity_closingOwnerId_fkey")
  @@map("opportunity")
}

model Payment {
  id          String    @id @default(uuid())
  workOrderId String
  amount      Float
  mode        String?
  status      String    @default("PENDING")
  paymentLink String?
  createdAt   DateTime  @default(now())
  updatedAt   DateTime  @updatedAt
  workOrder   WorkOrder @relation(fields: [workOrderId], references: [id], map: "Payment_workOrderId_fkey")

  @@index([workOrderId], map: "Payment_workOrderId_fkey")
  @@map("payment")
}

model PerformanceScore {
  id               String   @id @default(uuid())
  userId           String
  score            Float    @default(0)
  revenueScore     Float    @default(0)
  conversionScore  Float    @default(0)
  activityScore    Float    @default(0)
  disciplineScore  Float    @default(0)
  dataQualityScore Float    @default(0)
  tag              String   @default("ON_TRACK")
  date             DateTime @default(now())
  user             User     @relation(fields: [userId], references: [id], map: "PerformanceScore_userId_fkey")

  @@index([userId], map: "PerformanceScore_userId_fkey")
  @@map("performancescore")
}

model Product {
  id          String    @id @default(uuid())
  name        String
  description String?
  price       Float
  imageUrl    String?
  createdAt   DateTime  @default(now())
  updatedAt   DateTime  @updatedAt
  enquiries   Enquiry[] @relation("enquiry_products")

  @@map("product")
}

model Quote {
  id            String       @id @default(uuid())
  enquiryId     String?
  userId        String
  items         String       @db.LongText
  totalAmount   Float
  status        quote_status @default(DRAFT)
  pdfUrl        String?
  createdAt     DateTime     @default(now())
  updatedAt     DateTime     @updatedAt
  opportunityId String?
  enquiry       Enquiry?     @relation(fields: [enquiryId], references: [id], map: "Quote_enquiryId_fkey")
  opportunity   Opportunity? @relation(fields: [opportunityId], references: [id], map: "Quote_opportunityId_fkey")
  user          User         @relation(fields: [userId], references: [id], map: "Quote_userId_fkey")

  @@index([enquiryId], map: "Quote_enquiryId_fkey")
  @@index([opportunityId], map: "Quote_opportunityId_fkey")
  @@index([userId], map: "Quote_userId_fkey")
  @@map("quote")
}

model StrategicActivity {
  id               String   @id @default(uuid())
  userId           String
  type             String
  description      String?
  leadsGenerated   Int      @default(0)
  revenueGenerated Float    @default(0)
  date             DateTime @default(now())
  createdAt        DateTime @default(now())
  updatedAt        DateTime @updatedAt
  metadata         String?  @db.LongText
  user             User     @relation(fields: [userId], references: [id], map: "StrategicActivity_userId_fkey")

  @@index([userId], map: "StrategicActivity_userId_fkey")
  @@map("strategicactivity")
}

model Target {
  id                   String   @id @default(uuid())
  userId               String
  month                Int
  year                 Int
  monthlyTarget        Float
  minTarget            Float
  weeklyTarget         Float?
  dailyLeadTarget      Int?
  createdAt            DateTime @default(now())
  updatedAt            DateTime @updatedAt
  avgDealValue         Float?
  closureRatio         Float?
  requiredClosures     Int?
  requiredDemos        Int?
  requiredLeads        Int?
  requiredPotential    Int?
  requiredQualityLeads Int?
  user                 User     @relation(fields: [userId], references: [id], map: "Target_userId_fkey")

  @@index([userId], map: "Target_userId_fkey")
  @@map("target")
}

model Notification {
  id        String   @id @default(uuid())
  userId    String
  title     String
  body      String   @db.Text
  type      String   @default("INFO")
  isRead    Boolean  @default(false)
  metadata  String?  @db.LongText
  createdAt DateTime @default(now())
  user      User     @relation(fields: [userId], references: [id], map: "Notification_userId_fkey")

  @@index([userId], map: "Notification_userId_fkey")
  @@map("notification")
}

model SystemConfig {
  id        String   @id @default(uuid())
  key       String   @unique
  value     String   @db.Text
  updatedAt DateTime @updatedAt

  @@map("systemconfig")
}

model User {
  id                   String              @id @default(uuid())
  email                String              @unique(map: "User_email_key")
  password             String
  name                 String?
  role                 user_role           @default(TELESALES_EXECUTIVE)
  createdAt            DateTime            @default(now())
  updatedAt            DateTime            @updatedAt
  managerId            String?
  status               user_status         @default(APPROVED)
  attendance           Attendance[]
  clients              Client[]
  enquiries            Enquiry[]
  expenses             Expense[]
  followups            Followup[]
  incentives           Incentive[]
  locations            Location[]
  meetings             Meeting[]
  notifications        Notification[]
  opportunities        Opportunity[]       @relation("opportunity_assignedToTouser")
  closedOpportunities  Opportunity[]       @relation("opportunity_closingOwnerIdTouser")
  createdOpportunities Opportunity[]       @relation("opportunity_creatorIdTouser")
  demoOpportunities    Opportunity[]       @relation("opportunity_demoOwnerIdTouser")
  performanceScores    PerformanceScore[]
  quotes               Quote[]
  strategicActivities  StrategicActivity[]
  targets              Target[]
  manager              User?               @relation("userTouser", fields: [managerId], references: [id], map: "User_managerId_fkey")
  subordinates         User[]              @relation("userTouser")

  @@index([managerId], map: "User_managerId_fkey")
  @@map("user")
}

model WorkOrder {
  id            String      @id @default(uuid())
  opportunityId String
  status        String      @default("PENDING")
  details       String?
  createdAt     DateTime    @default(now())
  updatedAt     DateTime    @updatedAt
  payments      Payment[]
  opportunity   Opportunity @relation(fields: [opportunityId], references: [id], map: "WorkOrder_opportunityId_fkey")

  @@index([opportunityId], map: "WorkOrder_opportunityId_fkey")
  @@map("workorder")
}

model WhatsappTemplate {
  id           String   @id @default(uuid())
  type         String   @unique
  templateName String
  language     String   @default("en")
  body         String?  @db.Text
  createdAt    DateTime @default(now())
  updatedAt    DateTime @updatedAt

  @@map("whatsapp_template")
}

model WhatsappLog {
  id           String   @id @default(uuid())
  templateName String
  payload      String?  @db.Text
  response     String?  @db.Text
  status       String   @default("SENT")
  createdAt    DateTime @default(now())
  recipient    String

  @@map("whatsapp_log")
}

enum opportunity_stage {
  LEAD
  QUALIFIED
  POTENTIAL
  SALES
  LOST
}

enum user_role {
  ADMIN
  GENERAL_MANAGER
  MANAGER
  OFFICER
  TELESALES_EXECUTIVE
}

enum user_status {
  PENDING
  APPROVED
  REJECTED
}

enum incentive_type {
  DAILY
  MONTHLY
}

enum quote_status {
  DRAFT
  SENT
  ACCEPTED
  REJECTED
}

enum expense_status {
  PENDING
  APPROVED
  REJECTED
  REIMBURSED
}

enum client_status {
  LEAD
  QUALIFIED
  POTENTIAL
  SALES
  CLOSED
}

enum followup_stage {
  LEAD
  QUALIFIED
  POTENTIAL
  SALES
  CLOSED
}

enum activity_type {
  CALL
  MESSAGE
  DEMO_SCHEDULED
  DEMO_COMPLETED
  QUOTE_REQUEST
  QUOTE_SEND
  VISIT_SCHEDULED
  VISIT_COMPLETED
  NEGOTIATION
  FOLLOWUP
  DEMO
  QUOTE
}


