-- 创建公共 Outbox；该表及全部公共索引、约束只由 tech-public-foundation 维护。
CREATE TABLE tb_outbox_event (
    id bigserial PRIMARY KEY,
    event_id varchar(64) NOT NULL,
    event_type varchar(150) NOT NULL,
    payload_version integer NOT NULL DEFAULT 1,
    aggregate_type varchar(100) NOT NULL,
    aggregate_id varchar(100) NOT NULL,
    resource_type varchar(100) NOT NULL,
    resource_id varchar(100) NOT NULL,
    business_key varchar(150) NOT NULL DEFAULT '',
    request_id varchar(100) NOT NULL DEFAULT '',
    correlation_id varchar(100) NOT NULL DEFAULT '',
    payload jsonb NOT NULL,
    status integer NOT NULL DEFAULT 1,
    retry_count integer NOT NULL DEFAULT 0,
    max_retries integer NOT NULL DEFAULT 10,
    next_attempt_at timestamptz NOT NULL DEFAULT NOW(),
    lease_owner varchar(100),
    lease_expires_at timestamptz,
    last_error_code varchar(100) NOT NULL DEFAULT '',
    last_error_summary varchar(500) NOT NULL DEFAULT '',
    delivered_at timestamptz,
    created_at timestamptz NOT NULL DEFAULT NOW(),
    updated_at timestamptz NOT NULL DEFAULT NOW(),
    CONSTRAINT uq_outbox_event_id UNIQUE (event_id),
    CONSTRAINT ck_outbox_event_status CHECK (status IN (1, 2, 3, 4)),
    CONSTRAINT ck_outbox_event_payload_version CHECK (payload_version > 0),
    CONSTRAINT ck_outbox_event_retry_count CHECK (retry_count >= 0 AND max_retries > 0)
);

CREATE INDEX idx_outbox_event_claim
    ON tb_outbox_event (status, next_attempt_at, lease_expires_at, created_at);
CREATE INDEX idx_outbox_event_type_status
    ON tb_outbox_event (event_type, status);
CREATE INDEX idx_outbox_event_aggregate
    ON tb_outbox_event (aggregate_type, aggregate_id, created_at);
CREATE INDEX idx_outbox_event_resource
    ON tb_outbox_event (resource_type, resource_id, created_at);
CREATE INDEX idx_outbox_event_request_id ON tb_outbox_event (request_id) WHERE request_id <> '';
CREATE INDEX idx_outbox_event_correlation_id ON tb_outbox_event (correlation_id) WHERE correlation_id <> '';

COMMENT ON TABLE tb_outbox_event IS '公共可靠事件 Outbox';
COMMENT ON COLUMN tb_outbox_event.id IS 'Outbox记录主键ID';
COMMENT ON COLUMN tb_outbox_event.event_id IS '跨重试保持稳定的事件唯一ID';
COMMENT ON COLUMN tb_outbox_event.event_type IS '稳定事件类型编码';
COMMENT ON COLUMN tb_outbox_event.payload_version IS '事件载荷结构版本';
COMMENT ON COLUMN tb_outbox_event.aggregate_type IS '产生事件的聚合或业务事实类型';
COMMENT ON COLUMN tb_outbox_event.aggregate_id IS '产生事件的聚合或业务事实ID';
COMMENT ON COLUMN tb_outbox_event.resource_type IS '事件主要资源类型';
COMMENT ON COLUMN tb_outbox_event.resource_id IS '事件主要资源ID';
COMMENT ON COLUMN tb_outbox_event.business_key IS '业务幂等键，空字符串表示未提供';
COMMENT ON COLUMN tb_outbox_event.request_id IS '来源HTTP请求ID，空字符串表示无请求上下文';
COMMENT ON COLUMN tb_outbox_event.correlation_id IS '跨事务和异步链路关联ID';
COMMENT ON COLUMN tb_outbox_event.payload IS '结构化事件载荷快照';
COMMENT ON COLUMN tb_outbox_event.status IS '投递状态 1=待投递, 2=投递中, 3=已投递, 4=投递失败';
COMMENT ON COLUMN tb_outbox_event.retry_count IS '已发生的投递重试次数';
COMMENT ON COLUMN tb_outbox_event.max_retries IS '允许的最大投递重试次数';
COMMENT ON COLUMN tb_outbox_event.next_attempt_at IS '下一次允许领取投递的时间';
COMMENT ON COLUMN tb_outbox_event.lease_owner IS '当前Relay投递租约持有者';
COMMENT ON COLUMN tb_outbox_event.lease_expires_at IS '当前Relay投递租约过期时间';
COMMENT ON COLUMN tb_outbox_event.last_error_code IS '最近一次投递失败的稳定错误编码';
COMMENT ON COLUMN tb_outbox_event.last_error_summary IS '最近一次投递失败的安全摘要';
COMMENT ON COLUMN tb_outbox_event.delivered_at IS '事件成功提交到任务队列的时间';
COMMENT ON COLUMN tb_outbox_event.created_at IS 'Outbox记录创建时间';
COMMENT ON COLUMN tb_outbox_event.updated_at IS 'Outbox记录更新时间';
