#!/usr/bin/env python3
"""
为轻医美连锁品牌注入基础“岗位”数据（真落库）。

- 场景：前端岗位下拉显示 No data 时，先注入标准岗位，便于联调验证
- 数据来源：行业通用岗位，后续可按门店组织结构扩展
"""

import asyncio
from typing import List

from sqlalchemy import select

try:
    # 覆盖本地数据库连接（如存在）
    import local_config  # noqa: F401
except Exception:
    pass

from app.core.database import AsyncSessionLocal
from app.models.position import Position


async def ensure_positions_exists(session, names: List[str]) -> int:
    """确保给定岗位名称存在于数据库，已存在则跳过。

    返回新增的记录数。
    """
    existing = (await session.execute(select(Position))).scalars().all()
    existing_names = {p.name for p in existing}

    added = 0
    for name in names:
        if name in existing_names:
            continue
        obj = Position(
            name=name,
            code=name,  # 简化：与名称一致，前端无需依赖 code
            description=f"{name} 岗位（系统初始化）",
            status="active",
            level="junior",
        )
        session.add(obj)
        added += 1

    if added:
        await session.commit()
    return added


async def main() -> None:
    """脚本入口：写入基础岗位数据并打印结果。"""
    base_positions = [
        "咨询师",
        "治疗师",
        "皮肤管理师",
        "前台接待",
        "门店店长",
        "区域运营经理",
        "市场专员",
        "客服专员",
    ]

    async with AsyncSessionLocal() as session:
        added = await ensure_positions_exists(session, base_positions)
        # 打印结果
        print(f"✅ 岗位数据初始化完成，新增 {added} 条。")

        rows = (await session.execute(select(Position))).scalars().all()
        print("当前岗位：", ", ".join(p.name for p in rows))


if __name__ == "__main__":
    asyncio.run(main())


