from typing import Optional

from src.domain.entities.product import Product
from src.domain.interfaces.product_repository import ProductRepositoryInterface
from src.core.exceptions import NotFoundError


class SaveProductUseCase:
    """Creates a new product or updates an existing one."""

    def __init__(self, product_repo: ProductRepositoryInterface):
        self.product_repo = product_repo

    def create(self, merchant_id: int, data: dict, user_id: int) -> Product:
        return self.product_repo.create(merchant_id, data, user_id)

    def update(self, product_id: int, data: dict) -> Product:
        product = self.product_repo.update(product_id, data)
        if not product:
            raise NotFoundError(f"Product {product_id} not found")
        return product
