-- =============================================
-- 数据引擎初始化数据脚本
-- 执行前请先执行 init_core_tables.sql
-- =============================================

-- =============================================
-- 1. 创建腾讯广告平台
--    认证方式：OAuth2，access_token 放在 URL 查询参数
--    API Base: https://api.e.qq.com/v3.0
-- =============================================
INSERT INTO api_datasource_platform (
    tenant_id, creator, created_at, updater, updated_at,
    platform_code, platform_name, description, status,
    api_base_url, auth_type,
    token, client_id, client_secret,
    auth_config,
    rate_limit_per_minute, rate_limit_per_hour,
    concurrency_limit, request_timeout_ms, max_retries, retry_delay_ms
) VALUES (
    1, 'admin', NOW(), 'admin', NOW(),
    'tencent', '腾讯广告', '腾讯广告（广点通）数据同步', 'ACTIVE',
    'https://api.e.qq.com/v3.0', 'OAUTH2',
    '4bacfc7c9b0a31f70ec0eb4771f8b542',
    '1112038234', 'GxyjXFbZAs5dnsNQ',
    '{
        "token_in_query": true,
        "query_key": "access_token",
        "refresh_token": "d15b37363a42449026d337708516e95e",
        "extra_query_params": {
            "timestamp": "{timestamp}",
            "nonce": "{nonce}"
        }
    }'::jsonb,
    60, 3600, 10, 30000, 3, 1000
);

-- =============================================
-- 2. 创建腾讯广告的接口配置
--    接口前缀：https://api.e.qq.com/v3.0
-- =============================================

-- 2.1 账户关系接口（先获取所有账户ID）
-- 出参：{"code":0, "message":"ok", "data":{"list":[...], "page_info":{...}}}
-- 列表路径：data.list，分页：data.page_info.total_page
INSERT INTO api_interface (
    tenant_id, creator, created_at, updater, updated_at,
    platform_id, name, code, url, method, status, auth_type,
    request_config, response_config, table_definition
) VALUES (
    1, 'admin', NOW(), 'admin', NOW(),
    (SELECT id FROM api_datasource_platform WHERE platform_code = 'tencent'),
    '账户列表', 'account_relation',
    '/advertiser/get', 'GET', 'active', 'inherit',
    '{
        "parameters_location": "query",
        "page": 1,
        "page_size": 100,
        "page_param": "page",
        "page_size_param": "page_size",
        "pagination_mode": "PAGINATION_MODE_NORMAL",
        "fields": ["account_id", "corporation_name", "is_adx", "is_bid", "is_mp"]
    }'::jsonb,
    '{
        "success_field": "code",
        "success_value": 0,
        "message_field": "message",
        "list_path": "data.list"
    }'::jsonb,
    '{
        "table_name": "tencent_account_relation",
        "columns": [
            {"name": "account_id", "type": "BIGINT", "comment": "账户ID"},
            {"name": "corporation_name", "type": "VARCHAR(300)", "comment": "企业名称"},
            {"name": "is_adx", "type": "BOOLEAN", "comment": "是否ADX账户"},
            {"name": "is_bid", "type": "BOOLEAN", "comment": "是否竞价账户"},
            {"name": "is_mp", "type": "BOOLEAN", "comment": "是否MP账户"}
        ],
        "conflict_keys": ["account_id"]
    }'::jsonb
);

