#!/usr/bin/env python3
"""
xlsx의 StatusPoint DescriptorState0~7 → hc900.tag_metadata 로드
실행: python3 scripts/load_state_labels.py
"""
import openpyxl
import psycopg2
from pathlib import Path

XLSX_PATH = Path(__file__).parent.parent / "docs" / "Sinam_Tag_all.xlsx"
DB_DSN = "host=localhost port=5432 dbname=iiot_platform user=postgres password=postgres"

wb = openpyxl.load_workbook(XLSX_PATH, read_only=True, data_only=True)
ws = wb['Sheet1']
rows = list(ws.iter_rows(values_only=True))
headers = rows[1]
col = {h: i for i, h in enumerate(headers) if h}

conn = psycopg2.connect(DB_DSN)
cur = conn.cursor()

inserted = 0
for row in rows[2:]:
    name = str(row[col['ItemName']] or '').strip()
    cls  = str(row[col['Class']]    or '').strip()
    if cls != 'StatusPoint' or not name:
        continue

    base_tag = name.lower()

    for i in range(8):
        key = f'DescriptorState{i}'
        if key not in col:
            continue
        val = row[col[key]]
        if val is None or val == '':
            continue
        cur.execute("""
            INSERT INTO hc900.tag_metadata (base_tag, attribute, value)
            VALUES (%s, %s, %s)
            ON CONFLICT (base_tag, attribute) DO UPDATE SET value = EXCLUDED.value
        """, (base_tag, f'state{i}', str(val)))
        inserted += 1

conn.commit()
cur.close()
conn.close()
print(f"완료: {inserted}개 상태 레이블 저장")
