// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

// Looking for ways to speed up your queries, or scale easily with your serverless or edge functions?
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init

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

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

model User {
  id           String  @id @default(cuid())
  name         String
  email        String  @unique
  password     String
  role         Role    @default(USER)
  permissions  Json?   // For storing service access permissions
  serviceRequests ServiceRequest[]
  createdAt    DateTime @default(now())
  updatedAt    DateTime @updatedAt
}

model Admin {
  id           String   @id @default(cuid())
  name         String
  email        String   @unique
  password     String
  role         AdminRole @default(ADMIN)
  token        String?  @unique // For session management
  isActive     Boolean  @default(true)
  lastLogin    DateTime?
  createdAt    DateTime @default(now())
  updatedAt    DateTime @updatedAt
}

model SystemRole {
  id          String   @id @default(cuid())
  name        String   @unique
  description String
  isActive    Boolean  @default(true)
  permissions SystemRolePermission[]
  createdAt   DateTime @default(now())
  updatedAt   DateTime @updatedAt
}

model Permission {
  id          String   @id @default(cuid())
  name        String
  description String?
  module      String   // e.g., dashboard, requests, users, permissions
  action      String   // e.g., view, create, edit, delete, approve
  isActive    Boolean  @default(true)
  roles       SystemRolePermission[]
  createdAt   DateTime @default(now())
  updatedAt   DateTime @updatedAt

  @@unique([name, module, action])
}

model SystemRolePermission {
  id           String @id @default(cuid())
  roleId       String
  permissionId String
  role         SystemRole @relation(fields: [roleId], references: [id], onDelete: Cascade)
  permission   Permission  @relation(fields: [permissionId], references: [id], onDelete: Cascade)

  @@unique([roleId, permissionId])
}

model ServiceLink {
  id          String            @id @default(cuid())
  title       String
  subtitle    String?
  description String?
  linkType    ServiceLinkType   @default(INTERNAL) // Type of link: INTERNAL, EXTERNAL, SUBMENU
  linkUrl     String?           // URL for external links or internal routes
  linkPage    String?           // Internal page route
  iconFile    String?           // Uploaded icon file path
  iconUrl     String?           // External icon URL (fallback)
  order       Int               @default(0) // Order for display
  isActive    Boolean           @default(true)
  parentId    String?           // Parent service ID for submenu items
  parent      ServiceLink?      @relation("ServiceLinkHierarchy", fields: [parentId], references: [id])
  children    ServiceLink[]     @relation("ServiceLinkHierarchy")
  createdAt   DateTime          @default(now())
  updatedAt   DateTime          @updatedAt
}

model Service {
  id             String            @id @default(cuid())
  title          String
  slug           String            @unique
  description    String
  category       String?
  estimatedTime  String?
  requirements   Json?             // Store array of requirements
  iconUrl        String?           // Service icon URL
  linkType       ServiceLinkType   @default(INTERNAL) // Type of link: INTERNAL, EXTERNAL, SUBMENU
  linkUrl        String?           // URL for external links or internal routes
  isNewTab       Boolean           @default(false)    // Open in new tab for external links
  status         String            @default("ACTIVE")
  position       Int               @default(0) // Position for ordering services
  parentId       String?           // Parent service ID for submenu items
  parent         Service?          @relation("ServiceHierarchy", fields: [parentId], references: [id])
  children       Service[]         @relation("ServiceHierarchy")
  templates      ServiceTemplate[]
  requests       ServiceRequest[]
  createdAt      DateTime          @default(now())
  updatedAt      DateTime          @updatedAt
}

model ServiceTemplate {
  id             String  @id @default(cuid())
  serviceId      String
  templateContent String
  service        Service @relation(fields: [serviceId], references: [id], onDelete: Cascade)
  createdAt      DateTime @default(now())
  updatedAt      DateTime @updatedAt
}

model ServiceRequest {
  id              String   @id @default(cuid())
  userId          String
  serviceId       String
  trackingNumber  String   @unique
  status          RequestStatus @default(PENDING)
  submissionData  Json     // Store form data
  statusLog       Json?    // Store status change history
  signatureUrl    String?  // Digital signature URL
  photoUrl        String?  // Photo URL
  user            User     @relation(fields: [userId], references: [id])
  service         Service  @relation(fields: [serviceId], references: [id])
  createdAt       DateTime @default(now())
  updatedAt       DateTime @updatedAt
}

