import asyncio
import os
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
from sqlalchemy.orm import sessionmaker
from sqlalchemy import select
from dotenv import load_dotenv
import bcrypt

load_dotenv()

async def main():
    engine = create_async_engine(os.environ['DATABASE_URL'])
    SessionLocal = sessionmaker(engine, class_=AsyncSession)
    
    async with SessionLocal() as db:
        from app.models.rm_models import VendorMaster
        
        result = await db.execute(select(VendorMaster))
        vendors = result.scalars().all()
        
        initial_password = os.environ.get("VENDOR_PORTAL_INITIAL_PASSWORD")
        if not initial_password:
            raise SystemExit("Set VENDOR_PORTAL_INITIAL_PASSWORD before enabling vendor portal access.")
        updated = 0
        for vendor in vendors:
            vendor.portal_enabled = True  
            
            # Ensure they have a username
            if not vendor.portal_username:
                phone_val = vendor.phones[0] if (vendor.phones and isinstance(vendor.phones, list) and len(vendor.phones) > 0) else None
                vendor.portal_username = phone_val if phone_val else f"vendor_{str(vendor.vendor_id)[:8]}"
                
            # Initial portal password comes from VENDOR_PORTAL_INITIAL_PASSWORD; vendors change it after first login.
            new_password = initial_password[:72]

            # Hash directly with bcrypt (bcrypt limits passwords to 72 bytes)
            salt = bcrypt.gensalt()
            hashed_bytes = bcrypt.hashpw(new_password.encode('utf-8'), salt)
            # Store the hash prefix for passlib compatibility ($2b$)
            vendor.portal_password_hash = hashed_bytes.decode('utf-8')
            updated += 1
            
        if updated > 0:
            await db.commit()
            print(f"Successfully updated passwords and enabled portal access for {updated} vendors.")
            print("Format: Username = <username>, Password = <username>123")
        else:
            print("No vendors found to update.")

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