"""OKR模板库 — 制造业行业包（6个模板，source='industry_pack', industry_tag='manufacturing'）"""
import sys, os, logging
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))

from app.database import get_session_local
from app.models import OKRTemplate

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("seed_manufacturing_pack")

MANUFACTURING_TEMPLATES = [
    {
        "name": "提升产能利用率",
        "description": "优化排产与设备管理，提升产能利用率",
        "dimension": "process",
        "layer": "level2",
        "industry_tag": "manufacturing",
        "source": "industry_pack",
        "sort_order": 301,
        "preset_krs": [
            {"name": "产能利用率≥85%", "target_value": "≥85%", "weight": 34, "sort_order": 1},
            {"name": "设备停机时间下降≥20%", "target_value": "≥20%", "weight": 33, "sort_order": 2},
            {"name": "OEE综合效率≥75%", "target_value": "≥75%", "weight": 33, "sort_order": 3},
        ],
    },
    {
        "name": "降低生产成本",
        "description": "通过精益生产与供应链优化降低制造成本",
        "dimension": "finance",
        "layer": "level2",
        "industry_tag": "manufacturing",
        "source": "industry_pack",
        "sort_order": 302,
        "preset_krs": [
            {"name": "单位制造成本下降≥10%", "target_value": "≥10%", "weight": 34, "sort_order": 1},
            {"name": "原材料损耗率≤2%", "target_value": "≤2%", "weight": 33, "sort_order": 2},
            {"name": "采购成本下降≥5%", "target_value": "≥5%", "weight": 33, "sort_order": 3},
        ],
    },
    {
        "name": "提升产品一次合格率",
        "description": "强化质量管控，降低不良品率",
        "dimension": "process",
        "layer": "level2",
        "industry_tag": "manufacturing",
        "source": "industry_pack",
        "sort_order": 303,
        "preset_krs": [
            {"name": "一次合格率≥98%", "target_value": "≥98%", "weight": 34, "sort_order": 1},
            {"name": "客户投诉率下降≥30%", "target_value": "≥30%", "weight": 33, "sort_order": 2},
            {"name": "质量返工成本下降≥20%", "target_value": "≥20%", "weight": 33, "sort_order": 3},
        ],
    },
    {
        "name": "缩短交付周期",
        "description": "优化生产计划与供应链协同，缩短订单交付周期",
        "dimension": "customer",
        "layer": "level2",
        "industry_tag": "manufacturing",
        "source": "industry_pack",
        "sort_order": 304,
        "preset_krs": [
            {"name": "订单交付周期缩短≥15%", "target_value": "≥15%", "weight": 34, "sort_order": 1},
            {"name": "准时交付率≥95%", "target_value": "≥95%", "weight": 33, "sort_order": 2},
            {"name": "在制品库存下降≥20%", "target_value": "≥20%", "weight": 33, "sort_order": 3},
        ],
    },
    {
        "name": "设备智能化升级",
        "description": "推进设备数字化改造与智能制造",
        "dimension": "learning",
        "layer": "level2",
        "industry_tag": "manufacturing",
        "source": "industry_pack",
        "sort_order": 305,
        "preset_krs": [
            {"name": "核心设备联网率≥70%", "target_value": "≥70%", "weight": 34, "sort_order": 1},
            {"name": "自动化覆盖率≥50%", "target_value": "≥50%", "weight": 33, "sort_order": 2},
            {"name": "数据采集点覆盖关键工序100%", "target_value": "100%", "weight": 33, "sort_order": 3},
        ],
    },
    {
        "name": "安全生产零事故",
        "description": "完善安全管理体系，实现安全生产零事故",
        "dimension": "learning",
        "layer": "level2",
        "industry_tag": "manufacturing",
        "source": "industry_pack",
        "sort_order": 306,
        "preset_krs": [
            {"name": "重大安全事故0起", "target_value": "0起", "weight": 40, "sort_order": 1},
            {"name": "安全隐患整改率100%", "target_value": "100%", "weight": 30, "sort_order": 2},
            {"name": "安全培训覆盖率100%", "target_value": "100%", "weight": 30, "sort_order": 3},
        ],
    },
]


def seed_manufacturing_pack():
    """插入制造业行业包（幂等：按industry_tag去重）"""
    db = get_session_local()()
    try:
        existing = db.query(OKRTemplate).filter(
            OKRTemplate.industry_tag == "manufacturing",
            OKRTemplate.source == "industry_pack",
        ).count()
        if existing > 0:
            logger.info(f"已有 {existing} 条制造业行业包模板，跳过")
            return
        for item in MANUFACTURING_TEMPLATES:
            db.add(OKRTemplate(**item))
        db.commit()
        logger.info(f"✔ 已插入 {len(MANUFACTURING_TEMPLATES)} 条制造业行业包模板")
    except Exception as e:
        db.rollback()
        logger.error(f"制造业行业包插入失败: {e}")
        raise
    finally:
        db.close()


if __name__ == "__main__":
    seed_manufacturing_pack()
    logger.info("OKR制造业行业包初始化完成")
