import logging

from qbot.core import registry
from qbot.db import plugin_storage
from qbot.message import Image, OutgoingMessage, Text, send_message
from qbot.plugins.cmc import comic

PLUGIN_NAME = "xkcd"
LATEST_COMIC_KEY = "latest_comic"

logger = logging.getLogger(__name__)


@comic
async def xkcd():
    last_seen_comic = await registry.database.fetch_val(
        plugin_storage.select().where(
            (plugin_storage.c.plugin == PLUGIN_NAME)
            & (plugin_storage.c.key == LATEST_COMIC_KEY)
        ),
        column=plugin_storage.c.data,
    )
    try:
        resp = await registry.http_client.get("https://xkcd.com/info.0.json")
    except Exception:
        logger.exception("Failed to retrieve latest XKCD comic.")
        return
    if not 200 <= resp.status_code < 400:
        logger.error(f"Incorrect response from XKCD. Status: {resp.status_code}")
        return
    data = resp.json()
    if data["num"] != last_seen_comic:
        thread_ts = await send_message(
            OutgoingMessage(
                channel=registry.CHANNEL_COMICS,
                thread_ts=None,
                blocks=[
                    Text(f"https://xkcd.com - {data['safe_title']}"),
                    Image(image_url=data["img"], alt_text="XKCD"),
                ],
            )
        )
        await send_message(
            OutgoingMessage(
                channel=registry.CHANNEL_COMICS,
                thread_ts=thread_ts,
                blocks=[Text(data["alt"])],
            )
        )
        if last_seen_comic is None:
            await registry.database.execute(
                plugin_storage.insert(),
                values={
                    "plugin": PLUGIN_NAME,
                    "key": LATEST_COMIC_KEY,
                    "data": data["num"],
                },
            )
        else:
            await registry.database.execute(
                plugin_storage.update().where(
                    (plugin_storage.c.plugin == PLUGIN_NAME)
                    & (plugin_storage.c.key == LATEST_COMIC_KEY)
                ),
                values={"data": data["num"]},
            )

import logging

from qbot.core import registry
from qbot.db import plugin_storage
from qbot.message import Image, OutgoingMessage, Text, send_message
from qbot.plugins.cmc import comic

PLUGIN_NAME = "xkcd"
LATEST_COMIC_KEY = "latest_comic"

logger = logging.getLogger(__name__)


@comic
async def xkcd():
    last_seen_comic = await registry.database.fetch_val(
        plugin_storage.select().where(
            (plugin_storage.c.plugin == PLUGIN_NAME)
            & (plugin_storage.c.key == LATEST_COMIC_KEY)
        ),
        column=plugin_storage.c.data,
    )
    try:
        resp = await registry.http_client.get("https://xkcd.com/info.0.json")
    except Exception:
        logger.exception("Failed to retrieve latest XKCD comic.")
        return
    if not 200 <= resp.status_code < 400:
        logger.error(f"Incorrect response from XKCD. Status: {resp.status_code}")
        return
    data = resp.json()
    if data["num"] != last_seen_comic:
        thread_ts = await send_message(
            OutgoingMessage(
                channel=registry.CHANNEL_COMICS,
                thread_ts=None,
                blocks=[
                    Text(f"https://xkcd.com - {data['safe_title']}"),
                    Image(image_url=data["img"], alt_text="XKCD"),
                ],
            )
        )
        await send_message(
            OutgoingMessage(
                channel=registry.CHANNEL_COMICS,
                thread_ts=thread_ts,
                blocks=[Text(data["alt"])],
            )
        )
        if last_seen_comic is None:
            await registry.database.execute(
                plugin_storage.insert(),
                values={
                    "plugin": PLUGIN_NAME,
                    "key": LATEST_COMIC_KEY,
                    "data": data["num"],
                },
            )
        else:
            await registry.database.execute(
                plugin_storage.update().where(
                    (plugin_storage.c.plugin == PLUGIN_NAME)
                    & (plugin_storage.c.key == LATEST_COMIC_KEY)
                ),
                values={"data": data["num"]},
            )
