generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

// ============================================================
// USERS (Admin + Employee)
// ============================================================
model User {
  id              String      @id @default(cuid())
  email           String      @unique
  password        String
  role            String      @default("EMPLOYEE")
  name            String?
  phone           String?
  lastLogin       DateTime?
  failedAttempts  Int         @default(0)
  lockedUntil     DateTime?
  passwordHistory String[]    @default([])
  active          Boolean     @default(true)
  createdAt       DateTime    @default(now())
  updatedAt       DateTime    @updatedAt
  logs            Log[]
  quotations      Quotation[]
}

// ============================================================
// PRODUCTS
// ============================================================
model Product {
  id              String   @id @default(cuid())
  name            String
  description     String?
  longDescription String?
  price           Float
  vat             Boolean  @default(false)
  category        String?
  subCategory     String?
  stock           Int      @default(0)
  status          String   @default("PENDING")
  imageUrl        String?
  createdBy       String?
  createdAt       DateTime @default(now())
  updatedAt       DateTime @updatedAt
}

// ============================================================
// QUOTATIONS
// ============================================================
model Quotation {
  id            String   @id @default(cuid())
  serialNumber  String   @unique
  customerName  String?
  customerEmail String?
  customerPhone String?
  items         Json
  subtotal      Float
  vatAmount     Float
  total         Float
  status        String   @default("DRAFT")
  createdBy     String?
  createdByRole String?
  user          User?    @relation(fields: [createdBy], references: [id])
  createdAt     DateTime @default(now())
  updatedAt     DateTime @updatedAt
}

// ============================================================
// CUSTOMER SHOPPING LINKS (24-hour validity)
// ============================================================
model CustomerLink {
  id        String   @id @default(cuid())
  token     String   @unique
  createdBy String
  expiresAt DateTime
  active    Boolean  @default(true)
  createdAt DateTime @default(now())
}

// ============================================================
// EMPLOYEE REGISTRATION INVITES (7-day validity)
// ============================================================
model EmployeeInvite {
  id        String   @id @default(cuid())
  token     String   @unique
  createdBy String
  usedBy    String?
  expiresAt DateTime
  active    Boolean  @default(true)
  createdAt DateTime @default(now())
}

// ============================================================
// CUSTOMER SESSIONS (email verification + 6-hour session)
// ============================================================
model CustomerSession {
  id        String   @id @default(cuid())
  email     String
  code      String
  linkToken String
  expiresAt DateTime
  verified  Boolean  @default(false)
  createdAt DateTime @default(now())
}

// ============================================================
// REGISTERED CUSTOMERS (so admin can manage them)
// ============================================================
model Customer {
  id         String   @id @default(cuid())
  email      String   @unique
  name       String?
  phone      String?
  lastSeenAt DateTime @default(now())
  createdAt  DateTime @default(now())
}

// ============================================================
// PAYMENTS (M-Pesa STK Push)
// ============================================================
model Payment {
  id            String   @id @default(cuid())
  quotationId   String?
  customerPhone String
  amount        Float
  reference     String   @unique
  status        String   @default("PENDING")
  mpesaReceipt  String?
  approvedBy    String?
  createdAt     DateTime @default(now())
  updatedAt     DateTime @updatedAt
}

// ============================================================
// ACTIVITY LOGS
// ============================================================
model Log {
  id        String   @id @default(cuid())
  userId    String?
  user      User?    @relation(fields: [userId], references: [id])
  action    String
  interface String?
  details   String?
  ip        String?
  userAgent String?
  severity  String   @default("INFO")
  createdAt DateTime @default(now())
}

// ============================================================
// SECURITY ALERTS
// ============================================================
model SecurityAlert {
  id        String   @id @default(cuid())
  type      String
  message   String
  severity  String   @default("MEDIUM")
  resolved  Boolean  @default(false)
  createdAt DateTime @default(now())
}

// ============================================================
// SYSTEM SETTINGS
// ============================================================
model SystemSetting {
  key       String   @id
  value     String
  updatedAt DateTime @updatedAt
}