// Datasource configuration
datasource db {
  provider = "sqlserver"
  url      = env("DATABASE_URL")
}

// User model
model User {
  id            Int          @id @default(autoincrement())
  createdAt     DateTime     @default(now())
  email         String       @unique
  passwordHash  String
  firstname     String
  surname       String
  role          String        @default("USER") @db.VarChar(10)
  scientist     Scientist?    @relation // Optional relation to Scientist
}

// Earthquake model
model Earthquake {
  id            Int          @id @default(autoincrement())
  createdAt     DateTime     @default(now())
  updatedAt     DateTime     @updatedAt
  name          String       @db.VarChar(255)
  date          DateTime
  location      String
  magnitude     Float
  depth         Float
  casualties    Int
  creatorId     Int?         // Creator's ID (Foreign Key referencing Scientist)
  creator       Scientist?   @relation("ScientistEarthquakeCreator", fields: [creatorId], references: [id], onDelete: NoAction, onUpdate: NoAction) // Points back to Scientist
}

// Observatory model
model Observatory {
  id             Int          @id @default(autoincrement())
  createdAt      DateTime     @default(now())
  updatedAt      DateTime     @updatedAt
  name           String       @db.VarChar(255)
  location       String
  longitude      String
  latitude       String
  dateEstablished Int?
  functional     Boolean
  description    String?      @db.Text
  creatorId      Int?         // Creator's ID (Foreign Key referencing Scientist)
  creator        Scientist?   @relation("ScientistObservatoryCreator", fields: [creatorId], references: [id], onDelete: NoAction, onUpdate: NoAction) // Points back to Scientist
}

// Scientist model
model Scientist {
  id             Int          @id @default(autoincrement())
  name           String
  createdAt      DateTime     @default(now())
  level          String        @db.VarChar(10) // Junior or Senior
  user           User          @relation(fields: [userId], references: [id]) // Relates to the User model
  userId         Int           @unique

  // Self-referencing relation: superior and subordinates
  superior       Scientist?    @relation("SuperiorRelation", fields: [superiorId], references: [id], onDelete: NoAction, onUpdate: NoAction) // Parent scientist
  superiorId     Int?
  subordinates   Scientist[]   @relation("SuperiorRelation") // Scientists who view this one as a superior

  // Earthquake and Observatory relations
  earthquakes    Earthquake[]  @relation("ScientistEarthquakeCreator") // Scientists can create earthquakes
  observatories  Observatory[] @relation("ScientistObservatoryCreator") // Scientists can create observatories
}