from domain.users import User
from application.ports.user_repository import UserRepository

class CreateUser:
    def __init__(self, repo: UserRepository):
        if not isinstance(repo, UserRepository):
            raise TypeError("repo must implement UserRepository")
        self.repo = repo

    def execute(self, name:str, email:str, phone:str):
        user = User(user_id=0, email=email, name=name, phone=phone)
        self.repo.save(user)
        return user
    
class ViewUsers:
    def __init__(self, repo: UserRepository):
        if not isinstance(repo, UserRepository):
            raise TypeError("repo must implement UserRepository")
        self.repo = repo
    
    def execute(self):
        users = self.repo.viewAll()
        return users
    
class ViewUserById:
    def __init__(self, repo: UserRepository):
        if not isinstance(repo, UserRepository):
            raise TypeError("repo must implement UserRepository")
        self.repo = repo
    
    def execute(self, user_id: int) -> User | None:
        user = self.repo.viewById(user_id)
        return user