#!/usr/bin/env python3
"""初始化管理员账号脚本 - 使用SQL直接插入"""

import asyncio
import sys
sys.path.insert(0, '/Users/Chiguyong/Code/Fischer/geo/backend')

async def init_admin():
    from app.database import AsyncSessionLocal
    from sqlalchemy import text

    email = "admin"
    password_hash = "$2b$12$fIUPOBe/MQpN1Iajrvv.DuGQJlWBPOMO/PUaYcm0fh/j.lmgKlqXa"

    async with AsyncSessionLocal() as db:
        result = await db.execute(
            text("SELECT id FROM users WHERE email = :email"),
            {"email": email}
        )
        existing = result.fetchone()

        if existing:
            await db.execute(
                text("""
                    UPDATE users
                    SET password_hash = :password_hash,
                        is_admin = true,
                        is_active = true,
                        email_verified = true,
                        plan = 'enterprise',
                        name = '管理员'
                    WHERE email = :email
                """),
                {"password_hash": password_hash, "email": email}
            )
            await db.commit()
            print(f"管理员账号已更新: {email}")
        else:
            await db.execute(
                text("""
                    INSERT INTO users (id, email, password_hash, name, is_admin, is_active, email_verified, plan, max_queries)
                    VALUES (gen_random_uuid(), :email, :password_hash, '管理员', true, true, true, 'enterprise', 500)
                """),
                {"email": email, "password_hash": password_hash}
            )
            await db.commit()
            print(f"管理员账号已创建: {email}")

        print("")
        print("登录信息:")
        print(f"  邮箱: {email}")
        print(f"  密码: admin@123")

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