from typing import Optional
from api import models, schemas
from api.security import verify_password, get_password_hash
from api.logics.base import CRUDBase

class User(CRUDBase[models.User, schemas.UserCreate,schemas.UserUpdate]):
    def create(self, db, *, obj_in: schemas.UserCreate) -> models.User:
        db_obj = models.User(
            email=obj_in.email,
            hashed_password=get_password_hash(obj_in.password),
            full_name=obj_in.full_name,
            is_superuser=obj_in.is_superuser,
        )
        db.add(db_obj)
        db.commit()
        db.refresh(db_obj)
        return db_obj

    def authenticate(self, db, *, email: str, password: str) -> Optional[models.User]:
        user = self.get_by_email(db, email=email)
        if not user:
            return None
        if not verify_password(password, user.hashed_password):
            return None
        return user

    def get_by_email(self, db, *, email: str) -> Optional[models.User]:
        return db.query(models.User).filter(models.User.email == email).first()

    def is_active(self, user: models.User) -> bool:
        return user.is_active

user = User(models.User)