-- 2.2 图片素材接口（遍历每个账户拉取图片）
-- 出参：{"code":0, "message":"ok", "data":{"list":[...], "page_info":{...}}}
-- 列表路径：data.list，分页：data.page_info.total_page
INSERT INTO api_interface (
    tenant_id, creator, created_at, updater, updated_at,
    platform_id, name, code, url, method, status, auth_type,
    request_config, response_config, table_definition
) VALUES (
    1, 'admin', NOW(), 'admin', NOW(),
    (SELECT id FROM api_datasource_platform WHERE platform_code = 'tencent'),
    '图片素材', 'image',
    '/images/get', 'GET', 'active', 'inherit',
    '{
        "parameters_location": "query",
        "page": 1,
        "page_size": 100,
        "page_param": "page",
        "page_size_param": "page_size",
        "time_field": "last_modified_time",
        "prefetch": {
            "url": "/advertiser/get",
            "method": "GET",
            "response_path": "data.list",
            "target_param": "account_id",
            "value_field": "account_id"
        }
    }'::jsonb,
    '{
        "success_field": "code",
        "success_value": 0,
        "message_field": "message",
        "list_path": "data.list"
    }'::jsonb,
    '{
        "table_name": "tencent_image",
        "columns": [
            {"name": "image_id", "type": "VARCHAR(100)", "comment": "图片ID"},
            {"name": "account_id", "type": "BIGINT", "comment": "账户ID"},
            {"name": "width", "type": "INT", "comment": "宽度"},
            {"name": "height", "type": "INT", "comment": "高度"},
            {"name": "file_size", "type": "BIGINT", "comment": "文件大小"},
            {"name": "type", "type": "VARCHAR(50)", "comment": "图片类型"},
            {"name": "signature", "type": "VARCHAR(200)", "comment": "签名"},
            {"name": "description", "type": "TEXT", "comment": "描述"},
            {"name": "source_signature", "type": "VARCHAR(200)", "comment": "源签名"},
            {"name": "preview_url", "type": "TEXT", "comment": "预览URL"},
            {"name": "thumb_preview_url", "type": "TEXT", "comment": "缩略图URL"},
            {"name": "source_type", "type": "VARCHAR(50)", "comment": "来源类型"},
            {"name": "image_usage", "type": "VARCHAR(50)", "comment": "图片用途"},
            {"name": "created_time", "type": "BIGINT", "comment": "创建时间戳"},
            {"name": "last_modified_time", "type": "BIGINT", "comment": "最后修改时间戳"},
            {"name": "status", "type": "VARCHAR(50)", "comment": "状态"},
            {"name": "owner_account_id", "type": "VARCHAR(100)", "comment": "所有者账户ID"},
            {"name": "first_publication_status", "type": "VARCHAR(50)", "comment": "首次发布状态"},
            {"name": "quality_status", "type": "VARCHAR(50)", "comment": "质量状态"},
            {"name": "aigc_flag", "type": "VARCHAR(50)", "comment": "AIGC标志"},
            {"name": "aigc_type", "type": "INT", "comment": "AIGC类型"},
            {"name": "verify_status", "type": "VARCHAR(50)", "comment": "校验状态"},
            {"name": "verified_at", "type": "TIMESTAMP WITH TIME ZONE", "comment": "校验时间"},
            {"name": "verified_by", "type": "VARCHAR(64)", "comment": "校验人"}
        ],
        "conflict_keys": ["image_id", "account_id"]
    }'::jsonb
);

