from bs4 import BeautifulSoup
import re

def extract_from_article(html: str, article_id: str):
    soup = BeautifulSoup(html, "html.parser")

    content_div = soup.find("div", class_="cont-view")
    if not content_div:
        print(f"[❌] 본문 div(cont-view)를 찾을 수 없음. article_id={article_id}")
        return []

    text = content_div.get_text(separator="\n").strip()

    # 인사 내용 라인들만 추출 (예: 홍길동 -> 기획재정담당관)
    lines = text.split("\n")
    results = []

    # 예: ○ 산업통상자원부는 2024년 5월 27일 자로 ~~~
    #     * 국장급(고위공무원단) 전보
    #       산업정책관         홍길동
    for line in lines:
        line = line.strip()
        # 예: '에너지정책관               김민수'
        match = re.match(r"(.+?)\s{2,}(.+)$", line)
        if match:
            dept = match.group(1).strip()
            name = match.group(2).strip()
            results.append({
                "article_id": article_id,
                "name": name,
                "department": dept,
                "position": None  # 직책은 부서명에 포함되어 있으므로 따로 두지 않음
            })

    return results
