// 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 = "postgresql"
  url      = env("DATABASE_URL")
}

enum ProgressStatus {
  cari_data
  dihubungi
  negosiasi
  follow_up
  mou
  onboarded
  tidak_berminat
}

model RevokedToken {
  id         String   @id @default(uuid())
  token      String
  revoked_at DateTime @default(now())

  @@map("revoked_tokens")
}

model ActivityLog {
  id        String    @id @default(uuid())
  user_id   String
  username  String
  action    String
  createdAt DateTime  @default(now())
  updatedAt DateTime  @updatedAt
  deletedAt DateTime?

  @@map("activity_logs")
}

model User {
  id               String          @id @default(uuid())
  username         String
  email            String
  password         String
  firstname        String
  lastname         String
  role             String
  createdAt        DateTime        @default(now())
  updatedAt        DateTime        @updatedAt
  deletedAt        DateTime?
  hospitals        Hospital[]
  user_areas       UserArea[]
  vendors          Vendor[]
  status_histories StatusHistory[]

  @@map("users")
}

model Province {
  id         String     @id @default(uuid())
  name       String     @unique
  cities     City[]
  hospitals  Hospital[]
  user_areas UserArea[]
  createdAt  DateTime   @default(now())
  updatedAt  DateTime   @updatedAt
  deletedAt  DateTime?

  @@map("provinces")
}

model City {
  id          String     @id @default(uuid())
  name        String
  province_id String
  province    Province   @relation(fields: [province_id], references: [id])
  hospitals   Hospital[]
  createdAt   DateTime   @default(now())
  updatedAt   DateTime   @updatedAt
  deletedAt   DateTime?

  @@unique([name, province_id], name: "name_province_id")
  @@map("cities")
}

model Hospital {
  id                   String              @id @default(uuid())
  name                 String
  hospital_code        String?
  type                 String?
  ownership            String?
  province_id          String
  city_id              String
  address              String?
  simrs_type           String?
  contact              String?
  image                String?
  progress_status      ProgressStatus
  note                 String?
  created_by           String
  latitude             Float?
  longitude            Float?
  gmaps_url            String?             @db.Text
  createdAt            DateTime            @default(now())
  updatedAt            DateTime            @updatedAt
  deletedAt            DateTime?
  province             Province            @relation(fields: [province_id], references: [id])
  city                 City                @relation(fields: [city_id], references: [id])
  user                 User                @relation(fields: [created_by], references: [id])
  vendor_histories     VendorHistory[]
  executives_histories ExecutivesHistory[]
  status_histories     StatusHistory[]

  @@map("hospitals")
}

model Vendor {
  id               String          @id @default(uuid())
  name             String
  name_pt          String?
  strengths        String?
  weaknesses       String?
  website          String?
  created_by       String
  createdAt        DateTime        @default(now())
  updatedAt        DateTime        @updatedAt
  deletedAt        DateTime?
  user             User            @relation(fields: [created_by], references: [id])
  vendor_histories VendorHistory[]

  @@map("vendors")
}

model UserArea {
  id          String    @id @default(uuid())
  user_id     String
  province_id String
  user        User      @relation(fields: [user_id], references: [id])
  province    Province  @relation(fields: [province_id], references: [id])
  createdAt   DateTime  @default(now())
  updatedAt   DateTime  @updatedAt
  deletedAt   DateTime?

  @@map("user_areas")
}

model VendorHistory {
  id                    String    @id @default(uuid())
  hospital_id           String
  vendor_id             String?
  vendor_impression     String?   @db.Text
  status                String?
  contract_date         DateTime?
  contract_expired_date DateTime?
  hospital              Hospital  @relation(fields: [hospital_id], references: [id])
  vendor                Vendor?   @relation(fields: [vendor_id], references: [id])
  createdAt             DateTime  @default(now())
  updatedAt             DateTime  @updatedAt
  deletedAt             DateTime?

  @@map("vendor_histories")
}

model ExecutivesHistory {
  id             String    @id @default(uuid())
  hospital_id    String
  executive_name String?
  contact        String?
  status         String?
  start_term     DateTime?
  end_term       DateTime?
  hospital       Hospital  @relation(fields: [hospital_id], references: [id])
  createdAt      DateTime  @default(now())
  updatedAt      DateTime  @updatedAt
  deletedAt      DateTime?

  @@map("executives_histories")
}

model StatusHistory {
  id          String         @id @default(uuid())
  hospital_id String
  user_id     String
  old_status  ProgressStatus
  new_status  ProgressStatus
  note        String?        @db.Text
  hospital    Hospital       @relation(fields: [hospital_id], references: [id])
  user        User           @relation(fields: [user_id], references: [id])
  createdAt   DateTime       @default(now())
  updatedAt   DateTime       @updatedAt
  deletedAt   DateTime?

  @@map("status_histories")
}
