import os
from app import app, db, AdminUser 
from werkzeug.security import generate_password_hash

def seed_super_admins():
    # Everything must be indented inside the function
    with app.app_context():
        # List of Super Admins to create
        super_admins = [
            {'email': 'collins.onyegbado@collnetwork.net', 'role': 'super_admin'},
            {'email': 'collnetwork2@gmail.com', 'role': 'super_admin'}
        ]
        
        # This loop must be inside the 'with' block
        for data in super_admins:
            # 🔍 Query the database for this specific email
            existing_admin = AdminUser.query.filter_by(email=data['email']).first()
            
            if not existing_admin:
                # 🏗️ Create the new admin object
                new_admin = AdminUser(email=data['email'], role=data['role'])
                
                # 🔑 Set the temporary password
                new_admin.set_password('cisco123')
                
                # ➕ Add to the current session
                db.session.add(new_admin)
                print(f"Adding {data['email']} as a Super Admin...")
            else:
                print(f"Admin {data['email']} already exists. Skipping.")

        # 🚀 This must also be inside the 'with' block to use 'db'
        db.session.commit()
        print("Database seeding complete!")

if __name__ == "__main__":
    seed_super_admins()