"""
Reset admin password for guggeryves@hotmail.com
"""
import asyncio
import sys
from sqlalchemy import select
from app.database import AsyncSessionLocal
from app.models.user import User
from app.models.subscription import Subscription, SubscriptionTier, SubscriptionStatus, TIER_CONFIG
from app.services.auth import AuthService
from datetime import datetime, timedelta

async def reset_admin_password():
    async with AsyncSessionLocal() as db:
        admin_email = "guggeryves@hotmail.com"
        new_password = "Admin123!"
        
        print(f"🔍 Looking for user: {admin_email}")
        result = await db.execute(select(User).where(User.email == admin_email))
        user = result.scalar_one_or_none()
        
        if not user:
            print(f"❌ User with email {admin_email} not found.")
            return
        
        print(f"✅ User found: ID={user.id}, Name={user.name}")
        
        # Update password
        user.hashed_password = AuthService.hash_password(new_password)
        user.is_verified = True
        user.is_admin = True
        user.is_active = True
        user.updated_at = datetime.utcnow()
        await db.commit()
        print(f"✅ Password updated to: {new_password}")
        
        # Ensure user has Tycoon subscription
        sub_result = await db.execute(
            select(Subscription).where(Subscription.user_id == user.id)
        )
        subscription = sub_result.scalar_one_or_none()
        
        tycoon_config = TIER_CONFIG.get(SubscriptionTier.TYCOON, {})
        
        if not subscription:
            subscription = Subscription(
                user_id=user.id,
                tier=SubscriptionTier.TYCOON,
                status=SubscriptionStatus.ACTIVE,
                max_domains=tycoon_config.get("domain_limit", 500),
                started_at=datetime.utcnow(),
                expires_at=datetime.utcnow() + timedelta(days=365 * 100),
            )
            db.add(subscription)
            await db.commit()
            print("✅ Created new Tycoon subscription.")
        elif subscription.tier != SubscriptionTier.TYCOON or subscription.status != SubscriptionStatus.ACTIVE:
            subscription.tier = SubscriptionTier.TYCOON
            subscription.status = SubscriptionStatus.ACTIVE
            subscription.max_domains = tycoon_config.get("domain_limit", 500)
            subscription.updated_at = datetime.utcnow()
            await db.commit()
            print("✅ Updated subscription to Tycoon (Active).")
        else:
            print(f"✅ Subscription: {subscription.tier.value} ({subscription.status.value})")
        
        await db.refresh(user)
        
        print("\n==================================================")
        print("📋 FINAL STATUS:")
        print(f"   Email:      {user.email}")
        print(f"   Password:   {new_password}")
        print(f"   Name:       {user.name}")
        print(f"   Admin:      {'✅ Yes' if user.is_admin else '❌ No'}")
        print(f"   Verified:   {'✅ Yes' if user.is_verified else '❌ No'}")
        print(f"   Active:     {'✅ Yes' if user.is_active else '❌ No'}")
        print("==================================================")
        print("\n✅ Admin user is ready! You can now login.")

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