CREATE TABLE media_platforms (
    id            BIGSERIAL PRIMARY KEY,
    platform_id   VARCHAR(64) NOT NULL,
    name          VARCHAR(100) NOT NULL,
    category      VARCHAR(32) NOT NULL DEFAULT 'ugc',
    short_name    VARCHAR(16) NOT NULL,
    accent_color  VARCHAR(16) NOT NULL DEFAULT '#1677ff',
    login_url     TEXT,
    logo_url      TEXT,
    status        VARCHAR(20) NOT NULL DEFAULT 'active',
    sort_order    INT NOT NULL DEFAULT 0,
    created_at    TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at    TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE UNIQUE INDEX uk_media_platforms_platform_id ON media_platforms(platform_id);
CREATE INDEX idx_media_platforms_status_sort ON media_platforms(status, sort_order, id);

INSERT INTO media_platforms (platform_id, name, category, short_name, accent_color, login_url, sort_order)
VALUES
    ('toutiaohao', '头条号', 'news', '头', '#f5222d', 'https://mp.toutiao.com/auth/page/login', 10),
    ('baijiahao', '百家号', 'news', '百', '#31445a', 'https://baijiahao.baidu.com/builder/theme/bjh/login', 20),
    ('sohuhao', '搜狐号', 'news', '搜', '#fa8c16', 'https://mp.sohu.com/mpfe/v4/login', 30),
    ('qiehao', '企鹅号', 'social', 'Q', '#111827', 'https://om.qq.com', 40),
    ('zhihu', '知乎', 'social', '知', '#1677ff', 'https://www.zhihu.com/signin', 50),
    ('wangyihao', '网易号', 'news', '网', '#ff4d4f', 'https://mp.163.com/login.html', 60),
    ('jianshu', '简书', 'social', '简', '#f56a5e', 'https://www.jianshu.com/sign_in', 70),
    ('bilibili', 'bilibili', 'video', 'B', '#eb2f96', 'https://www.bilibili.com', 80),
    ('juejin', '稀土掘金', 'social', '掘', '#1677ff', 'https://juejin.cn/login', 90),
    ('smzdm', '什么值得买', 'social', '值', '#f5222d', 'https://zhiyou.smzdm.com/user/login', 100),
    ('weixin_gzh', '微信公众号', 'social', '微', '#13c26b', 'https://mp.weixin.qq.com/cgi-bin/loginpage', 110),
    ('zol', '中关村在线', 'news', 'Z', '#ff4d4f', 'https://post.zol.com.cn/v2/manage/works/all', 120),
    ('dongchedi', '懂车帝', 'news', '懂', '#fadb14', 'https://mp.dcdapp.com/login', 130);

CREATE TABLE platform_accounts (
    id             BIGSERIAL PRIMARY KEY,
    tenant_id      BIGINT NOT NULL REFERENCES tenants(id),
    user_id        BIGINT NOT NULL REFERENCES users(id),
    platform_id    VARCHAR(64) NOT NULL REFERENCES media_platforms(platform_id),
    platform_uid   VARCHAR(128) NOT NULL,
    nickname       VARCHAR(255) NOT NULL,
    avatar_url     TEXT,
    status         VARCHAR(20) NOT NULL DEFAULT 'active',
    metadata_json  JSONB,
    last_check_at  TIMESTAMPTZ,
    created_at     TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at     TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    deleted_at     TIMESTAMPTZ
);

CREATE INDEX idx_platform_accounts_tenant_platform ON platform_accounts(tenant_id, platform_id, status, created_at DESC);

CREATE TABLE publish_batches (
    id                 BIGSERIAL PRIMARY KEY,
    tenant_id          BIGINT NOT NULL REFERENCES tenants(id),
    article_id         BIGINT NOT NULL REFERENCES articles(id),
    initiator_user_id  BIGINT NOT NULL REFERENCES users(id),
    status             VARCHAR(20) NOT NULL DEFAULT 'pending',
    publish_type       VARCHAR(20) NOT NULL DEFAULT 'publish',
    cover_asset_url    TEXT,
    created_at         TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at         TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX idx_publish_batches_article_created ON publish_batches(article_id, created_at DESC);
CREATE INDEX idx_publish_batches_tenant_status ON publish_batches(tenant_id, status, created_at DESC);

CREATE TABLE publish_records (
    id                    BIGSERIAL PRIMARY KEY,
    tenant_id             BIGINT NOT NULL REFERENCES tenants(id),
    publish_batch_id      BIGINT NOT NULL REFERENCES publish_batches(id) ON DELETE CASCADE,
    article_id            BIGINT NOT NULL REFERENCES articles(id),
    platform_account_id   BIGINT NOT NULL REFERENCES platform_accounts(id),
    platform_id           VARCHAR(64) NOT NULL REFERENCES media_platforms(platform_id),
    status                VARCHAR(20) NOT NULL DEFAULT 'queued',
    external_article_id   VARCHAR(128),
    external_article_url  TEXT,
    external_manage_url   TEXT,
    published_at          TIMESTAMPTZ,
    request_payload_json  JSONB,
    response_payload_json JSONB,
    error_message         TEXT,
    retry_count           INT NOT NULL DEFAULT 0,
    created_at            TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at            TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE UNIQUE INDEX uk_publish_records_batch_account ON publish_records(publish_batch_id, platform_account_id);
CREATE INDEX idx_publish_records_article_created ON publish_records(article_id, created_at DESC);
CREATE INDEX idx_publish_records_status ON publish_records(status, created_at DESC);
