CREATE TABLE notifications
(
    id         BIGINT PRIMARY KEY,
    user_id    BIGINT       NOT NULL,
    type       VARCHAR(20)  NOT NULL,
    title      VARCHAR(200) NOT NULL,
    content    TEXT         NOT NULL,
    read       BOOLEAN      NOT NULL DEFAULT FALSE,
    link       VARCHAR(500),
    created_at TIMESTAMPTZ  NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ  NOT NULL DEFAULT NOW(),

    CONSTRAINT chk_notification_type CHECK (type IN ('order', 'community', 'system'))
);

CREATE INDEX idx_notifications_user_created ON notifications (user_id, created_at DESC);
CREATE INDEX idx_notifications_user_read_created ON notifications (user_id, read, created_at DESC);
CREATE INDEX idx_notifications_user_type_created ON notifications (user_id, type, created_at DESC);

CREATE TRIGGER trigger_notifications_updated_at
    BEFORE UPDATE
    ON notifications
    FOR EACH ROW
    EXECUTE FUNCTION update_updated_at_column();
