# services/motie_org_parser.py

import re
from bs4 import BeautifulSoup
from utils.fetcher import fetch_html

def get_last_emp_page():
    url = "https://www.motie.go.kr/kor/25/empSearch"
    html = fetch_html(url)
    if not html:
        return 1

    soup = BeautifulSoup(html, "html.parser")
    last_btn = soup.select_one("a.direction.last")

    if not last_btn:
        return 1

    onclick = last_btn.get("onclick", "")
    match = re.search(r"empSearch\.list\((\d+)\)", onclick)

    if match:
        return int(match.group(1))
    return 1

def parse_employee_page(page_index: int):
    url = f"https://www.motie.go.kr/kor/25/empSearch?pageIndex={page_index}"
    html = fetch_html(url)
    if not html:
        return []

    soup = BeautifulSoup(html, "html.parser")
    rows = soup.select("table tbody tr")

    parsed = []

    for row in rows:
        text = row.get_text(" ", strip=True)  # 셀 내용 공백으로 연결된 1줄 텍스트
        columns = text.split()

        if len(columns) < 5:
            continue

        name = columns[0]
        position = columns[1]
        department = columns[2]
        phone_raw = columns[-1]

        # 전화번호 추출
        phone_match = re.search(r"tel:([\d\-]+)]", phone_raw)
        phone = phone_match.group(1) if phone_match else phone_raw

        # 담당업무: 전화번호 제거 전 영역 추출
        task_with_phone = " ".join(columns[3:-1])
        task = task_with_phone.replace(phone, "").strip()

        parsed.append({
            "name": name,
            "position": position,
            "department": department,
            "task": task,
            "phone": phone
        })

    return parsed