package de.effigenix.infrastructure.masterdata.persistence.repository;

import de.effigenix.domain.masterdata.CategoryName;
import de.effigenix.domain.masterdata.ProductCategory;
import de.effigenix.domain.masterdata.ProductCategoryId;
import de.effigenix.domain.masterdata.ProductCategoryRepository;
import de.effigenix.infrastructure.masterdata.persistence.mapper.ProductCategoryMapper;
import de.effigenix.shared.common.RepositoryError;
import de.effigenix.shared.common.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Repository
@Profile("!no-db")
@Transactional(readOnly = true)
public class JpaProductCategoryRepository implements ProductCategoryRepository {

    private static final Logger logger = LoggerFactory.getLogger(JpaProductCategoryRepository.class);

    private final ProductCategoryJpaRepository jpaRepository;
    private final ProductCategoryMapper mapper;

    public JpaProductCategoryRepository(ProductCategoryJpaRepository jpaRepository, ProductCategoryMapper mapper) {
        this.jpaRepository = jpaRepository;
        this.mapper = mapper;
    }

    @Override
    public Result<RepositoryError, Optional<ProductCategory>> findById(ProductCategoryId id) {
        try {
            Optional<ProductCategory> result = jpaRepository.findById(id.value())
                    .map(mapper::toDomain);
            return Result.success(result);
        } catch (Exception e) {
            logger.trace("Database error in findById", e);
            return Result.failure(new RepositoryError.DatabaseError(e.getMessage()));
        }
    }

    @Override
    public Result<RepositoryError, List<ProductCategory>> findAll() {
        try {
            List<ProductCategory> result = jpaRepository.findAll().stream()
                    .map(mapper::toDomain)
                    .collect(Collectors.toList());
            return Result.success(result);
        } catch (Exception e) {
            logger.trace("Database error in findAll", e);
            return Result.failure(new RepositoryError.DatabaseError(e.getMessage()));
        }
    }

    @Override
    @Transactional
    public Result<RepositoryError, Void> save(ProductCategory category) {
        try {
            jpaRepository.save(mapper.toEntity(category));
            return Result.success(null);
        } catch (Exception e) {
            logger.trace("Database error in save", e);
            return Result.failure(new RepositoryError.DatabaseError(e.getMessage()));
        }
    }

    @Override
    @Transactional
    public Result<RepositoryError, Void> delete(ProductCategory category) {
        try {
            jpaRepository.deleteById(category.id().value());
            return Result.success(null);
        } catch (Exception e) {
            logger.trace("Database error in delete", e);
            return Result.failure(new RepositoryError.DatabaseError(e.getMessage()));
        }
    }

    @Override
    public Result<RepositoryError, Boolean> existsByName(CategoryName name) {
        try {
            return Result.success(jpaRepository.existsByName(name.value()));
        } catch (Exception e) {
            logger.trace("Database error in existsByName", e);
            return Result.failure(new RepositoryError.DatabaseError(e.getMessage()));
        }
    }
}
