-- =============================================
-- 数据引擎核心表初始化脚本
-- 只需执行此文件即可，其他表由系统自动创建
-- =============================================

-- =============================================
-- 1. 数据源平台配置表 (api_datasource_platform)
-- =============================================
CREATE SEQUENCE IF NOT EXISTS api_datasource_platform_id_seq START WITH 1 INCREMENT BY 1;

CREATE TABLE IF NOT EXISTS api_datasource_platform (
    id BIGINT NOT NULL DEFAULT nextval('api_datasource_platform_id_seq'::regclass),
    tenant_id BIGINT NOT NULL DEFAULT 0,
    creator VARCHAR(100) DEFAULT '',
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updater VARCHAR(100) DEFAULT '',
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP WITH TIME ZONE,
    platform_code VARCHAR(100) NOT NULL,
    platform_name VARCHAR(200) NOT NULL,
    description TEXT,
    status VARCHAR(50) DEFAULT 'ACTIVE',
    api_base_url VARCHAR(500),
    auth_type VARCHAR(50) DEFAULT 'API_KEY',
    token VARCHAR(500),
    api_key VARCHAR(500),
    client_id VARCHAR(200),
    client_secret VARCHAR(500),
    rate_limit_per_minute INT DEFAULT 60,
    rate_limit_per_hour INT DEFAULT 3600,
    concurrency_limit INT DEFAULT 10,
    request_timeout_ms INT DEFAULT 30000,
    max_retries INT DEFAULT 3,
    retry_delay_ms INT DEFAULT 1000,
    auth_config JSONB,
    version INT DEFAULT 0,
    PRIMARY KEY (id)
);

CREATE UNIQUE INDEX idx_api_datasource_platform_code ON api_datasource_platform(tenant_id, platform_code);

COMMENT ON TABLE api_datasource_platform IS '数据源平台配置表';
COMMENT ON COLUMN api_datasource_platform.id IS '主键ID，自增长';
COMMENT ON COLUMN api_datasource_platform.tenant_id IS '租户ID';
COMMENT ON COLUMN api_datasource_platform.creator IS '创建人';
COMMENT ON COLUMN api_datasource_platform.created_at IS '创建时间';
COMMENT ON COLUMN api_datasource_platform.updater IS '更新人';
COMMENT ON COLUMN api_datasource_platform.updated_at IS '更新时间';
COMMENT ON COLUMN api_datasource_platform.deleted_at IS '软删除时间';
COMMENT ON COLUMN api_datasource_platform.platform_code IS '平台编码，唯一标识';
COMMENT ON COLUMN api_datasource_platform.platform_name IS '平台名称';
COMMENT ON COLUMN api_datasource_platform.description IS '平台描述';
COMMENT ON COLUMN api_datasource_platform.status IS '状态: ACTIVE启用, INACTIVE停用';
COMMENT ON COLUMN api_datasource_platform.api_base_url IS 'API基础地址';
COMMENT ON COLUMN api_datasource_platform.auth_type IS '认证类型: TOKEN/API_KEY/OAUTH2/BASIC';
COMMENT ON COLUMN api_datasource_platform.token IS '认证token/密钥(access_token)';
COMMENT ON COLUMN api_datasource_platform.api_key IS 'API Key';
COMMENT ON COLUMN api_datasource_platform.client_id IS 'OAuth2 Client ID';
COMMENT ON COLUMN api_datasource_platform.client_secret IS 'OAuth2 Client Secret';
COMMENT ON COLUMN api_datasource_platform.rate_limit_per_minute IS '每分钟请求限制';
COMMENT ON COLUMN api_datasource_platform.rate_limit_per_hour IS '每小时请求限制';
COMMENT ON COLUMN api_datasource_platform.concurrency_limit IS '并发连接限制';
COMMENT ON COLUMN api_datasource_platform.request_timeout_ms IS '请求超时时间(毫秒)';
COMMENT ON COLUMN api_datasource_platform.max_retries IS '最大重试次数';
COMMENT ON COLUMN api_datasource_platform.retry_delay_ms IS '重试延迟(毫秒)';
COMMENT ON COLUMN api_datasource_platform.auth_config IS '自定义认证配置(JSONB)，支持各平台特有的认证方式';
COMMENT ON COLUMN api_datasource_platform.version IS '版本号(乐观锁)';

-- =============================================
-- 2. API接口管理表 (api_interface)
-- =============================================
CREATE SEQUENCE IF NOT EXISTS api_interface_id_seq START WITH 1 INCREMENT BY 1;

