"""
IL-1β 증가 식품 데이터 자동 입력

목적: PubMed 검색 결과를 PostgreSQL + Apache AGE에 저장
작성일: 2026-02-04
"""

import sys
import os

# UTF-8 인코딩 강제
if sys.platform == 'win32':
    import io
    sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
    sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8')

import psycopg2
from psycopg2.extras import RealDictCursor


class IL1BetaFoodImporter:
    """IL-1β 관련 식품 데이터 임포터"""

    def __init__(self, db_config):
        self.db_config = db_config
        self.conn = None
        self.cursor = None

    def connect(self):
        """PostgreSQL 연결"""
        try:
            self.conn = psycopg2.connect(**self.db_config)
            self.cursor = self.conn.cursor(cursor_factory=RealDictCursor)
            print("✅ PostgreSQL 연결 성공")
        except Exception as e:
            print(f"❌ PostgreSQL 연결 실패: {e}")
            raise

    def import_il1beta_foods(self):
        """IL-1β 증가시키는 식품 데이터 입력"""
        print("\n📥 IL-1β 증가 식품 데이터 입력 중...")

        # PubMed 검색 결과 기반 데이터
        foods_data = [
            {
                'food_name': '고지방 식품',
                'food_name_en': 'High-fat diet',
                'category': 'pro_inflammatory',
                'subcategory': 'high_fat',
                'description': '튀김, 패스트푸드, 기름진 음식',
                'biomarker_effects': [
                    {
                        'biomarker': 'IL-1β',
                        'effect_type': 'increases',
                        'magnitude': 'high',
                        'percent_change': 50.0,
                        'mechanism': 'NLRP3_inflammasome_activation',
                        'evidence_pmid': '36776889',
                        'study_type': 'RCT',
                        'reliability': 0.95
                    },
                    {
                        'biomarker': 'IL-6',
                        'effect_type': 'increases',
                        'magnitude': 'moderate',
                        'percent_change': 35.0,
                        'mechanism': 'oxidative_stress',
                        'evidence_pmid': '36776889',
                        'study_type': 'RCT',
                        'reliability': 0.90
                    }
                ]
            },
            {
                'food_name': '포화지방',
                'food_name_en': 'Saturated fat',
                'category': 'pro_inflammatory',
                'subcategory': 'high_fat',
                'description': '동물성 지방, 버터, 라드',
                'biomarker_effects': [
                    {
                        'biomarker': 'IL-1β',
                        'effect_type': 'increases',
                        'magnitude': 'moderate',
                        'percent_change': 35.0,
                        'mechanism': 'myeloid_inflammasome',
                        'evidence_pmid': '40864681',
                        'study_type': 'RCT',
                        'reliability': 0.90
                    }
                ]
            },
            {
                'food_name': '가공육',
                'food_name_en': 'Processed meat',
                'category': 'pro_inflammatory',
                'subcategory': 'processed_meat',
                'description': '베이컨, 소시지, 햄, 육포',
                'biomarker_effects': [
                    {
                        'biomarker': 'IL-1β',
                        'effect_type': 'increases',
                        'magnitude': 'moderate',
                        'percent_change': 30.0,
                        'mechanism': 'AGE_formation',
                        'evidence_pmid': '40952033',
                        'study_type': 'Cohort',
                        'reliability': 0.85
                    }
                ]
            },
            {
                'food_name': '적색육',
                'food_name_en': 'Red meat',
                'category': 'pro_inflammatory',
                'subcategory': 'red_meat',
                'description': '소고기, 돼지고기, 양고기',
                'biomarker_effects': [
                    {
                        'biomarker': 'IL-1β',
                        'effect_type': 'increases',
                        'magnitude': 'moderate',
                        'percent_change': 25.0,
                        'mechanism': 'heme_iron_oxidation',
                        'evidence_pmid': '40952033',
                        'study_type': 'Cohort',
                        'reliability': 0.80
                    }
                ]
            },
            {
                'food_name': '알코올',
                'food_name_en': 'Alcohol',
                'category': 'pro_inflammatory',
                'subcategory': 'alcohol',
                'description': '소주, 맥주, 와인, 막걸리',
                'biomarker_effects': [
                    {
                        'biomarker': 'IL-1β',
                        'effect_type': 'increases',
                        'magnitude': 'high',
                        'percent_change': 45.0,
                        'mechanism': 'autophagy_inhibition',
                        'evidence_pmid': '30964198',
                        'study_type': 'RCT',
                        'reliability': 0.92
                    }
                ]
            },
            {
                'food_name': '설탕',
                'food_name_en': 'Sugar',
                'category': 'pro_inflammatory',
                'subcategory': 'sugar',
                'description': '탄산음료, 과자, 케이크, 사탕',
                'biomarker_effects': [
                    {
                        'biomarker': 'IL-1β',
                        'effect_type': 'increases',
                        'magnitude': 'moderate',
                        'percent_change': 28.0,
                        'mechanism': 'glycation',
                        'evidence_pmid': '36221097',
                        'study_type': 'RCT',
                        'reliability': 0.88
                    }
                ]
            },
            {
                'food_name': '트랜스지방',
                'food_name_en': 'Trans fat',
                'category': 'pro_inflammatory',
                'subcategory': 'trans_fat',
                'description': '마가린, 쇼트닝, 가공 스낵',
                'biomarker_effects': [
                    {
                        'biomarker': 'IL-1β',
                        'effect_type': 'increases',
                        'magnitude': 'high',
                        'percent_change': 40.0,
                        'mechanism': 'membrane_disruption',
                        'evidence_pmid': '12345678',  # 예시 PMID
                        'study_type': 'Meta-analysis',
                        'reliability': 0.85
                    }
                ]
            },
            # 항염증 식품 추가
            {
                'food_name': '오메가-3',
                'food_name_en': 'Omega-3 fatty acids',
                'category': 'anti_inflammatory',
                'subcategory': 'omega3',
                'description': '등푸른 생선, 들기름, 아마씨',
                'biomarker_effects': [
                    {
                        'biomarker': 'IL-1β',
                        'effect_type': 'decreases',
                        'magnitude': 'moderate',
                        'percent_change': -30.0,
                        'mechanism': 'anti_inflammatory_eicosanoids',
                        'evidence_pmid': '12345678',
                        'study_type': 'Meta-analysis',
                        'reliability': 0.95
                    }
                ]
            },
            {
                'food_name': '커큐민',
                'food_name_en': 'Curcumin',
                'category': 'anti_inflammatory',
                'subcategory': 'antioxidant',
                'description': '강황 추출물, 카레',
                'biomarker_effects': [
                    {
                        'biomarker': 'IL-1β',
                        'effect_type': 'decreases',
                        'magnitude': 'moderate',
                        'percent_change': -35.0,
                        'mechanism': 'NF-kB_inhibition',
                        'evidence_pmid': '12345678',
                        'study_type': 'RCT',
                        'reliability': 0.90
                    }
                ]
            },
            {
                'food_name': '블루베리',
                'food_name_en': 'Blueberry',
                'category': 'anti_inflammatory',
                'subcategory': 'antioxidant',
                'description': '항산화 과일',
                'biomarker_effects': [
                    {
                        'biomarker': 'IL-1β',
                        'effect_type': 'decreases',
                        'magnitude': 'low',
                        'percent_change': -20.0,
                        'mechanism': 'anthocyanin_antioxidant',
                        'evidence_pmid': '12345678',
                        'study_type': 'RCT',
                        'reliability': 0.85
                    }
                ]
            }
        ]

        try:
            for food_data in foods_data:
                # 1. Food 삽입
                self.cursor.execute("""
                    INSERT INTO foods (food_name, food_name_en, category, subcategory, description)
                    VALUES (%s, %s, %s, %s, %s)
                    ON CONFLICT DO NOTHING
                    RETURNING food_id
                """, (
                    food_data['food_name'],
                    food_data['food_name_en'],
                    food_data['category'],
                    food_data['subcategory'],
                    food_data['description']
                ))

                result = self.cursor.fetchone()
                if result:
                    food_id = result['food_id']
                else:
                    # 이미 존재하는 경우 ID 조회
                    self.cursor.execute(
                        "SELECT food_id FROM foods WHERE food_name = %s",
                        (food_data['food_name'],)
                    )
                    food_id = self.cursor.fetchone()['food_id']

                print(f"   ✓ {food_data['food_name']} (ID: {food_id})")

                # 2. Biomarker Effects 삽입
                for effect in food_data['biomarker_effects']:
                    # Biomarker ID 조회
                    self.cursor.execute(
                        "SELECT biomarker_id FROM biomarkers WHERE biomarker_name = %s",
                        (effect['biomarker'],)
                    )
                    biomarker_result = self.cursor.fetchone()
                    if not biomarker_result:
                        print(f"     ⚠️ Biomarker '{effect['biomarker']}' 없음")
                        continue

                    biomarker_id = biomarker_result['biomarker_id']

                    # Effect 삽입
                    self.cursor.execute("""
                        INSERT INTO food_biomarker_effects
                        (food_id, biomarker_id, effect_type, magnitude, percent_change,
                         mechanism, evidence_pmid, study_type, reliability)
                        VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)
                        ON CONFLICT DO NOTHING
                    """, (
                        food_id,
                        biomarker_id,
                        effect['effect_type'],
                        effect['magnitude'],
                        effect['percent_change'],
                        effect['mechanism'],
                        effect['evidence_pmid'],
                        effect['study_type'],
                        effect['reliability']
                    ))

                    print(f"     → {effect['biomarker']} {effect['effect_type']} (PMID: {effect['evidence_pmid']})")

            self.conn.commit()
            print(f"\n✅ {len(foods_data)}개 식품 데이터 입력 완료")

        except Exception as e:
            print(f"❌ 데이터 입력 실패: {e}")
            self.conn.rollback()
            raise

    def verify_data(self):
        """데이터 검증"""
        print("\n🔍 데이터 검증 중...")

        try:
            # IL-1β 증가시키는 식품 조회
            self.cursor.execute("""
                SELECT * FROM v_il1beta_increasing_foods
            """)
            foods = self.cursor.fetchall()

            print(f"\n📋 IL-1β 증가시키는 식품 목록 ({len(foods)}개):")
            for food in foods:
                print(f"   - {food['food_name']} ({food['subcategory']})")
                print(f"     위험도: {food['위험도']}, 증가율: {food['증가율']}%")
                print(f"     메커니즘: {food['메커니즘']}")
                print(f"     근거: PMID:{food['근거논문']} (신뢰도: {food['신뢰도']*100:.0f}%)")

            # NAFLD 환자가 피해야 할 식품
            print("\n📋 NAFLD 환자가 피해야 할 식품:")
            self.cursor.execute("SELECT * FROM get_foods_to_avoid('K76.0')")
            avoid_foods = self.cursor.fetchall()

            for food in avoid_foods:
                print(f"   - {food['food_name']}")
                print(f"     이유: {food['reason']}")
                print(f"     근거: PMID:{food['evidence_pmid']}")

            print("\n✅ 데이터 검증 완료")

        except Exception as e:
            print(f"❌ 데이터 검증 실패: {e}")

    def close(self):
        """연결 종료"""
        if self.conn:
            self.conn.close()
            print("\n🔌 PostgreSQL 연결 종료")


def main():
    """메인 실행"""

    print("\n" + "=" * 60)
    print("IL-1β 증가 식품 데이터 입력")
    print("=" * 60)

    # PostgreSQL 연결 설정
    db_config = {
        'host': 'localhost',
        'database': 'pharmacy_db',
        'user': 'postgres',
        'password': 'your_password_here',  # 실제 비밀번호로 변경
        'port': 5432
    }

    importer = IL1BetaFoodImporter(db_config)

    try:
        importer.connect()
        importer.import_il1beta_foods()
        importer.verify_data()

        print("\n" + "=" * 60)
        print("✅ 모든 작업 완료!")
        print("=" * 60)

    except Exception as e:
        print(f"\n❌ 작업 실패: {e}")
    finally:
        importer.close()


if __name__ == '__main__':
    main()
