# -*- coding: utf-8 -*-
# 4バッチの調査結果(enrich-research-{1..4}.json)を名店マスターへマージ。
# officialSite / rating / reviewComment / confidence を反映。確認済の明確な誤りは訂正。タグ再生成。
import json, os, urllib.parse

HERE = os.path.dirname(os.path.abspath(__file__))
SRC  = os.path.join(HERE, "meiten-master.json")

with open(SRC, encoding="utf-8") as f:
    j = json.load(f)

# --- 調査結果を1つに統合 ---
research = {}
for n in (1,2,3,4):
    p = os.path.join(HERE, f"enrich-research-{n}.json")
    if os.path.exists(p):
        with open(p, encoding="utf-8") as f:
            d = json.load(f)
        research.update(d)
print("research records:", len(research))

# --- 高確度の明確な分類誤り訂正（GR19：確認済のみ） ---
CATEGORY_FIX = {
    "MTN-012": "飲食・焼肉",   # 本とさや＝書籍雑貨は誤り。浅草の炭火焼肉店（焼肉百名店2025・食べログ3.72）
}

LBLGROUP = {
    "L1a":"継続広告主","L1b":"継続広告主","L1c":"継続広告主","L1d":"継続広告主",
    "L2":"途絶★再コンタクト","L3":"埋蔵金(取材済未広告)","L4":"グループ内",
    "L5":"域外/大手","L6":"未接触/新規開拓",
}
def derive_tags(s):
    t=[]
    g=LBLGROUP.get(s.get("label"));  t.append(g) if g else None
    ward=(s.get("area") or {}).get("ward");  t.append(ward) if ward else None
    town=(s.get("area") or {}).get("town");  t.append(town) if town else None
    for piece in (s.get("category") or "").replace("／","・").split("・"):
        piece=piece.strip()
        if piece and piece not in t: t.append(piece)
    issues=s.get("issuesPlaced") or []
    if 15 in issues: t.append("Vol.15出稿")
    if s.get("lapsed"): t.append("途絶")
    if (s.get("tegataFit") or {}).get("level")=="高": t.append("手形適性高")
    if s.get("hotelEastFit")=="高": t.append("ホテル送客高")
    if s.get("potentialTier"): t.append("ポテンシャル"+s["potentialTier"])
    if s.get("rating") and s["rating"].get("score"):
        try:
            if float(str(s["rating"]["score"]).split()[0])>=3.5: t.append("高評価")
        except: pass
    seen=set(); out=[]
    for x in t:
        if x not in seen: seen.add(x); out.append(x)
    return out

stat={"site":0,"site_unknown":0,"rating":0,"review":0,"no_research":0}
for s in j.get("stores", []):
    sid=s.get("storeId")
    if sid in CATEGORY_FIX:
        s["category"]=CATEGORY_FIX[sid]
        s.setdefault("dataNote","")
        s["dataNote"]=(s.get("dataNote") or "")+" [カテゴリ訂正:調査により焼肉店と確認]"
    r=research.get(sid)
    if r:
        site=r.get("officialSite")
        s["officialSite"]= site if site else "不明"
        if site and site!="不明": stat["site"]+=1
        else: stat["site_unknown"]+=1
        rt=r.get("rating")
        s["rating"]= rt if (rt and isinstance(rt,dict) and rt.get("score")) else None
        if s["rating"]: stat["rating"]+=1
        rc=r.get("reviewComment")
        s["reviewComment"]= rc if rc else "不明"
        if rc and rc!="不明": stat["review"]+=1
        if r.get("confidence"): s["researchConfidence"]=r["confidence"]
    else:
        s["officialSite"]= s.get("officialSite") or "不明"
        stat["no_research"]+=1
    s["tags"]=derive_tags(s)

j["_meta"]["enriched"]="2026-06-06: googleMapsUrl/tags + 公式サイト/評価/コメント(4バッチWeb調査・出典確認分のみ・不明は不明)。"
with open(SRC,"w",encoding="utf-8") as f:
    json.dump(j,f,ensure_ascii=False,indent=2)

print("merged. stats:", stat)
# 高評価サンプル
hi=[(s["name"], s["rating"]["score"]) for s in j["stores"] if s.get("rating") and s["rating"].get("score")]
print("rating count:", len(hi))
