// Use DBML to define your database structure
// Docs: https://dbml.dbdiagram.io/docs

Table models {
  id int [pk]
  name varchar [not null, unique]
  strings int [not null, default: 6, note: "ilosc strun"]
  scale int [not null, default: 645, note: "menzura w mm"]
}

Table colors {
  id int [pk]
  name varchar [not null, unique]
  burst bool [not null, default: false, note: "czy ma cien"]
  monolith bool [not null, default: false, note: "czy nie jest transparetny"]
}

Enum finish_type {
  GLOSS
  SATIN
  MAT
  NITRO
}

Enum part_type {
  BODY
  NECK
}

Table parts {
  id int [pk]
  product_id int [ref: > products.id, not null]
  type part_type_type [not null]
  color_top int [ref: > colors.id, not null]
  color_back int [ref: > colors.id, not null]
  finish_top finish_type [not null]
  finish_back finish_type [not null]

  indexes {
    (product_id, type) [unique]
  }
}

Table products {
  id int [pk]
  model int [ref: > models.id, not null]
  note text
}

Table detail_products {
  id int [pk]
  product_id int [ref: - products.id, not null]
  spec jsonb [note: "specyfikacja pobrana ze starego systemu w formie json"]
  url varchar [note: "adres url strony ze specyfikacja ze starego systemu" ]

}

Table clients {
  id int [pk]
  name varchar [not null, note: "Nazwa klienta"]
  country varchar [note: "Kraj klienta"]
}

Table orders {
  id int [pk]
  order_number int [not null, note: "XXXX, eg 0027"]
  order_year int [not null, note: "YYYY, eg 2025"]
  client_id int [ref: > clients.id ,not null]
  indexes {
    (order_number, order_year) [unique]
  }
}

Table order_products {
  id int [pk]
  product_id int [ref: > products.id, not null, unique]
  order_id int [ref: > orders.id, not null]
  product_order_idx int [not null]

  indexes {
    (order_id, product_order_idx) [unique]
  }
}

Table operations {
  id int [pk]
  operation varchar [not null, unique]
  description text
}

Enum event_type {
  OPERATION
  INFO
  ERROR
}

Table event_info {
  id int [pk]
  event_id int [ref: > events.id, not null]
  comment text [default: null]
  photo_url varchar [default: null]

}
Table events {
  id int [pk]
  part_id int [ref: > parts.id, not null]
  operation_id int [ref: > operations.id]
  ordinal int [not null, note: "liczba porzadkowa, wielokrotnosc 32, zeby mozna bylo dodac pomiedzy"]
  type event_type [not null, default: "OPERATION"]
  date date [not null, note: "data jest ustawiana po wykonaniu operacji, niekonicznie w ten sam dzien. mozna edytowac"]

  indexes {
    (part_id, ordinal) [unique]
  }
}

Table production_lists {
  id int [pk]
  name varchar [not null, unique]
  description text
}

Table production_list_products {
  id int [pk]
  product_id int [ref: > products.id, not null]
  prod_list_id int [ref: > production_lists.id, not null]

  indexes {
    (product_id, prod_list_id) [unique]
  }
}