from __future__ import annotations

import asyncio

from loguru import logger

from vk_parser_app.constants import PUBLICATION_STATUS_PUBLISHED
from vk_parser_app.db import close_pool, fetch_int_setting, fetch_setting, get_pool
from vk_parser_app.workers.max_poster import MAXAPIClient, MAXAPIError, MAXPoster


async def main() -> None:
    pool = await get_pool()
    token = str(await fetch_setting("max_poster_bot_token", "") or "").strip()
    base_url = str(await fetch_setting("max_poster_api_base_url", "https://platform-api2.max.ru") or "").strip()
    if not token:
        raise RuntimeError("max_poster_bot_token is empty")

    rows = await pool.fetch(
        """
        SELECT id, raw_post_id, external_id
        FROM post_publications
        WHERE platform='max'
          AND status=$1
          AND COALESCE(url, '')=''
          AND COALESCE(external_id, '')<>''
        ORDER BY published_at DESC NULLS LAST, id DESC
        """,
        PUBLICATION_STATUS_PUBLISHED,
    )

    poster = MAXPoster()
    updated = 0
    missing = 0
    async with MAXAPIClient(
        token,
        base_url,
        timeout_sec=max(30, await fetch_int_setting("max_poster_timeout_sec", 120)),
        max_attempts=max(1, await fetch_int_setting("max_poster_max_attempts", 3)),
        retry_backoff_max_sec=max(1, await fetch_int_setting("max_poster_retry_backoff_max_sec", 30)),
    ) as client:
        for row in rows:
            try:
                data = await client.get_message(str(row["external_id"]))
            except MAXAPIError as exc:
                missing += 1
                logger.warning(
                    "MAX URL backfill skipped raw_post={} message={} error={}",
                    row["raw_post_id"],
                    row["external_id"],
                    exc,
                )
                continue
            url = poster.message_url_from_response(data)
            if not url:
                missing += 1
                continue
            await pool.execute(
                """
                UPDATE post_publications
                SET url=$2,
                    updated_at=NOW()
                WHERE id=$1
                """,
                int(row["id"]),
                url,
            )
            updated += 1
            logger.info("MAX URL backfilled raw_post={} url={}", row["raw_post_id"], url)

    logger.info("MAX URL backfill done: scanned={} updated={} missing={}", len(rows), updated, missing)
    await close_pool()


if __name__ == "__main__":
    asyncio.run(main())