CREATE TABLE IF NOT EXISTS api_interface (
    id BIGINT NOT NULL DEFAULT nextval('api_interface_id_seq'::regclass),
    tenant_id BIGINT NOT NULL DEFAULT 0,
    creator VARCHAR(100) DEFAULT '',
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updater VARCHAR(100) DEFAULT '',
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP WITH TIME ZONE,
    platform_id BIGINT NOT NULL,
    name VARCHAR(200) NOT NULL,
    code VARCHAR(100) NOT NULL,
    url VARCHAR(500) NOT NULL,
    method VARCHAR(20) NOT NULL DEFAULT 'GET',
    status VARCHAR(50) DEFAULT 'active',
    auth_type VARCHAR(50) DEFAULT 'api_key',
    request_config JSONB,
    response_config JSONB,
    limit_config JSONB,
    table_definition JSONB,
    PRIMARY KEY (id)
);

CREATE UNIQUE INDEX idx_api_interface_code ON api_interface(tenant_id, code);
CREATE INDEX idx_api_interface_platform_id ON api_interface(tenant_id, platform_id);
CREATE INDEX idx_api_interface_status ON api_interface(tenant_id, status);

COMMENT ON TABLE api_interface IS 'API接口管理表';
COMMENT ON COLUMN api_interface.id IS '主键ID，自增长';
COMMENT ON COLUMN api_interface.tenant_id IS '租户ID';
COMMENT ON COLUMN api_interface.creator IS '创建人';
COMMENT ON COLUMN api_interface.created_at IS '创建时间';
COMMENT ON COLUMN api_interface.updater IS '更新人';
COMMENT ON COLUMN api_interface.updated_at IS '更新时间';
COMMENT ON COLUMN api_interface.deleted_at IS '软删除时间';
COMMENT ON COLUMN api_interface.platform_id IS '所属平台ID';
COMMENT ON COLUMN api_interface.name IS '接口名称';
COMMENT ON COLUMN api_interface.code IS '接口编码，唯一';
COMMENT ON COLUMN api_interface.url IS '接口地址(相对路径)';
COMMENT ON COLUMN api_interface.method IS '请求方法: GET/POST/PUT/DELETE';
COMMENT ON COLUMN api_interface.status IS '接口状态: active启用/inactive停用';
COMMENT ON COLUMN api_interface.auth_type IS '认证类型: inherit(继承平台)/oauth2/apikey/basic';
COMMENT ON COLUMN api_interface.request_config IS '请求配置(JSONB): 含分页参数、预取配置、时间字段等';
COMMENT ON COLUMN api_interface.response_config IS '响应配置(JSONB): 自定义数据路径等';
COMMENT ON COLUMN api_interface.limit_config IS '接口独立限流配置(JSONB): 可选，覆盖平台配置';
COMMENT ON COLUMN api_interface.table_definition IS '表结构定义(JSONB): 描述目标表的字段结构，用于自动建表';

-- =============================================
-- 3. 同步任务日志表 (sync_task_log)
--    用于记录同步状态和补偿机制
-- =============================================
CREATE SEQUENCE IF NOT EXISTS sync_task_log_id_seq;

CREATE TABLE IF NOT EXISTS sync_task_log (
    id BIGINT NOT NULL DEFAULT nextval('sync_task_log_id_seq'::regclass),
    tenant_id BIGINT NOT NULL DEFAULT 0,
    creator VARCHAR(64) DEFAULT '',
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updater VARCHAR(64) DEFAULT '',
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP WITH TIME ZONE,
    task_id VARCHAR(64) NOT NULL DEFAULT '',
    task_type VARCHAR(64) NOT NULL DEFAULT '',
    platform_code VARCHAR(100) NOT NULL DEFAULT '',
    interface_code VARCHAR(100) NOT NULL DEFAULT '',
    advertiser_id BIGINT NOT NULL DEFAULT 0,
    start_time TIMESTAMP WITH TIME ZONE,
    end_time TIMESTAMP WITH TIME ZONE,
    status VARCHAR(32) NOT NULL DEFAULT '',
    retry_count INT DEFAULT 0,
    max_retry INT DEFAULT 3,
    page_info JSONB,
    request_params JSONB,
    error_message TEXT DEFAULT '',
    error_code VARCHAR(64) DEFAULT '',
    result_summary JSONB,
    next_retry_time TIMESTAMP WITH TIME ZONE,
    completed_at TIMESTAMP WITH TIME ZONE,
    duration_ms BIGINT DEFAULT 0,
    PRIMARY KEY (id)
);

CREATE INDEX idx_sync_task_log_task_id ON sync_task_log(tenant_id, task_id);
CREATE INDEX idx_sync_task_log_platform ON sync_task_log(tenant_id, platform_code);
CREATE INDEX idx_sync_task_log_interface ON sync_task_log(tenant_id, interface_code);
CREATE INDEX idx_sync_task_log_status ON sync_task_log(tenant_id, status);
CREATE INDEX idx_sync_task_log_created_at ON sync_task_log(tenant_id, created_at);
CREATE INDEX idx_sync_task_log_next_retry ON sync_task_log(tenant_id, next_retry_time);

