"""Fix password for IC user"""
import asyncio
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
from sqlalchemy.orm import sessionmaker
from sqlalchemy import select

DATABASE_URL = "mysql+aiomysql://root:@localhost:3306/smartlife_db?charset=utf8mb4"

async def main():
    from app.models.user import User
    from app.core.security import get_password_hash
    
    engine = create_async_engine(DATABASE_URL, echo=False)
    async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
    
    async with async_session() as session:
        result = await session.execute(
            select(User).where(User.email == "ithelpdesk@ic.gov.sa")
        )
        user = result.scalar_one_or_none()
        
        if user:
            user.password_hash = get_password_hash("ithelpdesk")
            await session.commit()
            print("✅ Password updated successfully!")
        else:
            print("❌ User not found")
    
    await engine.dispose()

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