-- -- Сначала создадим базу данных
-- DROP DATABASE transport_company;
-- CREATE DATABASE transport_company;

-- Переключаемся на базу данных
\c transport_company;
CREATE TABLE Clients (
    id SERIAL PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    email VARCHAR(100) UNIQUE NOT NULL,
    is_regular BOOLEAN DEFAULT FALSE
);

CREATE TABLE Routes (
    id SERIAL PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    start_point VARCHAR(100) NOT NULL,
    end_point VARCHAR(100) NOT NULL,
    price DECIMAL(10, 2) NOT NULL
);

CREATE TABLE Transport (
    id SERIAL PRIMARY KEY,
    type VARCHAR(50) NOT NULL,
    capacity DECIMAL(10, 2) NOT NULL,
    status VARCHAR(50) DEFAULT 'free'
);

CREATE TABLE RouteTransport (
    route_id INT REFERENCES Routes(id),
    transport_id INT REFERENCES Transport(id),
    departure_time TIMESTAMP,
    arrival_time TIMESTAMP,
    PRIMARY KEY (route_id, transport_id)
);

CREATE TABLE Cargo (
    id SERIAL PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    weight DECIMAL(10, 2) NOT NULL,
    route_id INT REFERENCES Routes(id)
);

CREATE TABLE Orders (
    id SERIAL PRIMARY KEY,
    client_id INT REFERENCES Clients(id),
    cargo_id INT REFERENCES Cargo(id),
    total_cost DECIMAL(10, 2) NOT NULL
);