COMMENT ON TABLE sync_task_log IS '同步任务日志表，记录同步状态和补偿信息';
COMMENT ON COLUMN sync_task_log.id IS '主键ID，自增长';
COMMENT ON COLUMN sync_task_log.tenant_id IS '租户ID';
COMMENT ON COLUMN sync_task_log.creator IS '创建人';
COMMENT ON COLUMN sync_task_log.created_at IS '创建时间';
COMMENT ON COLUMN sync_task_log.updater IS '更新人';
COMMENT ON COLUMN sync_task_log.updated_at IS '更新时间';
COMMENT ON COLUMN sync_task_log.deleted_at IS '软删除时间';
COMMENT ON COLUMN sync_task_log.task_id IS '任务唯一标识';
COMMENT ON COLUMN sync_task_log.task_type IS '任务类型: full(全量)/incremental(增量)/compensate(补偿)';
COMMENT ON COLUMN sync_task_log.platform_code IS '平台编码';
COMMENT ON COLUMN sync_task_log.interface_code IS '接口编码';
COMMENT ON COLUMN sync_task_log.advertiser_id IS '广告主/账户ID';
COMMENT ON COLUMN sync_task_log.start_time IS '数据开始时间';
COMMENT ON COLUMN sync_task_log.end_time IS '数据结束时间';
COMMENT ON COLUMN sync_task_log.status IS '任务状态: pending/running/success/failed/retrying/manual_review';
COMMENT ON COLUMN sync_task_log.retry_count IS '已重试次数';
COMMENT ON COLUMN sync_task_log.max_retry IS '最大重试次数';
COMMENT ON COLUMN sync_task_log.page_info IS '分页信息快照(JSONB)';
COMMENT ON COLUMN sync_task_log.request_params IS '请求参数快照(JSONB)';
COMMENT ON COLUMN sync_task_log.error_message IS '错误信息';
COMMENT ON COLUMN sync_task_log.error_code IS '错误码';
COMMENT ON COLUMN sync_task_log.result_summary IS '结果摘要(JSONB)';
COMMENT ON COLUMN sync_task_log.next_retry_time IS '下次重试时间';
COMMENT ON COLUMN sync_task_log.completed_at IS '完成时间';
COMMENT ON COLUMN sync_task_log.duration_ms IS '执行耗时(毫秒)';

-- =============================================
-- 4. 同步跟踪表 (sync_tracker)
--    记录每个平台+接口的最后同步时间，用于增量同步
-- =============================================
CREATE SEQUENCE IF NOT EXISTS sync_tracker_id_seq;

CREATE TABLE IF NOT EXISTS sync_tracker (
    id BIGINT NOT NULL DEFAULT nextval('sync_tracker_id_seq'::regclass),
    tenant_id BIGINT NOT NULL DEFAULT 0,
    creator VARCHAR(64) DEFAULT '',
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updater VARCHAR(64) DEFAULT '',
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP WITH TIME ZONE,
    platform_code VARCHAR(100) NOT NULL,
    interface_code VARCHAR(100) NOT NULL,
    last_sync_time BIGINT NOT NULL DEFAULT 0,
    last_sync_at TIMESTAMP WITH TIME ZONE,
    sync_status VARCHAR(32) DEFAULT 'pending',
    PRIMARY KEY (id)
);

CREATE UNIQUE INDEX idx_sync_tracker_unique ON sync_tracker(platform_code, interface_code);

COMMENT ON TABLE sync_tracker IS '同步跟踪表，记录增量同步时间戳';
COMMENT ON COLUMN sync_tracker.id IS '主键ID，自增长';
COMMENT ON COLUMN sync_tracker.tenant_id IS '租户ID';
COMMENT ON COLUMN sync_tracker.creator IS '创建人';
COMMENT ON COLUMN sync_tracker.created_at IS '创建时间';
COMMENT ON COLUMN sync_tracker.updater IS '更新人';
COMMENT ON COLUMN sync_tracker.updated_at IS '更新时间';
COMMENT ON COLUMN sync_tracker.deleted_at IS '软删除时间';
COMMENT ON COLUMN sync_tracker.platform_code IS '平台编码';
COMMENT ON COLUMN sync_tracker.interface_code IS '接口编码';
COMMENT ON COLUMN sync_tracker.last_sync_time IS '最后同步时间(Unix时间戳)';
COMMENT ON COLUMN sync_tracker.last_sync_at IS '最后同步时间点';
COMMENT ON COLUMN sync_tracker.sync_status IS '同步状态: pending/success/running/failed';
