-- 创建外部集成尝试权威记录；不建立外键，业务关系通过稳定 ID 显式维护。
CREATE TABLE tb_integration_log (
    id bigserial PRIMARY KEY,
    integration_id varchar(64) NOT NULL,
    idempotency_key varchar(160),
    provider varchar(32) NOT NULL,
    direction varchar(16) NOT NULL,
    operation varchar(64) NOT NULL,
    external_id varchar(128),
    resource_type varchar(64),
    resource_id varchar(128),
    resource_key varchar(128),
    trigger_source varchar(32),
    trigger_scene varchar(128),
    trigger_series varchar(64),
    scheduled_at timestamptz,
    started_at timestamptz,
    attempt integer NOT NULL DEFAULT 1,
    result varchar(20) NOT NULL,
    http_status integer,
    provider_code varchar(64),
    provider_message varchar(500),
    request_summary jsonb,
    response_summary jsonb,
    content_hash varchar(64) NOT NULL DEFAULT '',
    duration_ms bigint NOT NULL DEFAULT 0,
    state_changed boolean NOT NULL DEFAULT false,
    metadata jsonb,
    recovery_strategy varchar(255),
    request_id varchar(64),
    correlation_id varchar(64),
    audit_event_id bigint,
    created_at timestamptz NOT NULL DEFAULT NOW(),
    updated_at timestamptz NOT NULL DEFAULT NOW(),
    CONSTRAINT uq_integration_log_id UNIQUE (integration_id),
    CONSTRAINT uq_integration_log_idempotency UNIQUE (provider, operation, idempotency_key),
    CONSTRAINT ck_integration_log_direction CHECK (direction IN ('inbound', 'outbound')),
    CONSTRAINT ck_integration_log_result CHECK (result IN (
        'pending', 'success', 'failed', 'unknown', 'not_found', 'invalid_payload',
        'ignored', 'merged', 'rate_limited', 'completed', 'cancelled'
    )),
    CONSTRAINT ck_integration_log_attempt CHECK (attempt > 0),
    CONSTRAINT ck_integration_log_duration CHECK (duration_ms >= 0)
);

CREATE INDEX idx_integration_log_time ON tb_integration_log (created_at DESC, id DESC);
CREATE INDEX idx_integration_log_provider ON tb_integration_log (provider, operation, result, created_at DESC);
CREATE INDEX idx_integration_log_resource ON tb_integration_log (resource_type, resource_id, created_at DESC);
CREATE INDEX idx_integration_log_resource_key ON tb_integration_log (resource_type, resource_key, created_at DESC);
CREATE INDEX idx_integration_log_trigger ON tb_integration_log (trigger_series, attempt);
CREATE INDEX idx_integration_log_request ON tb_integration_log (request_id, created_at ASC) WHERE request_id IS NOT NULL;
CREATE INDEX idx_integration_log_correlation ON tb_integration_log (correlation_id, created_at ASC) WHERE correlation_id IS NOT NULL;

COMMENT ON TABLE tb_integration_log IS '外部集成调用、回调及未发送尝试记录';
COMMENT ON COLUMN tb_integration_log.id IS '外部集成记录主键ID';
COMMENT ON COLUMN tb_integration_log.integration_id IS '跨重试保持稳定的外部集成记录ID';
COMMENT ON COLUMN tb_integration_log.idempotency_key IS '外部操作业务幂等键，空值表示该操作无幂等键';
COMMENT ON COLUMN tb_integration_log.provider IS '外部服务提供方稳定编码';
COMMENT ON COLUMN tb_integration_log.direction IS '交互方向：inbound入站，outbound出站';
COMMENT ON COLUMN tb_integration_log.operation IS '外部接口或回调操作稳定编码';
COMMENT ON COLUMN tb_integration_log.external_id IS '外部系统返回的业务或请求标识';
COMMENT ON COLUMN tb_integration_log.resource_type IS '本地主要业务资源类型';
COMMENT ON COLUMN tb_integration_log.resource_id IS '本地主要业务资源ID';
COMMENT ON COLUMN tb_integration_log.resource_key IS '本地主要业务资源稳定键';
COMMENT ON COLUMN tb_integration_log.trigger_source IS '触发来源，如接口、回调、轮询或人工恢复';
COMMENT ON COLUMN tb_integration_log.trigger_scene IS '触发外部交互的业务场景';
COMMENT ON COLUMN tb_integration_log.trigger_series IS '同一计划或补偿序列的稳定标识';
COMMENT ON COLUMN tb_integration_log.scheduled_at IS '计划执行外部交互的时间';
COMMENT ON COLUMN tb_integration_log.started_at IS '实际开始外部交互的时间';
COMMENT ON COLUMN tb_integration_log.attempt IS '同一幂等操作的技术尝试序号';
COMMENT ON COLUMN tb_integration_log.result IS '尝试结果，pending 仅为内部执行态，其余为公开终态';
COMMENT ON COLUMN tb_integration_log.http_status IS '外部HTTP响应状态码';
COMMENT ON COLUMN tb_integration_log.provider_code IS '外部服务返回的稳定结果码';
COMMENT ON COLUMN tb_integration_log.provider_message IS '脱敏截断后的外部结果摘要';
COMMENT ON COLUMN tb_integration_log.request_summary IS '删除敏感信息后的请求摘要';
COMMENT ON COLUMN tb_integration_log.response_summary IS '删除敏感信息后的响应摘要';
COMMENT ON COLUMN tb_integration_log.content_hash IS '请求或回调正文的安全摘要';
COMMENT ON COLUMN tb_integration_log.duration_ms IS '本次外部交互耗时毫秒数';
COMMENT ON COLUMN tb_integration_log.state_changed IS '本次交互是否导致本地业务状态变化';
COMMENT ON COLUMN tb_integration_log.metadata IS '不含敏感值的结构化扩展元数据';
COMMENT ON COLUMN tb_integration_log.recovery_strategy IS '结果未知或失败后的恢复策略摘要';
COMMENT ON COLUMN tb_integration_log.request_id IS '来源HTTP请求ID';
COMMENT ON COLUMN tb_integration_log.correlation_id IS '跨事务和异步链路关联ID';
COMMENT ON COLUMN tb_integration_log.audit_event_id IS '预留的审计事件ID，仅代码显式维护且不建立外键';
COMMENT ON COLUMN tb_integration_log.created_at IS '外部集成记录创建时间';
COMMENT ON COLUMN tb_integration_log.updated_at IS '外部集成记录更新时间';
