generator client {
  provider      = "prisma-client-js"
  // Windows native + cPanel/CloudLinux (live host needs debian-openssl-1.0.x)
  binaryTargets = ["native", "debian-openssl-1.0.x", "debian-openssl-1.1.x", "debian-openssl-3.0.x", "rhel-openssl-1.0.x", "rhel-openssl-1.1.x", "rhel-openssl-3.0.x"]
}

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

enum Role {
  customer
  admin
}

enum Gender {
  men
  women
  unisex
}

enum OrderStatus {
  pending
  processing
  shipped
  delivered
  cancelled
}

model User {
  id           String   @id @default(cuid())
  name         String
  email        String   @unique
  passwordHash String
  role         Role     @default(customer)
  createdAt    DateTime @default(now())
  updatedAt    DateTime @updatedAt
  orders       Order[]
}

model Category {
  id          String    @id @default(cuid())
  name        String
  slug        String    @unique
  description String    @db.Text
  image       String    @db.Text
  gender      String    @default("all")
  products    Product[]
  createdAt   DateTime  @default(now())
  updatedAt   DateTime  @updatedAt
}

model Product {
  id               String      @id
  name             String
  slug             String      @unique
  price            Float
  compareAt        Float?
  categoryId       String
  category         Category    @relation(fields: [categoryId], references: [id])
  categoryName     String
  categorySlug     String
  gender           Gender
  description      String      @db.Text
  shortDescription String      @db.Text
  longDescription  String      @db.Text
  details          Json
  materials        Json
  care             Json
  fit              String      @db.Text
  sizeGuide        Json
  sizeGuideNote    String      @db.Text
  images           Json
  variants         Json
  stock            Int         @default(0)
  rating           Float       @default(4.5)
  reviewCount      Int         @default(0)
  tags             Json
  featured         Boolean     @default(false)
  flashSale        Boolean     @default(false)
  recentlyOrdered  Boolean     @default(false)
  bestSeller       Boolean     @default(false)
  newArrival       Boolean     @default(false)
  seller           String      @default("SiloMart")
  verified         Boolean     @default(true)
  sold             Int?
  flashLimit       Int?
  createdAt        DateTime    @default(now())
  updatedAt        DateTime    @updatedAt
  orderItems       OrderItem[]

  @@index([categorySlug])
  @@index([gender])
  @@index([featured])
  @@index([flashSale])
}

model Order {
  id              String      @id
  userId          String?
  user            User?       @relation(fields: [userId], references: [id], onDelete: SetNull)
  customerName    String
  email           String
  phone           String?
  status          OrderStatus @default(pending)
  subtotal        Float
  shipping        Float
  discount        Float       @default(0)
  total           Float
  shippingAddress String      @db.Text
  paymentMethod   String?
  paymentId       String?
  createdAt       DateTime    @default(now())
  updatedAt       DateTime    @updatedAt
  items           OrderItem[]

  @@index([userId])
  @@index([email])
  @@index([status])
}

model OrderItem {
  id        String  @id @default(cuid())
  orderId   String
  order     Order   @relation(fields: [orderId], references: [id], onDelete: Cascade)
  productId String?
  product   Product? @relation(fields: [productId], references: [id], onDelete: SetNull)
  name      String
  price     Float
  quantity  Int
  image     String  @db.Text
  variant   String?

  @@index([orderId])
}

/** Single-row store configuration (branding, Stripe, SMTP, SEO, footer). */
model SiteSettings {
  id                    String   @id @default("default")
  siteName              String   @default("SiloMart")
  tagline               String   @default("Honest bins. Quiet prices.")
  description           String   @db.Text
  logoUrl               String   @db.Text @default("")
  faviconUrl            String   @db.Text @default("")
  supportEmail          String   @default("support@silomart.com")
  supportPhone          String   @default("+1 (800) 555-0147")
  address               String   @db.Text @default("1200 Market Street, Suite 400, San Francisco, CA 94103, USA")
  facebookUrl           String   @db.Text @default("")
  instagramUrl          String   @db.Text @default("")
  twitterUrl            String   @db.Text @default("")
  youtubeUrl            String   @db.Text @default("")
  tiktokUrl             String   @db.Text @default("")
  currency              String   @default("USD")
  footerTagline         String   @db.Text @default("A mill-direct neighborhood grocer — short bins, dated drops, quiet prices.")
  footerNote            String   @default("Pickup · Delivery · Aisle walk")
  stripeEnabled         Boolean  @default(false)
  stripePublishableKey  String   @db.Text @default("")
  stripeSecretKey       String   @db.Text @default("")
  smtpHost              String   @default("")
  smtpPort              Int      @default(587)
  smtpUser              String   @default("")
  smtpPass              String   @db.Text @default("")
  smtpFrom              String   @default("")
  smtpSecure            Boolean  @default(false)
  updatedAt             DateTime @updatedAt
}

/** CMS content pages (policies, about, custom pages). */
model ContentPage {
  id           String   @id @default(cuid())
  slug         String   @unique
  title        String
  excerpt      String   @db.Text @default("")
  content      String   @db.LongText
  published    Boolean  @default(true)
  showInFooter Boolean  @default(true)
  footerGroup  String   @default("legal")
  sortOrder    Int      @default(0)
  createdAt    DateTime @default(now())
  updatedAt    DateTime @updatedAt

  @@index([published, showInFooter])
  @@index([footerGroup, sortOrder])
}

/** Fixed-window abuse protection buckets (no Redis). */
model RateLimitBucket {
  id      String   @id @db.VarChar(191)
  count   Int      @default(0)
  resetAt DateTime

  @@index([resetAt])
}

/** Stripe webhook idempotency — prevent duplicate processing. */
model StripeWebhookEvent {
  id          String   @id @db.VarChar(255)
  type        String
  processedAt DateTime @default(now())
}
