-- Store carts in database

DROP TABLE IF EXISTS cart_cookies, cart_users, cart_sessions;

CREATE TABLE cart_sessions (
    id               INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    market_locale_id INTEGER NOT NULL REFERENCES market_locales (id),
    public_id        TEXT NOT NULL,
    active           BOOLEAN NOT NULL DEFAULT TRUE,
    content          TEXT,
    revision         INTEGER NOT NULL DEFAULT 0,
    inactive_reason  TEXT,
    created          timestamp with time zone NOT NULL DEFAULT now(),
    last_modified    timestamp with time zone NOT NULL DEFAULT now()
);
CREATE UNIQUE INDEX ON cart_sessions (public_id);

-- last_modified reflects the last user interaction/modification of the cart.
-- It it not auto updated via trigger, to avoid having it update on non-user interactions, like us expiring the cart.
COMMENT ON COLUMN cart_sessions.last_modified IS 'Last update of cart contents';
