// 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"
  output   = "../generated/prisma/client"
}

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

enum UserRole {
  ADMIN
  MANAGER
  STAFF
  VIEWER
}

model User {
  id         String       @id @default(uuid())
  username   String       @unique
  name       String
  email      String       @unique
  password   String
  role       UserRole     @default(STAFF)
  isActive   Boolean      @default(true)
  createdAt  DateTime     @default(now())
  updatedAt  DateTime     @updatedAt
  movements  Movement[]
  assignment Assignment[]
}

enum RecipientDepartment {
  IT
  ING
  LNG
  TRAFFIC
  LOGISTICS
  ADMINISTRATION
  SALES
  OTHER
}

model Recipient {
  id          String       @id @default(uuid())
  username    String       @unique
  firstName   String
  lastName    String
  department  String?
  email       String?      @unique
  phone       String?
  isActive    Boolean      @default(true)
  createdAt   DateTime     @default(now())
  updatedAt   DateTime     @updatedAt
  assignments Assignment[]
  movement    Movement[]

  @@index([lastName, firstName])
}

model Category {
  id          String   @id @default(uuid())
  name        String   @unique
  description String?
  isActive    Boolean  @default(true)
  items       Item[]
  createdAt   DateTime @default(now())
  updatedAt   DateTime @updatedAt

  @@index([name])
}

enum ItemStatus {
  AVAILABLE
  ASSIGNED
  RESERVED
  IN_REPAIR
  BROKEN
  STOLEN
  DISPOSED
}

model Item {
  id           String       @id @default(uuid())
  name         String
  description  String?
  categoryId   String
  category     Category     @relation(fields: [categoryId], references: [id])
  serialNumber String?      @unique
  deliveryNote String?
  stock        Int
  status       ItemStatus   @default(AVAILABLE)
  createdAt    DateTime     @default(now())
  updatedAt    DateTime     @updatedAt
  assignements Assignment[] @relation("ItemAssignments")
  movements    Movement[]

  @@index([status])
  @@index([categoryId])
  @@index([name])
}

model Assignment {
  id             String     @id @default(uuid())
  quantity       Int
  notes          String?
  itemId         String
  item           Item       @relation("ItemAssignments", fields: [itemId], references: [id], onDelete: Cascade, onUpdate: Cascade)
  recipientId    String
  recipient      Recipient  @relation(fields: [recipientId], references: [id], onDelete: Cascade, onUpdate: Cascade)
  assignmentDate DateTime   @default(now())
  returnDate     DateTime?
  createdBy      String
  createdUser    User       @relation(fields: [createdBy], references: [id])
  createdAt      DateTime   @default(now())
  updatedAt      DateTime   @updatedAt
  movement       Movement[]

  @@index([itemId])
  @@index([recipientId])
}

enum MovementType {
  IN
  OUT
  ASSIGNMENT
  RETURN
  ADJUSTMENT
}

model Movement {
  id            String       @id @default(uuid())
  type          MovementType @default(IN)
  quantity      Int
  details       String?
  notes         String?
  itemId        String
  item          Item         @relation(fields: [itemId], references: [id])
  recipientId   String?
  recipient     Recipient?   @relation(fields: [recipientId], references: [id])
  assignamentId String?
  assignament   Assignment?  @relation(fields: [assignamentId], references: [id])
  userId        String
  user          User         @relation(fields: [userId], references: [id])
  createdAt     DateTime     @default(now())

  @@index([itemId])
  @@index([recipientId])
  @@index([type])
}
