"""§1 Git / GitHub 基础 · slide 14-25 (12 内容页) · v0.2 真实内容版

嵌入 6 张图（B5/A7/A8/A9/A10/A15）+ 6 张自画版面（5 命令/GitHub 介绍/commit history/分支 PR/tag/Lab）
"""

from pathlib import Path
from pptx.util import Inches
from pptx.enum.text import PP_ALIGN

from build_main import (
    _fill_ph, _add_textbox, _add_card,
    NAVY, CORAL, CORAL_LIGHT, BLUE_ACCENT, GREEN, GOLD,
    GRAY_300, GRAY_500, GRAY_700, WHITE,
)
from build_sec_M1 import _add_centered_image, _title


def build_slide_14_git_is(prs):
    """slide 14: Git 是什么 · 嵌入 B5 时光机类比"""
    s = prs.slides.add_slide(prs.slide_layouts[3])
    _title(s, "§2 · Git 是什么 · 有时光机的文件夹")
    _add_centered_image(s, "B5-git-time-folder.png")
    return s


def build_slide_15_four_zones(prs):
    """slide 15: 4 区图 · 嵌入 A7"""
    s = prs.slides.add_slide(prs.slide_layouts[3])
    _title(s, "§2 · 关键概念图 · 4 区流向")
    _add_centered_image(s, "A7-git-four-zones.png")
    return s


def build_slide_16_five_commands(prs):
    """slide 16: 5 个最常用命令 · 自画 5 卡片"""
    s = prs.slides.add_slide(prs.slide_layouts[3])
    _title(s, "§2 · 5 个最常用命令")
    cmds = [
        ("clone",   "git clone git@github.com:org/agf.git",  "首次拉远程仓库到本地",  CORAL),
        ("status",  "git status",                            "看工作区 + 暂存区状态",  GOLD),
        ("add",     "git add file.py",                       "把改动加到暂存区",      BLUE_ACCENT),
        ("commit",  "git commit -m \"feat: ...\"",          "暂存区 → 本地仓库",     NAVY),
        ("push",    "git push origin main",                  "本地 → 远程仓库",       GREEN),
    ]
    for i, (cmd, example, desc, color) in enumerate(cmds):
        y = Inches(1.6 + i * 1.05)
        _add_card(s, Inches(0.55), y, Inches(12.2), Inches(0.95),
                  fill=WHITE, border=color, accent=color)
        _add_textbox(s, Inches(0.85), y + Inches(0.08), Inches(2.0), Inches(0.5),
                     f"git {cmd}", size_pt=20, bold=True, color=color)
        _add_textbox(s, Inches(0.85), y + Inches(0.5), Inches(7.5), Inches(0.4),
                     example, size_pt=13, color=GRAY_700)
        _add_textbox(s, Inches(8.5), y + Inches(0.25), Inches(4.0), Inches(0.5),
                     desc, size_pt=14, color=GRAY_700)
    return s


def build_slide_17_github_is(prs):
    """slide 17: GitHub 是什么 · 自画类比"""
    s = prs.slides.add_slide(prs.slide_layouts[3])
    _title(s, "§2 · GitHub 是什么")
    _add_textbox(s, Inches(0.55), Inches(1.8), Inches(12.2), Inches(1.0),
                 "类比：Git 仓库的「网盘 + 协作平台」",
                 size_pt=26, bold=True, color=CORAL, align=PP_ALIGN.CENTER)
    items = [
        ("☁网盘", "存远程 git 仓库，团队共享 + 你 push / pull"),
        ("Issue", "工单 / bug 追踪，含 label / milestone / assignee"),
        ("🔀  Pull Request", "代码评审 + merge 入口，CI 自动跑测试"),
        ("Actions", "CI/CD 自动化（push 跑测试 / merge 部署）"),
        ("Release", "版本快照 + binary 下载页 + CHANGELOG"),
    ]
    for i, (head, body) in enumerate(items):
        y = Inches(3.1 + i * 0.75)
        _add_textbox(s, Inches(1.5), y, Inches(3.5), Inches(0.6),
                     head, size_pt=18, bold=True, color=NAVY)
        _add_textbox(s, Inches(5.0), y + Inches(0.08), Inches(7.5), Inches(0.6),
                     body, size_pt=14, color=GRAY_700)
    return s


