// 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 [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 {
  product_id int [pk, ref: - products.id, not null]
  spec jsonb [not null, note: "aktualny snapshot specyfikacji pobranej ze starego systemu"]
  source_url varchar [note: "adres url strony ze specyfikacja ze starego systemu"]
  source_hash varchar [not null, note: "hash aktualnego jsona specyfikacji do wykrywania zmian"]
  last_fetched_at timestamp [not null, note: "czas ostatniego pobrania specyfikacji ze starego systemu"]
  initial_spec_diff jsonb [note: "roznice miedzy aktualna specyfikacja a pierwsza pobrana wersja; null gdy brak zmian lub istnieje tylko pierwsza wersja"]
}

Table detail_product_versions {
  id int [pk]
  product_id int [ref: > products.id, not null]
  version_no int [not null, note: "kolejny numer wersji specyfikacji dla danego produktu; 1 oznacza pierwsza pobrana wersje"]
  spec jsonb [not null, note: "archiwalny snapshot specyfikacji ze starego systemu"]
  source_hash varchar [not null, note: "hash archiwalnej wersji jsona"]
  fetched_at timestamp [not null, note: "czas pobrania tej wersji"]

  indexes {
    (product_id, fetched_at)
    (product_id, version_no) [unique]
    (product_id, source_hash) [unique]
  }
}

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_kind {
  OPERATION
  NOTE
}

Enum note_type {
  INFO
  ERROR
}

Table event_operations {
  id int [pk]
  event_id int [ref: - events.id, not null, unique]
  operation_id int [ref: > operations.id, not null]
}

Table event_notes {
  id int [pk]
  event_id int [ref: - events.id, not null, unique]
  type note_type [not null]
  note text [not null]
}

Table event_photos {
  id int [pk]
  event_id int [ref: > events.id, not null]
  photo_url varchar [not null]
  description text
}

Table events {
  id int [pk]
  part_id int [ref: > parts.id, not null]
  ordinal int [not null, note: "liczba porzadkowa, wielokrotnosc 32, zeby mozna bylo dodac pomiedzy"]
  kind event_kind [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]
  }
}
