#!/usr/bin/env python3
"""Create truthful 16:9 evidence-gap cards for projects without a scene image."""

from __future__ import annotations

import csv
import html
import subprocess
from pathlib import Path


ROOT = Path(__file__).resolve().parents[1]
DATA = ROOT / "data"
OUT = ROOT / "assets" / "placeholders"


def clip(value: str, length: int = 52) -> str:
    value = " ".join((value or "待补证").split())
    return value if len(value) <= length else value[: length - 1] + "…"


def main() -> None:
    with (DATA / "project-image-selection.csv").open(encoding="utf-8-sig", newline="") as handle:
        rows = list(csv.DictReader(handle))
    OUT.mkdir(parents=True, exist_ok=True)
    built = 0
    for row in rows:
        if row["唯一主图"]:
            continue
        pid = row["项目ID"]
        name = html.escape(row["项目名称"])
        blocker = html.escape(clip(row["正式出图阻断"], 64))
        grade = html.escape(row["总体证据等级"])
        status = html.escape(row["主图状态"])
        svg = f'''<svg xmlns="http://www.w3.org/2000/svg" width="1600" height="900" viewBox="0 0 1600 900">
  <defs>
    <linearGradient id="bg" x1="0" y1="0" x2="1" y2="1"><stop stop-color="#071a24"/><stop offset="1" stop-color="#102f38"/></linearGradient>
    <pattern id="grid" width="48" height="48" patternUnits="userSpaceOnUse"><path d="M48 0H0V48" fill="none" stroke="#76d3c4" stroke-opacity=".09"/></pattern>
  </defs>
  <rect width="1600" height="900" fill="url(#bg)"/><rect width="1600" height="900" fill="url(#grid)"/>
  <rect x="78" y="70" width="1444" height="760" rx="38" fill="#0d2730" stroke="#78d8c8" stroke-opacity=".35" stroke-width="2"/>
  <text x="130" y="150" fill="#79dccc" font-size="26" font-family="PingFang SC,Microsoft YaHei,sans-serif" letter-spacing="3">{pid} · EVIDENCE GATE</text>
  <text x="130" y="238" fill="#f7fbfa" font-size="58" font-weight="700" font-family="PingFang SC,Microsoft YaHei,sans-serif">{name}</text>
  <circle cx="800" cy="430" r="112" fill="none" stroke="#f2bd5d" stroke-width="8" stroke-dasharray="16 12"/>
  <text x="800" y="470" text-anchor="middle" fill="#f2bd5d" font-size="122" font-weight="700" font-family="Arial,sans-serif">?</text>
  <text x="800" y="600" text-anchor="middle" fill="#ffffff" font-size="38" font-weight="650" font-family="PingFang SC,Microsoft YaHei,sans-serif">待补证，不虚构现场</text>
  <text x="800" y="662" text-anchor="middle" fill="#b8cbc8" font-size="26" font-family="PingFang SC,Microsoft YaHei,sans-serif">{blocker}</text>
  <rect x="130" y="728" width="210" height="54" rx="27" fill="#f2bd5d"/><text x="235" y="764" text-anchor="middle" fill="#1b2626" font-size="24" font-weight="700" font-family="PingFang SC,Microsoft YaHei,sans-serif">证据等级 {grade}</text>
  <text x="380" y="764" fill="#9eb7b3" font-size="22" font-family="PingFang SC,Microsoft YaHei,sans-serif">{status} · 不可作为 1:1 现场数字孪生</text>
</svg>'''
        svg_path = OUT / f"{pid}-evidence-placeholder.svg"
        png_path = OUT / f"{pid}-evidence-placeholder.png"
        svg_path.write_text(svg, encoding="utf-8")
        subprocess.run(["rsvg-convert", str(svg_path), "-o", str(png_path)], check=True)
        row["唯一主图"] = png_path.relative_to(ROOT).as_posix()
        row["主图状态"] = row["主图状态"] + "-证据卡片"
        built += 1
    with (DATA / "project-image-selection.csv").open("w", encoding="utf-8-sig", newline="") as handle:
        writer = csv.DictWriter(handle, fieldnames=list(rows[0]))
        writer.writeheader()
        writer.writerows(rows)
    print(f"built={built} output={OUT}")


if __name__ == "__main__":
    main()
