"""
동물약 태깅 및 MSSQL 동기화
1. 키워드로 CD_GOODS에서 동물약 검색
2. SQLite drug_tags.db에 태깅
3. MSSQL CD_GOODS.POS_BOON = '010103' 업데이트
"""
import sqlite3
from pathlib import Path
import sys

sys.path.insert(0, str(Path(__file__).parent.parent))
from db.dbsetup import db_manager
from sqlalchemy import text

# SQLite DB 경로
DB_PATH = Path(__file__).parent.parent / 'db' / 'drug_tags.db'

# 동물약 키워드
ANIMAL_KEYWORDS = [
    '동물', '반려', '애견', '강아지', '고양이', '반려견',
    '넥스가드', '브라벡토', '심파리카', '크레델리오', '컴포티스',
    '하트세이버', '하트가드', '다이로하트', '하트웜', '하트캅',
    '안텔민', '파라캅', '제스타제',
    '캐치원', '셀라이트', '가드닐', '리펠로', '심피드독',
    '세레니아', '아포퀄', '갈리프란트', '클라펫',
    '펫팜', '동물약품', '애니팜'
]

# 동물약 공급처 (SplName이 이 값이면 전부 동물약)
ANIMAL_SUPPLIERS = [
    '펫팜'
]

# 제외 키워드 (사람용 약)
EXCLUDE_KEYWORDS = [
    '헤리펫사', '토피라펫', '메타트레이스', '페리돈', '세파라캅'
]

def init_sqlite_db():
    """SQLite DB 초기화"""
    DB_PATH.parent.mkdir(parents=True, exist_ok=True)
    conn = sqlite3.connect(DB_PATH)
    cursor = conn.cursor()
    
    cursor.execute('''
        CREATE TABLE IF NOT EXISTS drug_tags (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            drug_code TEXT NOT NULL,
            drug_name TEXT,
            barcode TEXT,
            tag_type TEXT NOT NULL,
            tag_value TEXT,
            note TEXT,
            source TEXT DEFAULT 'keyword',
            confidence REAL DEFAULT 0.8,
            is_active BOOLEAN DEFAULT 1,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            UNIQUE(drug_code, tag_type)
        )
    ''')
    conn.commit()
    conn.close()
    print(f"✅ SQLite DB 준비: {DB_PATH}")

def search_animal_drugs():
    """MSSQL에서 동물약 검색 (키워드 + 공급처)"""
    print("🔍 CD_GOODS에서 동물약 검색 중...")

    session = db_manager.get_session('PM_DRUG')

    # 키워드 조건
    keyword_conds = ' OR '.join([f"GoodsName LIKE '%{kw}%'" for kw in ANIMAL_KEYWORDS])

    # 공급처 조건
    supplier_conds = ' OR '.join([f"SplName = '{sp}'" for sp in ANIMAL_SUPPLIERS])

    query = text(f"""
        SELECT DrugCode, GoodsName, BARCODE, POS_BOON, SplName
        FROM CD_GOODS
        WHERE (({keyword_conds}) OR ({supplier_conds}))
          AND GoodsSelCode = 'B'
    """)

    result = session.execute(query)
    drugs = result.fetchall()

    # 키워드 vs 공급처 통계
    by_keyword = [d for d in drugs if any(kw in (d.GoodsName or '') for kw in ANIMAL_KEYWORDS)]
    by_supplier = [d for d in drugs if d.SplName in ANIMAL_SUPPLIERS]
    supplier_only = [d for d in by_supplier if not any(kw in (d.GoodsName or '') for kw in ANIMAL_KEYWORDS)]

    print(f"✅ 발견: {len(drugs)}개 (키워드: {len(by_keyword)}, 공급처 추가: {len(supplier_only)})")
    if supplier_only:
        print("  📦 공급처 기반 신규:")
        for d in supplier_only:
            print(f"    {d.DrugCode}: {d.GoodsName} ({d.SplName})")

    return drugs

def tag_to_sqlite(drugs):
    """SQLite에 동물약 태깅"""
    print("\n📝 SQLite 태깅 중...")
    
    conn = sqlite3.connect(DB_PATH)
    cursor = conn.cursor()
    
    added = 0
    skipped = 0
    excluded = 0
    
    for drug in drugs:
        drug_code = drug[0]
        drug_name = drug[1] or ''
        barcode = drug[2]
        spl_name = drug[4] if len(drug) > 4 else ''

        # 제외 키워드 체크
        if any(ex in drug_name for ex in EXCLUDE_KEYWORDS):
            excluded += 1
            print(f"  ⛔ 제외: {drug_code} - {drug_name}")
            continue

        # 매칭 소스 구분
        by_kw = any(kw in drug_name for kw in ANIMAL_KEYWORDS)
        by_sp = spl_name in ANIMAL_SUPPLIERS
        source = 'keyword' if by_kw else 'supplier'
        note = '키워드 자동 태깅' if by_kw else f'공급처({spl_name}) 자동 태깅'

        try:
            cursor.execute('''
                INSERT INTO drug_tags (drug_code, drug_name, barcode, tag_type, tag_value, note, source)
                VALUES (?, ?, ?, 'animal_drug', 'all', ?, ?)
            ''', (drug_code, drug_name, barcode, note, source))
            added += 1
            print(f"  ✅ {drug_code}: {drug_name} [{source}]")
        except sqlite3.IntegrityError:
            skipped += 1
    
    conn.commit()
    conn.close()
    
    print(f"\n📊 태깅 결과: 추가 {added}개, 중복 {skipped}개, 제외 {excluded}개")
    return added

def sync_to_mssql():
    """SQLite 태그를 MSSQL POS_BOON에 동기화"""
    print("\n🔄 MSSQL 동기화 중...")
    
    # SQLite에서 동물약 목록 가져오기
    conn = sqlite3.connect(DB_PATH)
    cursor = conn.cursor()
    cursor.execute('''
        SELECT drug_code, drug_name FROM drug_tags 
        WHERE tag_type = 'animal_drug' AND is_active = 1
    ''')
    animal_drugs = cursor.fetchall()
    conn.close()
    
    print(f"  동물약 {len(animal_drugs)}개 → POS_BOON='010103' 업데이트")
    
    # MSSQL 업데이트
    session = db_manager.get_session('PM_DRUG')
    updated = 0
    
    for drug_code, drug_name in animal_drugs:
        try:
            result = session.execute(text('''
                UPDATE CD_GOODS SET POS_BOON = '010103' WHERE DrugCode = :dc
            '''), {'dc': drug_code})
            session.commit()
            if result.rowcount > 0:
                updated += 1
                print(f"  ✅ {drug_code}: {drug_name}")
        except Exception as e:
            print(f"  ❌ {drug_code}: {e}")
    
    print(f"\n🎉 완료! MSSQL 업데이트: {updated}개")

def main():
    print("=" * 50)
    print("🐾 동물약 태깅 시스템")
    print("=" * 50)
    
    # 1. SQLite 초기화
    init_sqlite_db()
    
    # 2. 동물약 검색
    drugs = search_animal_drugs()
    
    # 3. SQLite 태깅
    tag_to_sqlite(drugs)
    
    # 4. MSSQL 동기화
    sync_to_mssql()
    
    print("\n" + "=" * 50)
    print("✅ 모든 작업 완료!")
    print("=" * 50)

if __name__ == '__main__':
    main()
