"""获取一页作者作品并自动保存。"""
from __future__ import annotations

import asyncio

import httpx as httpx

from agents.find_agent.support.douyin_user_videos import (
    DEFAULT_MIN_DURATION_SECONDS,
    _douyin_user_videos_raw,
    _wait_rate_limit as _wait_rate_limit,
)
from agents.find_agent.support.search_persistence import persist_search_payload
from supply_agent.tools import tool


@tool
async def douyin_user_videos(
    run_id: str,
    account_id: str,
    query_reason: str,
    source_value: str | None = None,
    parent_search_id: int | None = None,
    page_no: int = 1,
    sort_type: str = "最热",
    cursor: str = "",
    min_duration_seconds: int = DEFAULT_MIN_DURATION_SECONDS,
    timeout: float | None = None,
) -> str:
    """获取一页作者作品，创建搜索记录和候选记录并返回对应数据库 ID。"""
    result = await _douyin_user_videos_raw(
        account_id=account_id,
        sort_type=sort_type,
        cursor=cursor,
        min_duration_seconds=min_duration_seconds,
        timeout=timeout,
    )
    return await asyncio.to_thread(
        persist_search_payload,
        result,
        run_id=run_id,
        keyword=f"author:{account_id}",
        query_reason=query_reason,
        source_type="author",
        source_value=source_value or account_id,
        parent_search_id=parent_search_id,
        cursor=cursor,
        page_no=page_no,
        provider="internal_blogger",
        sort_type=sort_type,
    )
