create table stock_handling
(
    id               int         not null auto_increment primary key,
    physical_item_id int         not null,
    field_updated    varchar(64) not null,
    value_prev       text        not null,
    value_new        text        not null,
    created_at       DATETIME DEFAULT now(),
    updated_at       DATETIME DEFAULT NULL ON UPDATE now()
);

-- 1) Room: the top‐level container
CREATE TABLE stock_room (
                      room_id     INT           NOT NULL AUTO_INCREMENT PRIMARY KEY,
                      room_symbol  VARCHAR(15)   NOT NULL,
                      room_name   VARCHAR(32)   DEFAULT NULL,
                      created_at  DATETIME      NOT NULL DEFAULT NOW(),
                      updated_at  DATETIME      NULL     ON UPDATE NOW()
);

-- 2) Line: belongs to a room
CREATE TABLE stock_line (
                      line_id     INT           NOT NULL AUTO_INCREMENT PRIMARY KEY,
                      line_symbol   VARCHAR(5)    NOT NULL,
                      line_name   VARCHAR(32)    DEFAULT NULL,
                      room_id     INT           NOT NULL,
                      created_at  DATETIME      NOT NULL DEFAULT NOW(),
                      updated_at  DATETIME      NULL     ON UPDATE NOW(),
                      FOREIGN KEY (room_id) REFERENCES stock_room(room_id)
);

-- 3) Rack: belongs to a line
CREATE TABLE stock_rack (
                      rack_id     INT           NOT NULL AUTO_INCREMENT PRIMARY KEY,
                      rack_symbol   VARCHAR(5)    NOT NULL,
                      rack_name   VARCHAR(32)    DEFAULT NULL,
                      line_id     INT           NOT NULL,
                      created_at  DATETIME      NOT NULL DEFAULT NOW(),
                      updated_at  DATETIME      NULL     ON UPDATE NOW(),
                      FOREIGN KEY (line_id) REFERENCES stock_line(line_id)
);

-- 4) Shelf: belongs to a rack
CREATE TABLE stock_shelf (
                       shelf_id    INT           NOT NULL AUTO_INCREMENT PRIMARY KEY,
                       shelf_symbol  VARCHAR(5)    NOT NULL,
                       shelf_name  VARCHAR(32)    DEFAULT NULL,
                       rack_id     INT           NOT NULL,
                       created_at  DATETIME      NOT NULL DEFAULT NOW(),
                       updated_at  DATETIME      NULL     ON UPDATE NOW(),
                       FOREIGN KEY (rack_id) REFERENCES stock_rack(rack_id)
);

CREATE TABLE stock_position (
                                position_id  INT           NOT NULL AUTO_INCREMENT PRIMARY KEY,
                                position_symbol VARCHAR(5)   NOT NULL,
                                position_name VARCHAR(32)   DEFAULT NULL,
                                shelf_id   INT           NOT NULL,
                                capacity        int        default 0,
                                temporary bool default false,
                                created_at   DATETIME      NOT NULL DEFAULT NOW(),
                                updated_at   DATETIME      NULL     ON UPDATE NOW(),
                                FOREIGN KEY (shelf_id) REFERENCES stock_shelf(shelf_id)
);

CREATE TABLE stock_section (
                         section_id   INT           NOT NULL AUTO_INCREMENT PRIMARY KEY,
                         section_symbol VARCHAR(5)    NOT NULL,
                         section_name VARCHAR(32)    DEFAULT NULL,
                         position_id     INT           NOT NULL,
                         capacity int not null,
                         retrievable bool default true,
                         created_at   DATETIME      NOT NULL DEFAULT NOW(),
                         updated_at   DATETIME      NULL     ON UPDATE NOW(),
                         FOREIGN KEY (position_id) REFERENCES stock_position(position_id)
);



create table stock_entries2section(
    entry_id int not null,
    section_id int not null,
    count int not null,
    created_at DATETIME DEFAULT now(),
    updated_at DATETIME DEFAULT NULL ON UPDATE now(),
    PRIMARY KEY (entry_id, section_id)
);

create table stock_batch
(
    id              int primary key auto_increment,
    user_id         bigint(20) unsigned not null,
    supplier_id     int      default null,
    tracking_number varchar(256)      default null,
    arrival_date    DATETIME DEFAULT null,
    default_batch    bool default false,
    created_at      DATETIME DEFAULT now(),
    updated_at      DATETIME DEFAULT NULL ON UPDATE now(),
    FOREIGN KEY (user_id) REFERENCES users (id)
);