-- 2.3 视频素材接口（遍历每个账户拉取视频）
-- 出参：{"code":0, "message":"ok", "data":{"list":[...], "page_info":{...}}}
-- 列表路径：data.list，分页：data.page_info.total_page
INSERT INTO api_interface (
    tenant_id, creator, created_at, updater, updated_at,
    platform_id, name, code, url, method, status, auth_type,
    request_config, response_config, table_definition
) VALUES (
    1, 'admin', NOW(), 'admin', NOW(),
    (SELECT id FROM api_datasource_platform WHERE platform_code = 'tencent'),
    '视频素材', 'video',
    '/videos/get', 'GET', 'active', 'inherit',
    '{
        "parameters_location": "query",
        "page": 1,
        "page_size": 100,
        "page_param": "page",
        "page_size_param": "page_size",
        "time_field": "last_modified_time",
        "prefetch": {
            "url": "/advertiser/get",
            "method": "GET",
            "response_path": "data.list",
            "target_param": "account_id",
            "value_field": "account_id"
        }
    }'::jsonb,
    '{
        "success_field": "code",
        "success_value": 0,
        "message_field": "message",
        "list_path": "data.list"
    }'::jsonb,
    '{
        "table_name": "tencent_video",
        "columns": [
            {"name": "video_id", "type": "BIGINT", "comment": "视频ID"},
            {"name": "account_id", "type": "BIGINT", "comment": "账户ID"},
            {"name": "width", "type": "INT", "comment": "宽度"},
            {"name": "height", "type": "INT", "comment": "高度"},
            {"name": "video_frames", "type": "INT", "comment": "视频帧数"},
            {"name": "video_fps", "type": "DECIMAL(10,2)", "comment": "视频帧率"},
            {"name": "video_codec", "type": "VARCHAR(50)", "comment": "视频编码"},
            {"name": "video_bit_rate", "type": "BIGINT", "comment": "视频比特率"},
            {"name": "audio_codec", "type": "VARCHAR(50)", "comment": "音频编码"},
            {"name": "audio_bit_rate", "type": "BIGINT", "comment": "音频比特率"},
            {"name": "file_size", "type": "BIGINT", "comment": "文件大小"},
            {"name": "type", "type": "VARCHAR(50)", "comment": "视频类型"},
            {"name": "signature", "type": "VARCHAR(200)", "comment": "签名"},
            {"name": "system_status", "type": "VARCHAR(50)", "comment": "系统状态"},
            {"name": "description", "type": "TEXT", "comment": "描述"},
            {"name": "preview_url", "type": "TEXT", "comment": "预览URL"},
            {"name": "key_frame_image_url", "type": "TEXT", "comment": "关键帧图片URL"},
            {"name": "created_time", "type": "BIGINT", "comment": "创建时间戳"},
            {"name": "last_modified_time", "type": "BIGINT", "comment": "最后修改时间戳"},
            {"name": "status", "type": "VARCHAR(50)", "comment": "状态"},
            {"name": "owner_account_id", "type": "VARCHAR(100)", "comment": "所有者账户ID"},
            {"name": "cover_id", "type": "VARCHAR(100)", "comment": "封面ID"},
            {"name": "first_publication_status", "type": "VARCHAR(50)", "comment": "首次发布状态"},
            {"name": "quality_status", "type": "VARCHAR(50)", "comment": "质量状态"},
            {"name": "aigc_flag", "type": "VARCHAR(50)", "comment": "AIGC标志"},
            {"name": "muse_aigc_version", "type": "INT", "comment": "Muse AIGC版本"},
            {"name": "verify_status", "type": "VARCHAR(50)", "comment": "校验状态"},
            {"name": "verified_at", "type": "TIMESTAMP WITH TIME ZONE", "comment": "校验时间"},
            {"name": "verified_by", "type": "VARCHAR(64)", "comment": "校验人"}
        ],
        "conflict_keys": ["video_id", "account_id"]
    }'::jsonb
);

-- 2.4 音频素材接口（POST + JSON Body，无需遍历账户）
-- 注意：此接口不依赖 account_id，不依赖 prefetch，不支持增量
-- 出参：{"code":0, "message":"ok", "data":{"list":[...], "page_info":{...}}}
-- 列表路径：data.list，分页：data.page_info.total_page
INSERT INTO api_interface (
    tenant_id, creator, created_at, updater, updated_at,
    platform_id, name, code, url, method, status, auth_type,
    request_config, response_config, table_definition
) VALUES (
    1, 'admin', NOW(), 'admin', NOW(),
    (SELECT id FROM api_datasource_platform WHERE platform_code = 'tencent'),
    '音频素材', 'audio',
    '/muse_audios/get', 'POST', 'active', 'inherit',
    '{
        "page": 1,
        "page_size": 100,
        "page_param": "page",
        "page_size_param": "page_size",
        "fields": ["audio_id", "cover_image_url", "audio_name", "author", "duration", "expire_time", "feel_tags", "genre_tags"]
    }'::jsonb,
    '{
        "success_field": "code",
        "success_value": 0,
        "message_field": "message",
        "list_path": "data.list"
    }'::jsonb,
    '{
        "table_name": "tencent_audio",
        "columns": [
            {"name": "audio_id", "type": "VARCHAR(100)", "comment": "音乐ID"},
            {"name": "audio_name", "type": "VARCHAR(500)", "comment": "音乐名称"},
            {"name": "author", "type": "VARCHAR(200)", "comment": "作者"},
            {"name": "duration", "type": "DECIMAL(10,2)", "comment": "时长(秒)"},
            {"name": "cover_image_url", "type": "TEXT", "comment": "封面图片URL"},
            {"name": "expire_time", "type": "BIGINT", "comment": "过期时间戳"},
            {"name": "feel_tags", "type": "JSONB", "comment": "情感标签"},
            {"name": "genre_tags", "type": "JSONB", "comment": "风格标签"},
            {"name": "verify_status", "type": "VARCHAR(50)", "comment": "校验状态"},
            {"name": "verified_at", "type": "TIMESTAMP WITH TIME ZONE", "comment": "校验时间"},
            {"name": "verified_by", "type": "VARCHAR(64)", "comment": "校验人"}
        ],
        "conflict_keys": ["audio_id"]
    }'::jsonb
);
