#!/usr/bin/env bash

set -euo pipefail

repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
source_script="$repo_root/scripts/archive_daily_qa.sh"
test_root="$(mktemp -d)"
trap 'rm -rf "$test_root"' EXIT

test_repo="$test_root/repo"
mkdir -p "$test_repo/scripts"
cp "$source_script" "$test_repo/scripts/archive_daily_qa.sh"
chmod +x "$test_repo/scripts/archive_daily_qa.sh"
git -C "$test_repo" init -q
git -C "$test_repo" config user.name 'Alice Zhang'
git -C "$test_repo" config user.email 'alice@example.invalid'
git -C "$test_repo" config research-institute.archivePerson 'Alice'

question_one="$test_root/question-one.md"
answer_one="$test_root/answer-one.md"
question_two="$test_root/question-two.md"
answer_two="$test_root/answer-two.md"
knowledge="$test_root/knowledge.md"
candidate="$test_root/candidate.md"

printf '请完整保留这个问题。\n第二行也不能省略。\n' > "$question_one"
printf '这是第一轮完整答案。\n包含第二行。\n' > "$answer_one"
printf '这是同一天的第二个问题。\n' > "$question_two"
printf '这是第二轮答案。\n' > "$answer_two"
printf '已验证知识。\n' > "$knowledge"
printf '待评审 Skill 候选。\n' > "$candidate"

bash "$test_repo/scripts/archive_daily_qa.sh" \
  --question-file "$question_one" \
  --answer-file "$answer_one" \
  --knowledge-file "$knowledge" \
  --skill-candidate-file "$candidate" \
  --date 2030-01-02

bash "$test_repo/scripts/archive_daily_qa.sh" \
  --question-file "$question_two" \
  --answer-file "$answer_two" \
  --date 2030-01-02

alice_archive="$test_repo/archives/daily/Alice-2030-01-02/Alice-2030-01-02.md"
test -f "$alice_archive"
test "$(rg -c '^## 问答 [0-9]+ — ' "$alice_archive")" = "2"
rg -q '^### 问$' "$alice_archive"
rg -q '^### 答$' "$alice_archive"
rg -q -F '请完整保留这个问题。' "$alice_archive"
rg -q -F '第二行也不能省略。' "$alice_archive"
rg -q -F '这是第一轮完整答案。' "$alice_archive"
rg -q -F '这是同一天的第二个问题。' "$alice_archive"
rg -q -F '这是第二轮答案。' "$alice_archive"
test -f "$test_repo/knowledge/daily/Alice-2030-01-02/knowledge.md"
test -f "$test_repo/skill-candidates/daily/Alice-2030-01-02/skill-candidates.md"

git -C "$test_repo" config research-institute.archivePerson 'Bob'
bash "$test_repo/scripts/archive_daily_qa.sh" \
  --question-file "$question_one" \
  --answer-file "$answer_one" \
  --date 2030-01-02
test -f "$test_repo/archives/daily/Bob-2030-01-02/Bob-2030-01-02.md"

preformatted="$test_root/preformatted.md"
printf '### 问\n\n预格式问题。\n\n### 答\n\n预格式答案。\n' > "$preformatted"
bash "$test_repo/scripts/archive_daily_qa.sh" \
  --entry-file "$preformatted" \
  --date 2030-01-03
rg -q -F '预格式问题。' "$test_repo/archives/daily/Bob-2030-01-03/Bob-2030-01-03.md"

summary_only="$test_root/summary-only.md"
printf '这里只是摘要，不是原始问答。\n' > "$summary_only"
if bash "$test_repo/scripts/archive_daily_qa.sh" --entry-file "$summary_only" --date 2030-01-04 >/dev/null 2>&1; then
  printf 'ERROR: summary-only entry was accepted.\n' >&2
  exit 1
fi

if bash "$test_repo/scripts/archive_daily_qa.sh" --question-file "$question_one" --date 2030-01-04 >/dev/null 2>&1; then
  printf 'ERROR: missing answer file was accepted.\n' >&2
  exit 1
fi

credential_question="$test_root/credential-question.md"
printf 'Authorization: Bearer should-not-be-archived\n' > "$credential_question"
if bash "$test_repo/scripts/archive_daily_qa.sh" \
  --question-file "$credential_question" \
  --answer-file "$answer_one" \
  --date 2030-01-04 >/dev/null 2>&1; then
  printf 'ERROR: credential-bearing question was accepted.\n' >&2
  exit 1
fi

remote_repo="$test_root/remote.git"
seed_repo="$test_root/seed"
alice_repo="$test_root/alice"
bob_repo="$test_root/bob"
check_repo="$test_root/check"

git init --bare -q --initial-branch=master "$remote_repo"
git init -q --initial-branch=master "$seed_repo"
mkdir -p "$seed_repo/scripts"
cp "$source_script" "$seed_repo/scripts/archive_daily_qa.sh"
chmod +x "$seed_repo/scripts/archive_daily_qa.sh"
git -C "$seed_repo" config user.name 'Seed'
git -C "$seed_repo" config user.email 'seed@example.invalid'
git -C "$seed_repo" add scripts/archive_daily_qa.sh
git -C "$seed_repo" commit -q -m 'test: seed archive script'
git -C "$seed_repo" remote add origin "$remote_repo"
git -C "$seed_repo" push -q -u origin master

git clone -q "$remote_repo" "$alice_repo"
git clone -q "$remote_repo" "$bob_repo"
git -C "$alice_repo" config user.name 'Alice Concurrent'
git -C "$alice_repo" config user.email 'alice-concurrent@example.invalid'
git -C "$alice_repo" config research-institute.archivePerson 'AliceConcurrent'
git -C "$bob_repo" config user.name 'Bob Concurrent'
git -C "$bob_repo" config user.email 'bob-concurrent@example.invalid'
git -C "$bob_repo" config research-institute.archivePerson 'BobConcurrent'

bash "$alice_repo/scripts/archive_daily_qa.sh" \
  --question-file "$question_one" \
  --answer-file "$answer_one" \
  --date 2030-01-05 \
  --push >/dev/null

bash "$bob_repo/scripts/archive_daily_qa.sh" \
  --question-file "$question_two" \
  --answer-file "$answer_two" \
  --date 2030-01-05 \
  --push >/dev/null

git clone -q "$remote_repo" "$check_repo"
test -f "$check_repo/archives/daily/AliceConcurrent-2030-01-05/AliceConcurrent-2030-01-05.md"
test -f "$check_repo/archives/daily/BobConcurrent-2030-01-05/BobConcurrent-2030-01-05.md"

printf 'TEST_OK archive_daily_qa verbatim per-person daily archive\n'