create table stock_entries
(
    id                   int                                          not null auto_increment primary key,
    physical_item_id     int                                          not null,
    supplier_id          int unsigned                                 not null,
    count                int              default 0                   not null, #needitovatelny ve formulari / jen odpis, inventura
    original_count                int              default 0                   not null,
    original_count_invoice                int              default 0                   not null,
    price                double                                       null,
    bought               date             default null,
    description          text                                         null,
    note                 text                                         null,
    country_of_origin_id int unsigned                                 not null,
    on_the_way           bool                                         not null default false,
    stock_batch_id       int              default null,
    created_at           timestamp        default current_timestamp() not null,
    created_by           tinyint unsigned default 1                   not null,
    updated_at           timestamp        default current_timestamp() not null on update current_timestamp(),
    updated_by           tinyint unsigned                             null,
    FOREIGN KEY (stock_batch_id) REFERENCES stock_batch (id)
);


# barvy, apod, original bez loga, v krabicce s potiskem, repliky v krabici bez potisku

create table stock_attributes
(
    id         int primary key auto_increment,
    name       varchar(64) not null,
    created_at DATETIME DEFAULT now(),
    updated_at DATETIME DEFAULT NULL ON UPDATE now()
);

create table stock_attributes_translation
(
    stock_attributes_id int          not null,
    language_id         int          not null,
    translated_name     varchar(128) not null,
    created_at          DATETIME DEFAULT now(),
    updated_at          DATETIME DEFAULT NULL ON UPDATE now(),
    FOREIGN KEY (stock_attributes_id) REFERENCES stock_attributes (id),
    PRIMARY KEY (stock_attributes_id, language_id)
);

create table stock_attribute_values
(
    id                 int primary key auto_increment,
    stock_attribute_id int         not null,
    name               varchar(64) not null,
    language_id        int         not null,
    created_at         DATETIME DEFAULT now(),
    updated_at         DATETIME DEFAULT NULL ON UPDATE now(),
    FOREIGN KEY (stock_attribute_id) REFERENCES stock_attributes (id)
);

create table stock_entry2attributes
(
    stock_attributes_id      int not null,
    stock_attribute_value_id int not null,
    created_at               DATETIME DEFAULT now(),
    updated_at               DATETIME DEFAULT NULL ON UPDATE now(),
    FOREIGN KEY (stock_attributes_id) REFERENCES stock_attributes (id),
    FOREIGN KEY (stock_attribute_value_id) REFERENCES stock_attribute_values (id),
    PRIMARY KEY (stock_attributes_id, stock_attribute_value_id)
);


create table stock_batch_files
(
    id             int primary key auto_increment,
    filename       text     not null,
    file_data      longblob not null, # object storage
    file_type      enum ('invoice', 'label', 'other'),
    stock_batch_id int      not null,
    share_location text default null, # direct nahrat sem
    user_id        bigint(20) unsigned   not null,
    created_at     DATETIME DEFAULT now(),
    updated_at     DATETIME DEFAULT NULL ON UPDATE now(),
    FOREIGN KEY (stock_batch_id) REFERENCES stock_batch (id),
    FOREIGN KEY (user_id) REFERENCES users (id)
);


create table stock_entries_status
(
    id          int primary key auto_increment,
    name        varchar(64) not null, #naskladneno, spocitano, presunuto na XY, poskozeno, chybi, nadbyva
    description varchar(256) default null,
    created_at  DATETIME     DEFAULT now(),
    updated_at  DATETIME     DEFAULT NULL ON UPDATE now()
);

create table stock_entries_status_history
(
    id                    int primary key auto_increment,
    stock_entries_id        int not null,
    section_id        int default null,
    stock_entries_status_id int not null,
    status_note           text     default null,
    created_at            DATETIME DEFAULT now(),
    updated_at            DATETIME DEFAULT NULL ON UPDATE now(),
    FOREIGN KEY (stock_entries_id) REFERENCES stock_entries (id) on delete cascade,
    FOREIGN KEY (section_id) REFERENCES stock_section (section_id),
    FOREIGN KEY (stock_entries_status_id) REFERENCES stock_entries_status (id)
);