def build_slide_18_commit_history(prs):
    """slide 18: 看懂 commit history · 自画"""
    s = prs.slides.add_slide(prs.slide_layouts[3])
    _title(s, "§2 · 看懂 commit history")
    _add_textbox(s, Inches(0.55), Inches(1.5), Inches(12.2), Inches(0.5),
                 "$ git log --oneline -10",
                 size_pt=16, bold=True, color=NAVY)
    sample_logs = [
        ("d7220da", "feat",     "build_main.py v0.2 + build_sec_M1.py"),
        ("e8ee19f", "feat",     "postcard-feature 8 sample md"),
        ("5f09f5d", "docs",     "bump spec to v2.3 with deck TOC mapping"),
        ("b04a60a", "chore",    "remove PptxTemplate3 references"),
        ("7ecc9db", "docs",     "add template-team-guide for Template.pptx"),
        ("f869236", "feat",     "sink draw.io diagram methodology into skill"),
        ("0bd917a", "feat",     "41 deck diagrams + spec v2.2"),
    ]
    for i, (sha, prefix, msg) in enumerate(sample_logs):
        y = Inches(2.1 + i * 0.42)
        _add_textbox(s, Inches(0.85), y, Inches(1.2), Inches(0.4),
                     sha, size_pt=13, color=GRAY_500)
        _add_textbox(s, Inches(2.1), y, Inches(0.8), Inches(0.4),
                     prefix, size_pt=13, bold=True,
                     color=GREEN if prefix == "feat" else (BLUE_ACCENT if prefix == "docs" else GRAY_700))
        _add_textbox(s, Inches(3.0), y, Inches(9.7), Inches(0.4),
                     msg, size_pt=13, color=NAVY)
    # 前缀约定
    _add_textbox(s, Inches(0.55), Inches(5.4), Inches(12.2), Inches(0.5),
                 "Conventional Commits 前缀（必走）：",
                 size_pt=16, bold=True, color=NAVY)
    prefixes = [
        ("feat", "新功能"), ("fix", "bug 修复"), ("refactor", "重构"),
        ("test", "测试"), ("docs", "文档"), ("chore", "杂项"),
    ]
    for i, (prefix, desc) in enumerate(prefixes):
        col = i % 3
        row = i // 3
        x = Inches(0.85 + col * 4.0)
        y = Inches(6.0 + row * 0.5)
        _add_textbox(s, x, y, Inches(3.8), Inches(0.4),
                     f"▎ {prefix}: {desc}", size_pt=13, color=GRAY_700)
    return s


def build_slide_19_tdd_git(prs):
    """slide 19: TDD ↔ Git commit 顺序 · 嵌入 A8"""
    s = prs.slides.add_slide(prs.slide_layouts[3])
    _title(s, "§2 · TDD ↔ Git · commit 顺序的意义")
    _add_centered_image(s, "A8-tdd-git-timeline.png")
    return s


def build_slide_20_branch_pr(prs):
    """slide 20: 分支 + PR 流程 · 自画"""
    s = prs.slides.add_slide(prs.slide_layouts[3])
    _title(s, "§2 · 分支 + Pull Request")
    steps = [
        ("①", "git checkout -b feature/login",    "开 feature 分支（不直接在 main 改）", CORAL),
        ("②", "git commit / push",                  "本地 commit + push 远程 feature 分支", NAVY),
        ("③", "gh pr create / GitHub 打开 PR",     "提 PR · 触发 reviewer + CI",        GOLD),
        ("④", "Reviewer + CI 通过",                 "改完 push 后自动重跑 CI",            GREEN),
        ("⑤", "Squash & merge → 删 feature 分支",  "main 历史干净 + PR auto-close issue", BLUE_ACCENT),
    ]
    for i, (num, cmd, desc, color) in enumerate(steps):
        y = Inches(1.6 + i * 1.05)
        _add_card(s, Inches(0.55), y, Inches(12.2), Inches(0.95),
                  fill=WHITE, border=color, accent=color)
        _add_textbox(s, Inches(0.85), y + Inches(0.2), Inches(0.8), Inches(0.55),
                     num, size_pt=28, bold=True, color=color)
        _add_textbox(s, Inches(1.7), y + Inches(0.1), Inches(5.5), Inches(0.5),
                     cmd, size_pt=15, bold=True, color=NAVY)
        _add_textbox(s, Inches(1.7), y + Inches(0.55), Inches(5.5), Inches(0.4),
                     desc, size_pt=13, color=GRAY_700)
    return s


def build_slide_21_worktree(prs):
    """slide 21: Worktree dev 并行 · 嵌入 A9 (教学概念，§3 p53 实战)"""
    s = prs.slides.add_slide(prs.slide_layouts[3])
    _title(s, "§2 · Worktree · dev 并行隔离")
    _add_centered_image(s, "A9-worktree-parallel.png", height_inch=4.8, top_inch=1.5)
    # 与 p53 区分：本页是教学（§2 Git 入门掌握 worktree 命令），p53 是 §3 派单实战
    _add_textbox(s, Inches(0.55), Inches(6.5), Inches(12.2), Inches(0.4),
                 "本节教学概念 · §3（p53）将看 PRD 拆 Task 后 PL 如何用 worktree "
                 "让 3 dev 并行不冲突",
                 size_pt=12, color=GRAY_500, align=PP_ALIGN.CENTER, bold=True)
    return s


def build_slide_22_pre_commit(prs):
    """slide 22: pre-commit hook · 嵌入 A10"""
    s = prs.slides.add_slide(prs.slide_layouts[3])
    _title(s, "§2 · pre-commit Hook · 防密钥泄漏")
    _add_centered_image(s, "A10-pre-commit-hook.png")
    return s


