"""平台店铺映射内部上报的收发模型。

pricebot 在淘宝比价拿到 shopId 后 server→server POST 一行映射。所有字段可空(按比价
角色稀疏填充),server 端只校验共享密钥 + 幂等(trace_id)落库。
"""
from __future__ import annotations

from pydantic import BaseModel


class StoreMappingIn(BaseModel):
    """一次比价的跨平台店铺映射上报体(一行)。"""

    trace_id: str
    business_type: str = "food"
    source_platform: str | None = None

    # 跨平台身份(按角色稀疏填充)
    id_taobao: str | None = None
    name_taobao: str | None = None
    id_meituan: str | None = None
    name_meituan: str | None = None
    id_jd: str | None = None
    name_jd: str | None = None

    # 地理
    city: str | None = None
    geohash: str | None = None
    lng: float | None = None
    lat: float | None = None
    taobao_address: str | None = None

    # 溯源
    source_device_id: str | None = None
    source_user_id: int | None = None

    # 淘宝原料(可复跳 / 可重解析 / 调试)
    taobao_share_url: str | None = None
    taobao_resolved_url: str | None = None
    taobao_deeplink: str | None = None

    # 美团原料(poi_id_str 非稳定主键, 单列存; 见 model 注释)
    meituan_poi_id_str: str | None = None
    meituan_share_url: str | None = None
    meituan_resolved_url: str | None = None
    meituan_deeplink: str | None = None

    # 京东原料(venderId 单列存, storeId 进 id_jd; 见 model 注释)
    jd_vender_id: str | None = None
    jd_share_url: str | None = None
    jd_resolved_url: str | None = None
    jd_deeplink: str | None = None

    attrs: dict | None = None


class StoreMappingOut(BaseModel):
    """上报结果。inserted=1 为新建该 trace 行,0 为合并进已存在行(填空,不覆盖);
    row_id 为该 trace 对应行 id。"""

    inserted: int
    row_id: int | None = None


class StoreMappingInvalidateIn(BaseModel):
    """标记某平台某 shopId 的缓存 deeplink 失效(pricebot 撞错误页回退时上报)。"""

    platform: str  # 目前只支持 "taobao"
    shop_id: str  # 失效的店铺 id(淘宝 = shopId = id_taobao)


class StoreMappingInvalidateOut(BaseModel):
    """标记结果。affected = 本次新标记失效的行数(按 shopId 标记所有匹配行)。"""

    ok: bool
    affected: int
