-- 2026_06_25_001_share_events.sql
-- 分享事件追踪表（spec: docs/superpowers/specs/2026-06-11-share-modal-redesign-design.md § 3.5）

CREATE TABLE IF NOT EXISTS public.share_events (
    id              BIGSERIAL PRIMARY KEY,
    asset_id        BIGINT          NOT NULL,
    sharer_user_id  BIGINT          NOT NULL,
    system_type     VARCHAR(32)     NOT NULL,
    share_target    VARCHAR(32)     NOT NULL,
    result          VARCHAR(32)     NOT NULL,
    client_ts       BIGINT          NOT NULL,
    server_ts       BIGINT          NOT NULL DEFAULT (EXTRACT(EPOCH FROM NOW()) * 1000)::BIGINT,
    extra           JSONB           DEFAULT '{}'::jsonb,

    CONSTRAINT fk_share_events_sharer FOREIGN KEY (sharer_user_id)
        REFERENCES public.users(id) ON DELETE RESTRICT
);

CREATE INDEX IF NOT EXISTS idx_share_events_asset_id    ON public.share_events (asset_id);
CREATE INDEX IF NOT EXISTS idx_share_events_sharer      ON public.share_events (sharer_user_id);
CREATE INDEX IF NOT EXISTS idx_share_events_system_type ON public.share_events (system_type);
CREATE INDEX IF NOT EXISTS idx_share_events_server_ts   ON public.share_events (server_ts DESC);

-- 序列起始值预留（按 CLAUDE.md 数据库规范）
ALTER SEQUENCE public.share_events_id_seq RESTART WITH 10000;