model SystemSettings {
  id                   String  @id @default(cuid())
  siteName             String  @default("Polres Wonosobo")
  siteDescription      String  @default("Sistem Pelayanan Digital Polres Wonosobo")
  contactEmail         String  @default("polreswonosobo@polri.go.id")
  contactPhone         String  @default("(0286) 321001")
  contactWhatsApp      String  @default("+62 812-3456-7890")
  address              String  @default("Jl. Pahlawan No. 15, Wonosobo")
  workingHours         String  @default("Senin - Jumat: 08:00 - 16:00")
  timezone             String  @default("Asia/Jakarta")
  language             String  @default("id")
  emailNotifications   Boolean @default(true)
  smsNotifications     Boolean @default(false)
  autoAssignment       Boolean @default(false)
  maintenanceMode      Boolean @default(false)
  maxFileSize          Int     @default(50)
  allowedFileTypes     Json?
  sessionTimeout       Int     @default(8)
  passwordMinLength    Int     @default(8)
  requireEmailVerification Boolean @default(true)
  // Hero section settings
  heroTitle            String  @default("Sistem Pelayanan Digital")
  heroSubtitle         String  @default("Polres Wonosobo")
  heroDescription      String  @default("Layanan pengajuan dokumen kepolisian secara online yang mudah, cepat, dan terpercaya")
  heroBackgroundImage  String  @default("/hero-bg.jpg")
  heroButtonText       String  @default("Lihat Layanan")
  heroButtonLink       String  @default("#layanan")
  heroSecondaryButtonText String @default("Lacak Pengajuan")
  heroSecondaryButtonLink String @default("/lacak")
  // Branding settings
  siteLogo             String?  // Website Logo
  siteIcon             String?  // Favicon
  polresLogo           String?  // Logo Polres (Branding)
  polresLogo2          String?  // Additional Logo Polres (Branding)
  runningText          String?  // Running text for landing page
  // SEO Settings
  metaTitle            String?
  metaDescription      String?
  metaKeywords         String?
  ogImage              String?
  // Kapolres Profile Settings
  kapolresName          String  @default("AKBP. Dr. Budi Purnama, S.I.K., M.H.")
  kapolresNRP           String  @default("87050168")
  kapolresPangkat       String  @default("AKBP")
  kapolresSignature     String? // Base64 encoded signature image
  // Nomor Pengajuan Format Settings
  requestNumberFormat   String  @default("SR-{YYYY}{MM}{DD}-{seq:4}")
  requestNumberSequence Int     @default(1)
  requestNumberReset    String  @default("daily") // daily, monthly, yearly, never
  // Format per kategori layanan
  skckNumberFormat      String  @default("SKCK-{YYYY}{MM}{DD}-{seq:4}")
  spktNumberFormat      String  @default("SPKT-{YYYY}{MM}{DD}-{seq:4}")
  lalinNumberFormat     String  @default("LL-{YYYY}{MM}{DD}-{seq:4}")
  skckSequence          Int     @default(1)
  spktSequence          Int     @default(1)
  lalinSequence         Int     @default(1)
  createdAt            DateTime @default(now())
  updatedAt            DateTime @updatedAt
}

model LandingPage {
  id          String        @id @default(cuid())
  name        String        @unique
  slug        String        @unique
  title       String
  description String?
  type        LandingPageType @default(CLASSIC)
  path        String        @unique // Path to the page component
  isActive    Boolean       @default(false)
  isDefault   Boolean       @default(false)
  settings    Json?         // Landing page specific settings
  metadata    Json?         // SEO metadata, custom configs
  createdAt   DateTime      @default(now())
  updatedAt   DateTime      @updatedAt
}

model Slider {
  id          String   @id @default(cuid())
  title       String?
  subtitle    String?
  description String?
  imageFile   String?  // Uploaded image file path
  imageUrl    String?  // External image URL (fallback)
  linkUrl     String?  // Optional link when slider is clicked
  displayType String   @default("WITH_TEXT") // WITH_TEXT or BACKGROUND_ONLY
  isActive    Boolean  @default(true)
  order       Int      @default(0) // Order for display
  createdAt   DateTime @default(now())
  updatedAt   DateTime @updatedAt
}

model Polres {
  id            String   @id @default(cuid())
  name          String   @unique
  address       String
  phone         String?
  email         String?
  website       String?
  fax           String?
  description   String?
  logo          String?  // Logo file path
  photo         String?  // Building/photo file path
  kapolresName  String?  // Name of Kapolres
  kapolresNRP   String?  // NRP of Kapolres
  kapolresPangkat String? // Rank of Kapolres
  isActive      Boolean  @default(true)
  order         Int      @default(0)
  polseks       Polsek[]
  createdAt     DateTime @default(now())
  updatedAt     DateTime @updatedAt
}

model Polsek {
  id          String   @id @default(cuid())
  name        String
  address     String
  phone       String?
  email       String?
  website     String?
  fax         String?
  description String?
  photo       String?  // Building/photo file path
  kapolsekName String? // Name of Kapolsek
  kapolsekNRP  String? // NRP of Kapolsek
  kapolsekPangkat String? // Rank of Kapolsek
  isActive    Boolean  @default(true)
  order       Int      @default(0)
  polresId    String
  polres      Polres   @relation(fields: [polresId], references: [id], onDelete: Cascade)
  createdAt   DateTime @default(now())
  updatedAt   DateTime @updatedAt

  @@unique([name, polresId])
}

model SocialMedia {
  id          String            @id @default(cuid())
  platform    SocialMediaPlatform
  name        String            // Display name (e.g., "Polres Wonosobo Official")
  url         String            // Full URL to social media profile
  username    String?           // Username/handle (e.g., "@polreswonosobo")
  description String?           // Optional description
  icon        String?           // Icon file path or URL
  isActive    Boolean           @default(true)
  order       Int               @default(0)
  createdAt   DateTime          @default(now())
  updatedAt   DateTime          @updatedAt

  @@unique([platform, name])
}

enum Role {
  USER
  ADMIN
  SUPERADMIN
}

enum AdminRole {
  ADMIN
  SUPERADMIN
}

enum RequestStatus {
  PENDING
  IN_PROGRESS
  NEED_REVISION
  REVIEWING
  COMPLETED
}

enum ServiceLinkType {
  INTERNAL    // Link to internal service page
  EXTERNAL    // Link to external website
  SUBMENU     // Has submenu items
}

enum LandingPageType {
  CLASSIC     // Original landing page design
  MODERN      // Modern landing page design
  MINIMAL     // Minimal landing page design
  CORPORATE   // Corporate landing page design
}

enum SocialMediaPlatform {
  FACEBOOK
  INSTAGRAM
  TWITTER
  YOUTUBE
  TIKTOK
  LINKEDIN
  WHATSAPP
  TELEGRAM
}