# at top of persist_snapshot.py:
from sqlalchemy.ext.asyncio import AsyncSession

from typing import Dict
from models.market import (
    ProductLog,
)
from shared.db.session import get_session


async def log_product_result(ok: bool, payload: Dict) -> None:
    async with get_session() as session:
        await _log_product_result(session, ok, payload)
        await session.commit()


async def _log_product_result(session: AsyncSession, ok: bool, payload: Dict) -> None:
    session.add(ProductLog(
        ok=ok,
        slug=payload.get("slug"),
        href_tried=payload.get("href_tried"),
        error_type=payload.get("error_type"),
        error_message=payload.get("error_message"),
        http_status=payload.get("http_status"),
        final_url=payload.get("final_url"),
        transport_error=payload.get("transport_error"),
        title=payload.get("title"),
        has_description_html=payload.get("has_description_html"),
        has_description_short=payload.get("has_description_short"),
        sections_count=payload.get("sections_count"),
        images_count=payload.get("images_count"),
        embedded_images_count=payload.get("embedded_images_count"),
        all_images_count=payload.get("all_images_count"),
    ))

