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

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

model User {
  id            String    @id @default(cuid())
  name          String?
  email         String?   @unique
  emailVerified DateTime?
  image         String?
  password      String?
  role          UserRole  @default(POSTACI)
  createdAt     DateTime  @default(now())
  updatedAt     DateTime  @updatedAt
  groupLeaderId String?
  groupLeader   User?     @relation("GroupMembers", fields: [groupLeaderId], references: [id])
  groupMembers  User[]    @relation("GroupMembers")
  routes        Route[]
  accounts      Account[]
  sessions      Session[]
  history       History[]
}

model Account {
  id                String  @id @default(cuid())
  userId            String
  type              String
  provider          String
  providerAccountId String
  refresh_token     String? @db.Text
  access_token      String? @db.Text
  expires_at        Int?
  token_type        String?
  scope             String?
  id_token          String? @db.Text
  session_state     String?

  user User @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@unique([provider, providerAccountId])
}

model Session {
  id           String   @id @default(cuid())
  sessionToken String   @unique
  userId       String
  expires      DateTime
  user         User     @relation(fields: [userId], references: [id], onDelete: Cascade)
}

model Route {
  id          String        @id @default(cuid())
  name        String?
  createdAt   DateTime      @default(now())
  updatedAt   DateTime      @updatedAt
  userId      String
  user        User          @relation(fields: [userId], references: [id])
  addresses   Address[]     @relation("RouteAddresses")
  status      RouteStatus   @default(CREATED)
  deliveries  Delivery[]    @relation("RouteDeliveries")
  history     History[]
}

model Address {
  id        String     @id @default(cuid())
  street    String
  city      String
  postcode  String
  country   String     @default("Switzerland")
  latitude  Float?
  longitude Float?
  routeId   String
  route     Route      @relation("RouteAddresses", fields: [routeId], references: [id], onDelete: Cascade)
  order     Int        @default(0)
  delivery  Delivery[] @relation("AddressDeliveries")
  history   History[]
}

model Delivery {
  id        String         @id @default(cuid())
  addressId String
  address   Address        @relation("AddressDeliveries", fields: [addressId], references: [id], onDelete: Cascade)
  routeId   String
  route     Route          @relation("RouteDeliveries", fields: [routeId], references: [id], onDelete: Cascade)
  status    DeliveryStatus @default(PENDING)
  createdAt DateTime       @default(now())
  updatedAt DateTime       @updatedAt
}

model History {
  id        String   @id @default(cuid())
  userId    String
  user      User     @relation(fields: [userId], references: [id], onDelete: Cascade)
  routeId   String
  route     Route    @relation(fields: [routeId], references: [id], onDelete: Cascade)
  addressId String
  address   Address  @relation(fields: [addressId], references: [id], onDelete: Cascade)
  action    String
  createdAt DateTime @default(now())
}

enum UserRole {
  ADMIN
  GRUP_SEFI
  POSTACI
}

enum RouteStatus {
  CREATED
  OPTIMIZED
  IN_PROGRESS
  COMPLETED
  CANCELLED
}

enum DeliveryStatus {
  PENDING
  IN_PROGRESS
  COMPLETED
  FAILED
}
