from fastapi import Request, HTTPException
from starlette.middleware.sessions import SessionMiddleware
from passlib.context import CryptContext

# Password hashing
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")

# 👉 Change these credentials
ADMIN_USERNAME = "admin"
ADMIN_PASSWORD_HASH = pwd_context.hash("Admin123")

def verify_password(plain, hashed):
    return pwd_context.verify(plain, hashed)

def authenticate(username: str, password: str):
    if username == ADMIN_USERNAME and verify_password(password, ADMIN_PASSWORD_HASH):
        return True
    return False

def require_login(request: Request):
    if not request.session.get("user"):
        raise HTTPException(status_code=401, detail="Not authenticated")