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

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

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

model Report {
  id                    Int                 @id @default(autoincrement())
  employeeId            Int
  employee              Employee            @relation(fields: [employeeId], references: [id])
  createdDate           DateTime            @default(now())
  updatedDate           DateTime            @updatedAt
  shift                 String // 'day' or 'night'
  areaId                Int
  area                  Area                @relation(fields: [areaId], references: [id])
  dredgerLocationId     Int
  dredgerLocation       DredgerLocation     @relation(fields: [dredgerLocationId], references: [id])
  dredgerLineLength     Int
  reclamationLocationId Int
  reclamationLocation   ReclamationLocation @relation(fields: [reclamationLocationId], references: [id])
  shoreConnection       Int
  reclamationHeight     Json // JSON: { base: int, extra: int }
  pipelineLength        Json // JSON: { main: int, ext1: int, reserve: int, ext2: int }
  stats                 Json // JSON: { Dozers: int, Exc: int, Loaders: int, Foreman: string, Laborer: int }
  timeSheet             Json // JSON: Array of timesheet objects
  stoppages             Json // JSON: Array of stoppage records
  notes                 String?

  // Sheet relations
  daySheetFor   Sheet[] @relation("DayShift")
  nightSheetFor Sheet[] @relation("NightShift")

  // Worker relations
  shiftWorkers ShiftWorker[]
}

model Area {
  id      Int      @id @default(autoincrement())
  name    String   @unique
  reports Report[]
  sheets  Sheet[]
}

model DredgerLocation {
  id      Int      @id @default(autoincrement())
  name    String   @unique
  class   String // 's', 'd', 'sp'
  reports Report[]
  sheets  Sheet[]
}

model ReclamationLocation {
  id      Int      @id @default(autoincrement())
  name    String   @unique
  reports Report[]
  sheets  Sheet[]
}

model Employee {
  id                  Int                  @id @default(autoincrement())
  name                String
  authLevel           Int
  username            String               @unique
  email               String               @unique
  password            String
  status              String               @default("active") // 'active' or 'inactive'
  reports             Report[]
  passwordResetTokens PasswordResetToken[]
}

model PasswordResetToken {
  id         Int      @id @default(autoincrement())
  token      String   @unique
  employeeId Int
  employee   Employee @relation(fields: [employeeId], references: [id], onDelete: Cascade)
  expiresAt  DateTime
  used       Boolean  @default(false)
  createdAt  DateTime @default(now())
}

model Foreman {
  id   Int    @id @default(autoincrement())
  name String
}

model Equipment {
  id       Int    @id @default(autoincrement())
  category String
  model    String
  number   Int
}

model Worker {
  id           Int           @id @default(autoincrement())
  name         String        @unique
  status       String        @default("active") // 'active' or 'inactive'
  createdAt    DateTime      @default(now())
  updatedAt    DateTime      @updatedAt
  shiftWorkers ShiftWorker[]
}

model ShiftWorker {
  id       Int    @id @default(autoincrement())
  reportId Int
  workerId Int
  report   Report @relation(fields: [reportId], references: [id], onDelete: Cascade)
  worker   Worker @relation(fields: [workerId], references: [id], onDelete: Cascade)

  @@unique([reportId, workerId])
}

model Sheet {
  id                    Int      @id @default(autoincrement())
  dayShiftId            Int?
  nightShiftId          Int?
  status                String   @default("pending") // 'pending', 'completed'
  areaId                Int
  dredgerLocationId     Int
  reclamationLocationId Int
  date                  String // Store as string in YYYY-MM-DD format
  createdAt             DateTime @default(now())
  updatedAt             DateTime @updatedAt

  // Relations
  area                Area                @relation(fields: [areaId], references: [id])
  dredgerLocation     DredgerLocation     @relation(fields: [dredgerLocationId], references: [id])
  reclamationLocation ReclamationLocation @relation(fields: [reclamationLocationId], references: [id])
  dayShift            Report?             @relation("DayShift", fields: [dayShiftId], references: [id])
  nightShift          Report?             @relation("NightShift", fields: [nightShiftId], references: [id])

  @@unique([areaId, dredgerLocationId, reclamationLocationId, date])
}

model MailSettings {
  id        Int      @id @default(autoincrement())
  host      String
  port      Int
  secure    Boolean  @default(false)
  username  String
  password  String
  fromName  String
  fromEmail String
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}
