generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["postgresqlExtensions"]
}

datasource db {
  provider   = "postgresql"
  url        = env("DATABASE_URL")
  directUrl  = env("DIRECT_URL")
  extensions = [pgcrypto, uuid_ossp(map: "uuid-ossp", schema: "extensions")]
}

model cities {
  id            String         @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  geographic_id String?        @db.Uuid
  name          String         @db.VarChar(100)
  code          String         @db.VarChar(10)
  created_at    DateTime       @default(now()) @db.Timestamptz(6)
  updated_at    DateTime       @default(now()) @db.Timestamptz(6)
  geographics   geographics?   @relation(fields: [geographic_id], references: [id])
  crimes        crimes[]
  demographics  demographics[]
  districts     districts[]

  @@index([name])
}

model contact_messages {
  id                 String                  @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  name               String?                 @db.VarChar(255)
  email              String?                 @db.VarChar(255)
  phone              String?                 @db.VarChar(20)
  message_type       String?                 @db.VarChar(50)
  message_type_label String?                 @db.VarChar(50)
  message            String?
  status             status_contact_messages @default(new)
  created_at         DateTime                @default(now()) @db.Timestamptz(6)
  updated_at         DateTime                @db.Timestamptz(6)
}

model crime_cases {
  id                String            @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  crime_id          String?           @db.Uuid
  crime_category_id String?           @db.Uuid
  date              DateTime          @db.Timestamptz(6)
  time              DateTime          @db.Timestamptz(6)
  location          String            @db.VarChar(255)
  latitude          Float
  longitude         Float
  description       String
  victim_count      Int
  status            crime_status      @default(new)
  created_at        DateTime          @default(now()) @db.Timestamptz(6)
  updated_at        DateTime          @default(now()) @db.Timestamptz(6)
  crime_categories  crime_categories? @relation(fields: [crime_category_id], references: [id])
  crimes            crimes?           @relation(fields: [crime_id], references: [id])
}

model crime_categories {
  id          String        @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  name        String        @db.VarChar(255)
  description String
  created_at  DateTime      @default(now()) @db.Timestamptz(6)
  updated_at  DateTime      @default(now()) @db.Timestamptz(6)
  crime_cases crime_cases[]
}

model crimes {
  id              String        @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  district_id     String?       @db.Uuid
  city_id         String?       @db.Uuid
  year            Int
  number_of_crime Int
  rate            crime_rates   @default(low)
  heat_map        Json?
  created_at      DateTime      @default(now()) @db.Timestamptz(6)
  updated_at      DateTime      @default(now()) @db.Timestamptz(6)
  crime_cases     crime_cases[]
  cities          cities?       @relation(fields: [city_id], references: [id])
  districts       districts?    @relation(fields: [district_id], references: [id])

  @@unique([city_id, year])
  @@unique([district_id, year])
}

model demographics {
  id                 String     @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  district_id        String?    @db.Uuid
  city_id            String?    @db.Uuid
  province_id        String?    @db.Uuid
  year               Int
  population         Int
  population_density Float
  poverty_rate       Float
  created_at         DateTime   @default(now()) @db.Timestamptz(6)
  updated_at         DateTime   @default(now()) @db.Timestamptz(6)
  cities             cities?    @relation(fields: [city_id], references: [id])
  districts          districts? @relation(fields: [district_id], references: [id])

  @@unique([city_id, year])
  @@unique([district_id, year])
}

model districts {
  id           String         @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  city_id      String         @db.Uuid
  name         String         @db.VarChar(100)
  code         String         @db.VarChar(10)
  created_at   DateTime       @default(now()) @db.Timestamptz(6)
  updated_at   DateTime       @default(now()) @db.Timestamptz(6)
  crimes       crimes[]
  demographics demographics[]
  cities       cities         @relation(fields: [city_id], references: [id], onDelete: Cascade)
  geographics  geographics?

  @@index([name])
}

model geographics {
  id          String     @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  district_id String?    @unique @db.Uuid
  latitude    Float?
  longitude   Float?
  land_area   Float?
  polygon     Json?
  created_at  DateTime   @default(now()) @db.Timestamptz(6)
  updated_at  DateTime   @default(now()) @db.Timestamptz(6)
  cities      cities[]
  districts   districts? @relation(fields: [district_id], references: [id])
}

model profiles {
  id         String    @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  user_id    String    @unique @db.Uuid
  bio        String?
  address    String?   @db.VarChar(255)
  city       String?   @db.VarChar(100)
  country    String?   @db.VarChar(100)
  birth_date DateTime?
  users      users     @relation(fields: [user_id], references: [id])

  @@index([user_id])
}

model users {
  id                       String    @id @db.Uuid
  email                    String    @unique @db.VarChar(255)
  email_verified           Boolean   @default(false)
  first_name               String?   @db.VarChar(255)
  last_name                String?   @db.VarChar(255)
  avatar                   String?   @db.VarChar(255)
  role                     roles     @default(user)
  created_at               DateTime  @default(now())
  updated_at               DateTime
  banned_until             DateTime?
  confirmation_sent_at     DateTime?
  confirmation_token       String?   @db.VarChar(255)
  deleted_at               DateTime?
  email_change             String?   @db.VarChar(255)
  email_change_sent_at     DateTime?
  email_change_token       String?   @db.VarChar(255)
  email_confirmed_at       DateTime?
  encrypted_password       String?   @db.VarChar(255)
  is_anonymous             Boolean?  @default(false)
  is_sso_user              Boolean?  @default(false)
  last_sign_in_at          DateTime?
  phone                    String?   @db.VarChar(20)
  phone_confirmed_at       DateTime?
  raw_app_meta_data        Json?
  raw_user_meta_data       Json?
  reauthentication_sent_at DateTime?
  reauthentication_token   String?   @db.VarChar(255)
  recovery_sent_at         DateTime?
  recovery_token           String?   @db.VarChar(255)
  providers                Json?     @default("[]")
  profiles                 profiles?

  @@index([role])
}

enum crime_rates {
  low
  medium
  high
}

enum crime_status {
  new
  in_progress
  resolved
}

enum roles {
  admin
  staff
  user
}

enum status_contact_messages {
  new
  read
  replied
  resolved
}
