"""
Insert ERP data source config into data_source_config table
"""
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from dotenv import load_dotenv
load_dotenv()

from app.database import get_session_local
from app.models import DataSourceConfig

db = get_session_local()()
try:
    # Check if already exists
    existing = db.query(DataSourceConfig).filter(
        DataSourceConfig.name == 'ERP系统 - 博海网络'
    ).first()
    
    if existing:
        print(f"数据源已存在: id={existing.id}, name={existing.name}")
    else:
        source = DataSourceConfig(
            name='ERP系统 - 博海网络',
            source_type='erp',
            api_endpoint='http://127.0.0.1:8300/api/v1',
            api_key='erp-gateway-key-bhwl-2026',
            sync_type='batch',
            status='active',
        )
        db.add(source)
        db.commit()
        db.refresh(source)
        print(f"数据源插入成功: id={source.id}, name={source.name}")
    
    # Show all sources
    all_sources = db.query(DataSourceConfig).all()
    print(f"\n当前 data_source_config 表记录数: {len(all_sources)}")
    for s in all_sources:
        print(f"  id={s.id}, name={s.name}, type={s.source_type}, status={s.status}")
        
except Exception as e:
    print(f"错误: {e}")
    db.rollback()
finally:
    db.close()
