
CREATE TYPE job_title AS ENUM ('Начальник цеха','Мастер','Сборщик');
CREATE TYPE status AS ENUM ('Выполнено','В процессе','Создано','Ожидание комплектующих');


CREATE TABLE workers
(
    id           SERIAL PRIMARY KEY,
    telegram_id  BIGINT UNIQUE NOT NULL,
    name         VARCHAR       NOT NULL,
    email        VARCHAR(50),
    phone_number VARCHAR(20),
    job_title    job_title default 'Сборщик',
    created_at   TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at   TIMESTAMP
);



CREATE TABLE orders
(
    id                SERIAL PRIMARY KEY,
    name              VARCHAR,
    worker_id         BIGINT REFERENCES workers (telegram_id),
    status_id         status    DEFAULT 'Создано',
    counterparty      VARCHAR(50),
    customer          VARCHAR NOT NULL,
    commencement_work DATE,
    end_work          DATE,
    created_at        TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    description       VARCHAR   DEFAULT NULL
);

CREATE TABLE components
(
    id          SERIAL PRIMARY KEY,
    name        VARCHAR NOT NULL,
    description VARCHAR NULL
);

CREATE TABLE order_components
(
    id           SERIAL PRIMARY KEY,
    order_id     INTEGER REFERENCES orders (id),
    component_id INTEGER REFERENCES components (id),
    quantity     INTEGER DEFAULT 1
);
