import sys
import os
import requests
sys.path.append(os.path.dirname(os.path.dirname(__file__)))

from services.personnel_parser import extract_from_article
from services.personnel_history_service import record_if_changed

def test_article_tracking():
    article_id = "4475254"
    url = f"https://www.motie.go.kr/kor/article/ATCL6e90bb9de/{article_id}/view?mno="

    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",
    }

    res = requests.get(url, headers=headers)
    html = res.text

    # ✅ HTML 저장
    save_path = os.path.join(os.path.dirname(__file__), f"sample_{article_id}.html")
    with open(save_path, "w", encoding="utf-8") as f:
        f.write(html)
    print(f"[💾] HTML 저장 완료: {save_path}")

    # ✅ 인사 발령 정보 추출
    people = extract_from_article(html, article_id)

    if not people:
        print("❌ 인사 발령 정보 추출 실패: 추출된 인물이 없습니다.")
        return

    for person in people:
        print(f"🔎 추출된 인물: {person['name']} - {person['department']} ({person['position']})")
        record_if_changed(person)

if __name__ == "__main__":
    test_article_tracking()
