CREATE TABLE hotel
(
    id      SERIAL PRIMARY KEY,
    name    VARCHAR(255) NOT NULL,
    address VARCHAR(255) NOT NULL
);

CREATE TABLE room
(
    id       INTEGER PRIMARY KEY,
    hotel_id INTEGER NOT NULL,
    beds     INTEGER NOT NULL CHECK (beds > 0),
    size     INTEGER NOT NULL CHECK (size > 0),
    FOREIGN KEY (hotel_id) REFERENCES hotel (id)
);

CREATE TABLE "user"
(
    email VARCHAR(255) PRIMARY KEY,
    hash  VARCHAR(255) NOT NULL,
    salt  VARCHAR(255) NOT NULL,
    role  SMALLINT     NOT NULL CHECK ( role IN (0, 3) )
);

CREATE TABLE reservation
(
    id         SERIAL PRIMARY KEY,
    room_id    INTEGER      NOT NULL,
    start      TIMESTAMP    NOT NULL,
    "end"      TIMESTAMP    NOT NULL,
    "user"     VARCHAR(255) NOT NULL,
    checked_in BOOLEAN      NOT NULL DEFAULT FALSE,
    FOREIGN KEY (room_id) REFERENCES room (id),
    FOREIGN KEY ("user") REFERENCES "user" (email)
);

CREATE TABLE task
(
    id          SERIAL PRIMARY KEY,
    room_id     INTEGER     NOT NULL,
    description TEXT        NOT NULL,
    status      VARCHAR(12) NOT NULL CHECK ( status IN ('todo', 'in_progress', 'done') ),
    FOREIGN KEY (room_id) REFERENCES room (id)
);

CREATE TABLE session
(
    id          VARCHAR(128) PRIMARY KEY,
    data        JSONB     NOT NULL,
    expiry_date TIMESTAMP NOT NULL
)