def build_slide_23_tag(prs):
    """slide 23: Tag · Release 版本快照 · 自画"""
    s = prs.slides.add_slide(prs.slide_layouts[3])
    _title(s, "§2 · Tag · Release 版本快照（MAJOR/MINOR 必打）")
    # 命令样例
    _add_textbox(s, Inches(0.55), Inches(1.5), Inches(12.2), Inches(0.5),
                 "同会话内必走：tag annotation 含 tagged-along commits",
                 size_pt=16, bold=True, color=NAVY)
    cmd = ("git tag -a v1.0.0 -m \"Release v1.0.0 · 旅游明信片首版\n\n"
           "Tagged-along commits:\n"
           "- abc1234 feat: postcard three-step pipeline (PR #287)\n"
           "- def5678 feat: postcard API + DB schema (PR #288)\n"
           "- ghi9012 fix: PII redact in vision call log (PR #295)\"\n\n"
           "git push --tags")
    _add_card(s, Inches(0.55), Inches(2.1), Inches(12.2), Inches(3.3),
              fill=GRAY_500, border=GRAY_500)
    _add_textbox(s, Inches(0.85), Inches(2.25), Inches(11.6), Inches(3.0),
                 cmd, size_pt=14, color=WHITE)
    # SemVer 提示
    _add_textbox(s, Inches(0.55), Inches(5.7), Inches(12.2), Inches(0.5),
                 "SemVer：MAJOR (破坏性) · MINOR (新功能) · PATCH (修 bug)",
                 size_pt=15, color=CORAL, align=PP_ALIGN.CENTER, bold=True)
    _add_textbox(s, Inches(0.55), Inches(6.3), Inches(12.2), Inches(0.5),
                 "tag-orphan 反模式：commit 后未同会话 git tag -a → PL 必须当次会话补打或解释延期",
                 size_pt=13, color=GRAY_700, align=PP_ALIGN.CENTER)
    return s


def build_slide_24_github_release(prs):
    """slide 24: GitHub Release / Issue / PR 实战 · 嵌入 A15"""
    s = prs.slides.add_slide(prs.slide_layouts[3])
    _title(s, "§2 · GitHub Release / Issue / PR 全流程实战")
    _add_centered_image(s, "A15-github-collab.png")
    return s


def build_slide_25_lab(prs):
    """slide 25: Lab 小练习 · 自画"""
    s = prs.slides.add_slide(prs.slide_layouts[3])
    _title(s, "§2 · Lab 小练习（5 min）· 找一条 TDD red commit")
    _add_textbox(s, Inches(0.55), Inches(1.6), Inches(12.2), Inches(0.5),
                 "现场跑 3 条命令：", size_pt=18, bold=True, color=NAVY)
    cmds = [
        ("$ git clone git@github.com:org/agf.git",  "拉本仓库到 ~/wt/agf"),
        ("$ cd agf && git log --oneline -20",       "看最近 20 commits"),
        ("$ git log --grep='^test:' --oneline",    "找所有 TDD red commit"),
    ]
    for i, (cmd, desc) in enumerate(cmds):
        y = Inches(2.3 + i * 0.85)
        _add_card(s, Inches(0.55), Inches(2.2 + i * 0.85), Inches(12.2), Inches(0.75),
                  fill=GRAY_500, border=GRAY_500)
        _add_textbox(s, Inches(0.85), Inches(2.3 + i * 0.85), Inches(8.0), Inches(0.4),
                     cmd, size_pt=15, bold=True, color=WHITE)
        _add_textbox(s, Inches(8.9), Inches(2.36 + i * 0.85), Inches(4.0), Inches(0.4),
                     f"→ {desc}", size_pt=13, color=WHITE)
    # 学员任务
    _add_textbox(s, Inches(0.55), Inches(5.2), Inches(12.2), Inches(0.5),
                 "你的任务：", size_pt=18, bold=True, color=CORAL)
    tasks = [
        "① 找到任意一条 test: 开头的 commit · 记下 SHA",
        "② git show <SHA> 看代码是 unit test 还是 SIT test",
        "③ 看下一条 feat: commit 的时间戳是否 > test commit（验证 TDD 顺序）",
    ]
    for i, task in enumerate(tasks):
        _add_textbox(s, Inches(0.85), Inches(5.85 + i * 0.5), Inches(12.0), Inches(0.45),
                     task, size_pt=14, color=GRAY_700)
    return s


def build(prs):
    """构建 §1 12 张内容 slide (slide 14-25)"""
    build_slide_14_git_is(prs)
    build_slide_15_four_zones(prs)
    build_slide_16_five_commands(prs)
    build_slide_17_github_is(prs)
    build_slide_18_commit_history(prs)
    build_slide_19_tdd_git(prs)
    build_slide_20_branch_pr(prs)
    build_slide_21_worktree(prs)
    build_slide_22_pre_commit(prs)
    build_slide_23_tag(prs)
    build_slide_24_github_release(prs)
    build_slide_25_lab(prs)
